-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSet.cpp
211 lines (186 loc) Β· 7.09 KB
/
Set.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include <iostream>
using namespace std;
/*βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ*/
class Set{
public:
int count = 0;
int set[100];
void InsertToSet(Set*, int a);
void UnionOfSets(Set*, Set*);
void IntersectionOfSets(Set*, Set*);
void DifferenceOfSets1(Set*, Set*);
void DifferenceOfSets2(Set*, Set*);
int CardinalityOfSet(Set*);
int Membership(Set*, int key);
void Display(Set*);
};
/*βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ*/
void Set :: InsertToSet(Set *s, int a){
s->set[count] = a;
s->count++;
}
/*βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ*/
void Set :: UnionOfSets(Set *s1, Set *s2){
cout << "Union of set 1 and set 2 : {";
int k;
for (int a = 0; a < s1->count; a++){
cout << s1->set[a] << " ,";
}
for (int i = 0; i < s2->count; i++){
for (k = 0; k < s1->count; k++){
if (s2->set[i] == s1->set[k]){
break;
}
}
if (k == s1->count){
cout << s2->set[i] << " ,";
}
}
cout << "}\n";
}
/*βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ*/
void Set :: IntersectionOfSets(Set *s1, Set *s2){
cout<< "Intersection of set 1 and set 2 : {";
for (int i = 0; i < s1->count; i++){
for (int j = 0; j < s2->count; j++){
if (s1->set[i] == s2->set[j])
cout << s1->set[i] << " ,";
}
}
cout << "}\n";
}
/*βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ*/
void Set :: DifferenceOfSets1(Set *s1, Set *s2){
cout << "Difference of set 1 and set 2 : {";
int l;
for (int i = 0; i < s1->count; i++){
for (l = 0; l < s2->count; l++){
if (s1->set[i] == s2->set[l]){
break;
}
}
if (l == s2->count)
cout << s1->set[i] << " ,";
}
cout << "}\n";
}
/*βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ*/
void Set :: DifferenceOfSets2(Set *s1, Set *s2){
cout <<"Difference of set 2 and set 1 : {";
int l;
for (int i = 0; i < s2->count; i++){
for (l = 0; l < s1->count; l++){
if (s2->set[i] == s1->set[l]){
break;
}
}
if (l == s1->count)
cout << s2->set[i] << " ,";
}
cout << "}\n";
}
/*βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ*/
int Set :: CardinalityOfSet(Set* s){
return s->count;
}
/*βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ*/
int Set :: Membership(Set* s, int key){
int i;
for (i = 0; i < s->count; i++){
if (s->set[i] == key)
return 1;
}
return 0;
}
/*βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ*/
void Set :: Display(Set* s){
cout << "{";
for (int i = 0; i < s->count; i++){
cout << s->set[i] << " ,";
}
cout << "}\n";
}
/*βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ*/
int main (){
Set s1, s2, obj;
int x, y, key;
int ch, val;
cout << "1 βββββ> Insert in Set 1\n";
cout << "2 βββββ> Insert in Set 2\n";
cout << "3 βββββ> Union of Set 1 and Set 2\n";
cout << "4 βββββ> Intersection of Set 1 and Set 2\n";
cout << "5 βββββ> Difference between Sets (Set 1 - Set 2)\n";
cout << "6 βββββ> Difference between Sets (Set 2 - Set 1)\n";
cout << "7 βββββ> Cardinality of Set 1\n";
cout << "8 βββββ> Cardinality of Set 2\n";
cout << "9 βββββ> Check for Membership (Set 1)\n";
cout << "10 βββββ> Check for Membership (Set 2)\n";
cout << "11 βββββ> Display Set 1\n";
cout << "12 βββββ> Display Set 2\n";
cout << "13 βββββ> Exit\n";
do{
cout << "\nEnter your choice : ";
cin >> ch;
switch(ch){
case 1:
cout << "Enter the Element to be Added to the Set 1 : ";
int a;
cin >> a;
s1.InsertToSet(&s1, a);
break;
case 2:
cout << "Enter the Element to be Added to the Set 2 : ";
int b;
cin >> b;
s2.InsertToSet(&s2, b);
break;
case 3:
obj.UnionOfSets(&s1, &s2);
break;
case 4:
obj.IntersectionOfSets(&s1, &s2);
break;
case 5:
obj.DifferenceOfSets1(&s1, &s2);
break;
case 6:
obj.DifferenceOfSets2(&s1, &s2);
break;
case 7:
x = s1.CardinalityOfSet(&s1);
cout << "Cardinality of Set 1 : " << x << "\n";
break;
case 8:
y = s2.CardinalityOfSet(&s2);
cout << "Cardinality of Set 2 : " << y << "\n";
break;
case 9:
cout << "Enter the Element to be Checked : ";
cin >> key;
if (s1.Membership(&s1, key))
cout << key << " is present in Set 1\n";
else
cout << key << " is not present in Set 1\n";
break;
case 10:
cout << "Enter the Element to be Checked : ";
cin >> key;
if (s2.Membership(&s2, key))
cout << key << " is present in Set 2\n";
else
cout << key << " is not present in Set 2\n";
break;
case 11:
s1.Display(&s1);
break;
case 12:
s2.Display(&s2);
break;
case 13:
break;
default :
cout <<"Enter a Value between 1 and 13";
}
}while(ch != 13);
}
/*---------------------------------------------------------------------------*/