-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathList.cpp
More file actions
239 lines (227 loc) · 5.35 KB
/
Copy pathList.cpp
File metadata and controls
239 lines (227 loc) · 5.35 KB
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
//WAP to create a class List of two sets and perform basic operation
#include <iostream>
#include <iomanip>
#define max 100
using namespace std;
class List
{
private: int a[max],size;
public : List() //Default Constructor
{
size=0;
}
List(int s)//Paramerterised Constructor
{
size=s;
}
List(const List& A) //Copy Constructor
{
size=A.size;
for(int i=0;i<size;i++)
{
a[i]=A.a[i];
}
}
List operator+(const List& A);
List operator*(const List& A);
List operator-(const List& A);
void inputArray();
void sortArray();
void const print();
};
void List::inputArray() //Function to take input in array
{
cout<<"Enter the elements"<<endl;
for(int i=0;i<size;i++)
cin>>a[i];
}
void const List::print() //Function to print array
{
if(size==0)
cout<<"{Empty}"<<endl;
else
{
cout<<"{";
for(int i=0;i<size;i++)
cout<<a[i]<<",";
cout<<"\b }"<<endl;
}
}
void List::sortArray() //Function to sort array in ascending order
{
if(size>1)
{
int min,temp;
for(int i=0;i<size-1;i++)
{
min=i;
for(int j=i+1;j<size;j++)
if(a[j]<a[min])
min=j;
temp=a[min];
a[min]=a[i];
a[i]=temp;
}
}
else
return;
}
List List::operator+(const List& A) //Function to union two sets
{
if(size==0&&A.size==0)
return (*this);
else
{
List B(size+A.size+1);
for(int i=0;i<A.size;i++) //Inserting Set 1 in Union Set
B.a[i]=A.a[i];
int d=A.size,k;
for(int i=0;i<size;i++)
{
k=0;
for(int j=0;j<A.size;j++)
{
if(A.a[j]==a[i])
k++;
}
if(k==0)
B.a[d++]=a[i]; //Inserting Elements of Set 2 if not repeated
B.size=d;
}
return B;
}
}
List List::operator*(const List& A) //Function for intersection
{
if(size==0&&A.size==0)
return (*this);
else
{
List ob(size);
int t=0,d,b=0;
for(int i=0;i<size;i++)
{
d=0;
for(int j=0;j<A.size;j++)
{
if(a[i]==A.a[j])
d++;
}
if(d!=0)
{
for(int k=0;k<ob.size;k++)
{
if(a[i]==ob.a[k])
b++;
}
if(b==0)
ob.a[t++]=a[i]; //Putting elements in Intersection set
}
ob.size=t;
}
List B(ob.size);
t=0;
for(int i=0;i<ob.size;i++)
{
d=0;
for(int j=i+1;j<ob.size;j++)
{
if(ob.a[i]==ob.a[j])
d++;
}
if(d==0)
B.a[t++]=ob.a[i]; //Removing Repeated elements
B.size=t;
}
return B;
}
}
List List::operator-(const List& A) //Function for Subtracting Sets
{
if(size==0&&A.size==0)
return (*this);
else
{
List B(size);
int t=0,d;
for(int i=0;i<size;i++)
{
d=0;
for(int j=0;j<A.size;j++)
{
if(a[i]==A.a[j])
d++;
}
if(d==0)
B.a[t++]=a[i];
B.size=t;
}
return B;
}
}
int main()
{
int s,s1,i,j=0;
while(j!=1)
{
List B,ob2,ob1,A,U,I,ob3,AB,CA,CB,BA;
cout<<"Enter the size of Set 1"<<endl;
cin>>s;
if(s!=0)
{
A=List(s);
A.inputArray();
ob1=List(A); //Storing A in ob1 using copy constructor
}
cout<<"Do you want to input second array? Enter 1 for yes or 0 for No"<<endl;
cin>>i;
if(i==1)
{
cout<<"Enter the size of Set 2"<<endl;
cin>>s1;
if(s1!=0)
{
B=List(s1);
B.inputArray();
ob2=List(B); //Storing B in ob2 using copy constructor
}
}
if(s==0)
U=ob2+ob1;
else
U=ob1+ob2; //Taking Union
ob3=List(U); //Storing Union in ob3 using copy constructor
CA=ob3-ob1; //Complementing A
CB=ob3-ob2; //Complementing B
AB=ob1-ob2; //A-B
BA=ob2-ob1; //B-A
I=ob1*ob2; //Taking Intersection
cout<<"\n\nPrinting Details\n\n"<<endl;
cout<<"Set 1"<<endl;
A.sortArray();
A.print();
cout<<"Set 2"<<endl;
B.print();
cout<<"Union"<<endl;
U.sortArray();
U.print();
cout<<"Intersection"<<endl;
I.sortArray();
I.print();
cout<<"Complement A"<<endl;
CA.sortArray();
CA.print();
cout<<"Complement B"<<endl;
CB.sortArray();
CB.print();
cout<<"A-B"<<endl;
AB.sortArray();
AB.print();
cout<<"B-A"<<endl;
BA.sortArray();
BA.print();
cout<<"\n \n ENTER 1 TO EXIT PROGRAM OR 0 TO RUN AGAIN"<<endl;
cin>>j;
}
return 1;
}