Skip to content

Commit 16c2593

Browse files
committed
Create symmetric-difference.cpp
1 parent cf53fb0 commit 16c2593

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed

C++/symmetric-difference.cpp

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
#include <iostream>
2+
#include <list>
3+
using namespace std;
4+
5+
class SymmetricDifference
6+
{
7+
struct SETS
8+
{
9+
list<int> set;
10+
};
11+
12+
private:
13+
14+
SETS* _SETS;
15+
list<int> _SYMMETRIC_DIFFERENCE;
16+
int _NUMBER_OF_SETS;
17+
18+
bool IsUnique(int control)
19+
{
20+
for(int number : _SYMMETRIC_DIFFERENCE)
21+
{
22+
if(control == number)
23+
{
24+
return false;
25+
}
26+
}
27+
28+
return true;
29+
}
30+
31+
void Difference(list<int> set)
32+
{
33+
for(int number : set)
34+
{
35+
if(IsUnique(number))
36+
{
37+
_SYMMETRIC_DIFFERENCE.push_back(number);
38+
}
39+
else
40+
{
41+
_SYMMETRIC_DIFFERENCE.remove(number);
42+
}
43+
}
44+
45+
_SYMMETRIC_DIFFERENCE.sort();
46+
}
47+
48+
public:
49+
50+
SymmetricDifference(list<list<int>> sets)
51+
{
52+
this->_NUMBER_OF_SETS = sets.size() - 1;
53+
this->_SETS = new SETS[_NUMBER_OF_SETS];
54+
int index = -1;
55+
56+
for(list<int> set : sets)
57+
{
58+
switch(index)
59+
{
60+
case -1:
61+
this->_SYMMETRIC_DIFFERENCE = set;
62+
_SYMMETRIC_DIFFERENCE.sort();
63+
_SYMMETRIC_DIFFERENCE.unique();
64+
break;
65+
66+
default:
67+
this->_SETS[index].set = set;
68+
this->_SETS[index].set.sort();
69+
this->_SETS[index].set.unique();
70+
break;
71+
}
72+
73+
index++;
74+
}
75+
}
76+
77+
~SymmetricDifference()
78+
{
79+
delete _SETS;
80+
}
81+
82+
void Calculate()
83+
{
84+
for(int i = 0; i < _NUMBER_OF_SETS; i++)
85+
{
86+
Difference(_SETS[i].set);
87+
}
88+
}
89+
90+
void PrintSymmetricDifference()
91+
{
92+
for(int number : _SYMMETRIC_DIFFERENCE)
93+
{
94+
cout << number << ", ";
95+
}
96+
97+
cout << endl;
98+
}
99+
};
100+
101+
main()
102+
{
103+
list<list<int>> sets;
104+
sets.push_back({ 3, 3, 3, 2, 5 }); // 1
105+
sets.push_back({ 2, 1, 5, 7 }); // 2
106+
sets.push_back({ 3, 4, 6, 6 }); // 3
107+
sets.push_back({ 1, 2, 3 }); // 4
108+
sets.push_back({ 5, 3, 9, 8 }); // 5
109+
sets.push_back({ 1 }); // 6
110+
111+
SymmetricDifference symmetricDifference = SymmetricDifference(sets);
112+
symmetricDifference.Calculate();
113+
symmetricDifference.PrintSymmetricDifference();
114+
115+
return 0;
116+
}

0 commit comments

Comments
 (0)