Created
September 5, 2023 11:29
-
-
Save HSley13/35d623758740cf84285189895c7d81d7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <cstdio> | |
#include <cmath> | |
#include <cctype> | |
#include <cstdlib> | |
using namespace std; | |
const int maxsize=100; // Constant Maximum size of every Sets | |
class set | |
{ | |
int len; | |
char members[maxsize]; | |
int find(char ch); | |
public: | |
set() | |
{ | |
len=0; | |
} | |
int getlen() | |
{ | |
return len; | |
} | |
bool isMember(char ch); // Check whether a character is a member or not | |
void showset(); | |
set operator+(char ch); // Adding One character to the Set | |
set operator-(char ch); // Removing One character to the Set | |
set operator+(set ob2); // Union of 2 Sets | |
set operator-(set ob2); // Difference of 2 Sets | |
bool operator<(set ob2); // Set in the left is a subset of the one in the right | |
bool operator>(set ob2); // Set in the left is a superset of the one in the right | |
set operator&(set ob2); // Intersection of 2 Sets | |
set operator|(set ob2); // Symetric difference of 2 Sets | |
}; | |
int set::find(char ch) | |
{ | |
for(int i=0; i<len; i++) | |
if(ch==members[i]) return i; // checking if Ch is in the member list, returning its array location if foound and -1 if not found | |
return -1; | |
} | |
bool set::isMember(char ch) | |
{ | |
if(find(ch)!=-1) return true; //checking if Ch is found in the list and returning True, that's why we createad the Find() function | |
else return false; | |
} | |
void set::showset() | |
{ | |
cout<<"{ "; | |
for(int i=0; i<len; i++) | |
{ | |
cout<<members[i]<<" "; // display every character held in the Set | |
} | |
cout<<"}"; | |
} | |
set set::operator+(char ch) | |
{ | |
if (len==maxsize) | |
{ | |
cout<<"The Set is Full\n"; // checking if the Set is full by comparing the last array location which is Len to the maxsize | |
return *this; // returning the current Set is it's Full | |
} | |
set newset= *this; // coppying the current Set cause we'll just add the new character to it | |
if(isMember(ch)) return *this; // returning the current Set if the character is already part of it | |
else | |
{ | |
newset.members[newset.len]= ch; // adding Ch to the newSet Member list by precising in the last newSet array location | |
newset.len++; // incremention the last array location if there's a possible addition to it | |
} | |
return newset; // returning the newset which holds the new and hold characters | |
} | |
set set::operator-(char ch) | |
{ | |
set newset; | |
int i= find(ch); // i is holding the array location of the character you want to take out with the find() function | |
for(int j=0; j<len; j++) | |
{ | |
if(j!=i) newset= newset + members[j]; //adding every other characters to the newset except the one whose array location is equal to the one stored in i | |
} | |
return newset; | |
} | |
set set::operator+(set ob2) | |
{ | |
set newset= *this; // coppying the current set | |
for(int i=0; i<ob2.len; i++) | |
{ | |
newset= newset + ob2.members[i]; //adding the Second Set member characters from the copy of the current set we have made | |
} | |
return newset; | |
} | |
set set::operator-(set ob2) | |
{ | |
set newset= *this; // coppying the current set | |
for(int i=0; i<ob2.len; i++) | |
{ | |
newset= newset - ob2.members[i]; //removing the Second Set member characters from the copy of the current set we have made | |
} | |
return newset; | |
} | |
bool set::operator<(set ob2) | |
{ | |
if(len>ob2.len) return false; // ob1 has more elements | |
for (int i=0; i<len; i++) | |
if(ob2.find(members[i])==-1) return false; | |
return true; | |
} | |
bool set::operator>(set ob2) | |
{ | |
if(len<ob2.len) return false; // ob2 has more elements | |
for (int i=0; i<ob2.len; i++) | |
if(find(ob2.members[i])==-1) return false; | |
return true; | |
} | |
set set::operator&(set ob2) | |
{ | |
set newset; | |
for(int i=0; i<len; i++) | |
if(ob2.find(members[i])!=-1) newset= newset + members[i]; //Checking for every character of the first Set that are in the second Set | |
return newset; | |
} | |
set set::operator|(set ob2) | |
{ | |
set newset; | |
for(int i=0; i<len; i++) | |
if(ob2.find(members[i])!=-1) newset= newset+ members[i]; | |
*this= *this - newset; | |
// just take the Intersection of both out of the first Set | |
return *this; | |
} | |
int main() | |
{ | |
set s1, s2, s3; | |
char j, k, l; | |
cout<<"Enter 4 characters into the Set 1 \n"; | |
for (int i=0; i<4; i++) | |
{ | |
cin>>j; | |
s1= s1 + j; | |
} | |
cout<<" The characters inside the Set 1 are: "; | |
s1.showset(); | |
cout<<"\n"; | |
cout<<"Enter 4 characters into the Set 2 \n"; | |
for (int i=0; i<4; i++) | |
{ | |
cin>>k; | |
s2= s2 + k; | |
} | |
cout<<" The characters inside the Set 2 are: "; | |
s2.showset(); | |
cout<<"\n"; | |
cout<<"Enter 4 characters into the Set 3 \n"; | |
for (int i=0; i<4; i++) | |
{ | |
cin>>l; | |
s3= s3 + l; | |
} | |
cout<<" The characters inside the Set 3 are: "; | |
s3.showset(); | |
cout<<"\n"; | |
cout<<"Look for One of the characters you've entered into the Set 1 \n"; | |
char q; | |
cin>>q; | |
if(s1.isMember(q)) cout<<q<<" is a member of the Set 1\n"; | |
else cout<<q<<" is not a member of the Set 1\n"; | |
cout<<"Look for One of the characters you've entered into the Set 3 \n"; | |
char e; | |
cin>>e; | |
if(s1.isMember(e)) cout<<w<<" is a member of the Set 3\n"; | |
else cout<<e<<" is not a member of the Set 3\n"; | |
cout<<" We gonna add One last character in the Set 1\n"; | |
char t; | |
cin>>t; | |
s1=s1 + t; | |
cout<<" Now all the values of Set 1 are : "; | |
s1.showset(); | |
cout<<"\n"; | |
cout<<" We gonna add One last character in the Set 3\n"; | |
char v; | |
cin>>v; | |
s3=s3 + v; | |
cout<<" Now all the values of Set 3 are : "; | |
s3.showset(); | |
cout<<"\n"; | |
cout<<"Take One character out of Set 1.\n"; | |
char g; | |
cin>>g; | |
s1= s1-g; | |
cout<<"Now Set 1 only has: "; | |
s1.showset(); | |
cout<<"\n"; | |
cout<<"Take One character out of Set 3\n"; | |
char n; | |
cin>>n; | |
s3= s3-n; | |
cout<<"Now Set 3 only has: "; | |
s3.showset(); | |
cout<<"We gonna make the Union of Set 2 and Set 3 which is Set 5 : "; | |
set s5= s2 + s3; | |
s5.showset(); | |
cout<<" \n"; | |
cout<<"We gonna make the difference of Set 2 and Set 3 which is Set 8 : "; | |
set s8= s2 - s3; | |
s8.showset(); | |
cout<<" \n"; | |
cout<<"We gonna make the difference of Set 1 and Set 3 which is Set 9 : "; | |
set s9= s1 - s3; | |
s9.showset(); | |
cout<<" \n"; | |
if(s3<s2) cout<<" S3 is a subset of S2\n"; | |
else cout<<" S3 is not a subset of S2\n"; | |
if(s2<s3) cout<<" S2 is a subset of S3\n"; | |
else cout<<" S2 is not a subset of S3\n"; | |
set s10= s1&s2; | |
cout<<"The intersection of S1 and S2 is : \n"; | |
s10.showset(); | |
cout<<"\n"; | |
set s12= s2|s3; | |
cout<<" The Symetric difference between S1 and S2 is: "; | |
s12.showset(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment