-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSetADT.cpp
277 lines (242 loc) · 6.55 KB
/
SetADT.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include <iostream>
using namespace std;
const int MAX = 10;
template<class T>
class Set {
T data[MAX];
int n;
public:
Set() {
n = -1;
}
bool insertItem(T);
bool removeItem(T);
bool contains(T);
int size();
void print();
void input(int num);
Set union1(Set, Set);
Set intersection1(Set, Set);
Set difference1(Set, Set);
bool subSet(Set);
};
template<class T>
bool Set<T>::subSet(Set<T> s2) {
int count = 0;
int size = s2.size();
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= s2.n; j++) {
if (data[i] == s2.data[j]) {
count++;
break;
}
}
}
if (count == size) {
return true;
}
return false;
}
template<class T>
void Set<T>::input(int num) {
T item;
for (int i = 0; i < num; i++) {
if (i == 0) {
cout << "\nEnter the " << i + 1 << "st item: ";
} else if (i == 1) {
cout << "\nEnter the " << i + 1 << "nd item: ";
} else if (i == 2) {
cout << "\nEnter the " << i + 1 << "rd item: ";
} else {
cout << "\nEnter the " << i + 1 << "th item: ";
}
cin >> item;
insertItem(item);
}
}
template<class T>
void Set<T>::print() {
for (int i = 0; i <= n; i++)
cout << " " << data[i];
}
template<class T>
Set<T> Set<T>::union1(Set<T> s1, Set<T> s2) {
Set<T> s3;
int flag = 0;
int i = 0;
for (i = 0; i <= s1.n; i++) {
s3.insertItem(s1.data[i]);
}
for (int j = 0; j <= s2.n; j++) {
flag = 0;
for (i = 0; i <= s1.n; i++) {
if (s1.data[i] == s2.data[j]) {
flag = 1;
break;
}
}
if (flag == 0) {
s3.insertItem(s2.data[j]);
}
}
return s3;
}
template<class T>
Set<T> Set<T>::difference1(Set<T> s1, Set<T> s2) {
Set<T> s3;
int flag = 1;
for (int i = 0; i <= s1.n; i++) {
for (int j = 0; j <= s2.n; j++) {
if (s1.data[i] == s2.data[j]) {
flag = 0;
break;
} else
flag = 1;
}
if (flag == 1) {
s3.insertItem(s1.data[i]);
}
}
return s3;
}
template<class T>
Set<T> Set<T>::intersection1(Set<T> s1, Set<T> s2) {
Set<T> s3;
for (int i = 0; i <= s1.n; i++) {
for (int j = 0; j <= s2.n; j++) {
if (s1.data[i] == s2.data[j]) {
s3.insertItem(s1.data[i]);
break;
}
}
}
return s3;
}
template<class T>
bool Set<T>::insertItem(T item) {
if (n >= MAX - 1) {
cout << "\nOverflow, set is full.";
return false;
}
data[++n] = item;
return true;
}
template<class T>
bool Set<T>::removeItem(T item) {
if (n == -1) {
cout << "\nUnderflow, cannot perform delete operation on empty set.";
return false;
}
for (int i = 0; i <= n; i++) {
if (data[i] == item) {
for (int j = i; j < n; j++) {
data[j] = data[j + 1];
}
return true;
}
}
return false;
}
template<class T>
bool Set<T>::contains(T item) {
for (int i = 0; i <= n; i++) {
if (data[i] == item)
return true;
}
return false;
}
template<class T>
int Set<T>::size() {
return n + 1;
}
int main() {
Set<int> s1, s2, s3;
int choice;
int size1, size2, item;
cout << "\nEnter number of items in the first set: ";
cin >> size1;
s1.input(size1);
cout << "\nEnter number of items in the second set: ";
cin >> size2;
s2.input(size2);
do {
cout << "\n====================SET OPERATIONS===================="
<< "\n1.Insert" << "\n2.Remove" << "\n3.Contains" << "\n4.Size of the set" << "\n5.Intersection"
<< "\n6.Union" << "\n7.Difference" << "\n8.Check if subset" << "\n9.Display" << "\n0. Exit"
<< "\nPlease enter the choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "\nPlease enter the item: ";
cin >> item;
if (s1.insertItem(item)) {
cout << "\n" << item << " inserted.";
} else {
cout << "\n" << "Insertion can not be done.";
}
break;
case 2:
cout << "\nPlease enter the item: ";
cin >> item;
if (s1.removeItem(item)) {
cout << "\n" << item << " deleted.";
} else {
cout << "\n" << "Deletion can not be done.";
}
break;
case 3:
cout << "\nEnter item: ";
cin >> item;
if (s1.contains(item)) {
cout << "\n" << item << " is present.";
} else {
cout << "\n" << item << "is not present.";
}
break;
case 4:
cout << "\nSize of set is: " << s1.size();
break;
case 5:
s3 = s1.intersection1(s1, s2);
cout << "\nFirst set's items are: ";
s1.print();
cout << "\nSecond set's items are: ";
s2.print();
cout << "\nIntersection: ";
s3.print();
break;
case 6:
s3 = s1.union1(s1, s2);
cout << "\nFirst set's items are: ";
s1.print();
cout << "\nSecond set's items are: ";
s2.print();
cout << "\nUnion: ";
s3.print();
break;
case 7:
s3 = s1.difference1(s1, s2);
cout << "\nFirst set's items are: ";
s1.print();
cout << "\nSecond set's items are: ";
s2.print();
cout << "\nDifference: ";
s3.print();
break;
case 8:
if (s1.subSet(s2)) {
cout << "\nSecond set is subset of first set.";
} else {
cout << "\nSecond set is not a subset of first set.";
}
break;
case 9:
cout << "\nFirst set's items are: ";
s1.print();
cout << "\nSecond set's items are: ";
s2.print();
break;
}
} while (choice != 0);
return 0;
}