Repository navigation
Expand file tree
/
Copy pathclass_Set.cpp
More file actions
168 lines (140 loc) · 3.24 KB
/
Copy pathclass_Set.cpp
File metadata and controls
168 lines (140 loc) · 3.24 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
//bsdsf25m050
#include<iostream>
#include<string>
using namespace std;
class Set{
private:
int *data;
int capacity;
int noofelements;
public:
Set ( int cap = 5){
if(cap<=0){
capacity=5;
}
else{
capacity=cap;
}
data = new int[capacity];
noofelements=0;
}
Set( const Set & ref){
capacity=ref.capacity;
noofelements=ref.noofelements;
data=new int[capacity];
for(int i=0; i<noofelements; i++){
data[i]=ref.data[i];
}
}
~Set(){
delete [] data;
}
bool contain(int n){
for(int i=0; i<noofelements; i++){
if(data[i]==n){
return true;
}
}
return false;
}
void insert ( int element){
if(noofelements==capacity){
cout<<"no space"<<endl;
return ;
}
if(!contain(element)){
data[noofelements]=element;
noofelements++;
}
}
void remove ( int element){
for(int i=0;i<noofelements;i++){
if(data[i]==element){
for(int j=i; j<noofelements-1; j++){
data[j]=data[j+1];
}
noofelements--;
return;
}
}
}
Set calcUnion ( Set & s2 ){
Set result(capacity+s2.capacity);
for(int i=0; i<noofelements; i++){
result.insert(data[i]);
}
for(int i=0; i<s2.noofelements; i++){
result.insert(s2.data[i]);
}
return result;
}
Set calcIntersection ( Set & s2 ){
Set result(capacity);
for(int i=0; i<s2.noofelements; i++){
if(s2.contain(data[i])){
result.insert(data[i]);
}
}
return result;
}
Set calcSymmetricDifference ( Set & s2 ){
Set result(capacity+s2.capacity);
for(int i=0; i<noofelements; i++){
if(!s2.contain(data[i])){
result.insert(data[i]);
}
}
for(int i=0; i<s2.noofelements; i++){
if(!contain(s2.data[i])){
result.insert(s2.data[i]);
}
}
return result;
}
int getCardinality(){
return noofelements;
}
void display(){
cout<<" { ";
for(int i=0; i<noofelements; i++){
cout<<data[i]<<" ";
}
cout<<"}"<<endl;
}
};
int main(){
int size1;
cout<<"enter size of set 1: "<<endl;
cin>>size1;
Set s1(size1);
cout<<"enter elements of se1 2: "<<endl;
for(int i=0;i<size1; i++){
int j;
cin>>j;
s1.insert(j);
}
int size2;
cout<<"enter size of set 2 : "<<endl;
cin>>size2;
Set s2(size2);
cout<<"enter elements of set 2: "<<endl;
for(int i=0;i<size2; i++){
int j;
cin>>j;
s2.insert(j);
}
Set s3=s1.calcUnion(s2);
cout<<"union : ";
s3.display();
Set s4=s1.calcIntersection(s2);
cout<<"intersection : ";
s4.display();
Set s5=s1.calcSymmetricDifference(s2);
cout<<"symmetric difference: ";
s5.display();
cout<<"cardinality of s2 is : " <<s2.getCardinality()<<endl;
s1.remove(2);
cout<<"after removal set 1 is: ";
s1.display();
return 0;
}