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
|
// sets
#include <iostream>
using namespace std;
const int MaxSize = 100;
class Set{
public:
int len;
char members[MaxSize]; // this array holds the set
/* The find() function is private because
it is not used outside the set class */
int find(char ch); // find an element in the set
// Construct a null set.
Set() { len = 0;}
// Return the number of elements in the set
int getLength(){ return len;}
void showset(); // Display the set
bool isMember(char ch); // check for membership
Set operator+(char ch); // add an element to the set
Set operator-(char ch); // remove an element from the set
Set operator+(Set ob2); // set union
Set operator-(Set ob2); // set different
bool operator<(Set ob2); // subset
bool operator>(Set ob2); // superset
bool operator==(Set ob2); // equal to
};
/* Return the index of the element
specified by ch, or -1 if not found */
int Set::find(char ch){
int i;
for(i=0; i < len; i++)
if(members[i] == ch) return i;
return -1;
}
// Show the set
void Set::showset(){
cout << "{ ";
for(int i=0; i<len; i++)
cout << members[i] << ' ';
cout << "}\n";
}
/* Return true if ch is a member of the set.
Return false otherwise. */
bool Set::isMember(char ch){
if(find(ch) != -1) return true;
return false;
}
Set Set::operator+(char ch){
Set newset;
if(len == MaxSize){
cout << "Set is full.\n";
return *this; // return existing set
}
newset = *this; // duplicate existing set
if(find(ch) == -1){ // if not found
// add new element to newset
newset.members[newset.len] = ch;
newset.len++;
}
return newset;
}
// Remove an element from the set.
Set Set::operator-(char ch){
Set newset;
int i = find(ch); // i will be -1 if element not found
// copy and compress the remaining elements
for(int j=0; j < len; j++)
if(j != i) newset = newset + members[j];
return newset;
}
// set union
Set Set::operator+(Set ob2){
Set newset = *this; // copy the first set
// Add unique elements from second set to the first
for(int i=0; i <ob2.len; i++)
newset = newset + ob2.members[i];
return newset; // return updated set
}
// Set the difference
Set Set::operator-(Set ob2){
Set newset = *this; // copy the first set
// Subtract elements from second set
for(int i=0; i < ob2.len; i++)
newset = newset - members[i];
return newset; // return updated set
}
bool Set::operator<(Set ob2){
Set large, small, temp;
int check=0, n, m;
temp = *this;
//find which set is bigger/smaller, assign to large/small
large = ob2.len < temp.len ? temp : ob2;
small = ob2.len < temp.len ? ob2 : temp;
check = small.len;
if(temp.len>ob2.len) return false;
for(n=0; n<small.len; n++){
for(m=0; m<large.len; m++){
if(small.members[n]==large.members[m]) check--;
}
}
if(!check) return true;
return false;
}
bool Set::operator>(Set ob2){
Set large, small, temp;
int check=0, difference=0, f, g;
temp = *this;
large = temp.len > ob2.len ? *this : ob2;
small = temp.len < ob2.len ? *this : ob2;
check = large.len;
if(temp.len<ob2.len) return false;
for(f=0; f<large.len; f++){
for(g=0; g<small.len; g++){
if(large.members[f]==small.members[g]) check--;
}
}
difference = large.len - small.len;
if(check) check -= difference;
if(!check) return true;
else return false;
}
bool Set::operator==(Set ob2){
Set temp;
temp = *this;
for(int i=0; i<temp.len; i++){
if(temp.members[i]==ob2.members[i]) continue;
else break;
return true;
}
return false;
}
int main()
{
Set s1, s2;
s1 = s1 + 'A';
s1 = s1 + 'B';
s1 = s1 + 'C';
s2 = s2 + 'A';
s2 = s2 + 'B';
s2 = s2 + 'C';
s2 = s2 + 'D';
cout << "s1: ";
s1.showset();
cout << "s2: ";
s2.showset();
cout << "S1 IS A SUBSET OF S2, NOW TESTING > OPERATOR...\n\n";
//all if statements will return true when desired results reached
if(s1 < s2) cout << "Returned true, < operator works and s1 is a subset of s2.\n";
if(!(s1 > s2)) cout << "Returned false, > operator works and s1 is not a superset of s2.\n";
if(!(s2 < s1)) cout << "Returned false, < operator works and s2 is not a subset of s1.\n";
if(s2 > s1) cout << "Returned true, > operator works and s2 is a superset of s1.\n";
return 0;
}
|