Implement a class MeraSet, which abstracts a mathematical Set of integral numbers. Maximum members can be 100. Implement following Member functions:
int Insert(int e) ; // adds value e to the set and returns 1, if the value already exists or the set has already 100 members, then it does not add and return -1.
int Size(); // return size of the set
int Remove(int e); // removes e from Set, and return 1. If e is not member of set, returns -1.
void Print(); // prints the members of the set in set notation e.g. Set = {1,2,3,8,-3 }
operator + ; // returns union of two sets
operator -; // returns difference set of two sets i.e. A-B
Make private member variables and more functions as you like.
Implement MeraSet3 class similar to last assignment. Make it unlimited, ie. use new and delete to extend or contract memory size of underlying array as required. Initially make it size 1, and grow it to two when you have the first insert, and so on. Similarly reduce size by 1 when a member element is removed.
#include <iostream>
using namespace std;
class MeraSet{
int *members, cardi;
public:
MeraSet(): cardi(0) {}
int Insert(int e){
if(cardi == 100){
cout<<"Set is full!\n";
return -1;
}
if(cardi == 0){
members = new int[1];
cardi++;
members[0] = e;
return 1;
}
for(int i = 0; i < cardi; i++){
if(members[i] == e) return -1;
}
members = (int*) realloc(members, ++cardi * sizeof(int));
members[cardi - 1] = e;
return 1;
}
int Remove(int e){
for(int j = 0; j < cardi; j++){
if(members[j] == e){
int *temp = new int[cardi];
for(int i = 0; i < cardi; i++) temp[i] = members[i];
members = (int*) realloc(members, --cardi * sizeof(int));
for(int i = 0, k = 0; i < cardi + 1; i++){
if(i == j) continue;
else{
members[k] = temp[i];
k++;
}
}
return 1;
}
}
return -1;
}
void Print(){
if(cardi == 0) cout<<"{}";
else{
cout<<"{";
for(int i = 0; i < cardi; i++) cout<<members[i]<<",";
cout<<"\b}";
}
}
MeraSet operator+(const MeraSet &other){
MeraSet result = *this;
for(int i = 0; i < other.cardi; i++) result.Insert(other.members[i]);
return result;
}
MeraSet operator-(const MeraSet &other){
MeraSet result = *this;
for(int i = 0; i < other.cardi; i++) result.Remove(other.members[i]);
return result;
}
MeraSet(const MeraSet &other){
this->cardi = other.cardi;
this->members = new int[this->cardi];
for(int i = 0; i < this->cardi; i++) this->members[i] = other.members[i];
}
};
class MeraSet3{
int *members, cardi;
public:
MeraSet3(): cardi(0) {}
int Insert(int e){
if(cardi == 0){
members = new int[1];
cardi++;
members[0] = e;
return 1;
}
for(int i = 0; i < cardi; i++){
if(members[i] == e) return -1;
}
members = (int*) realloc(members, ++cardi * sizeof(int));
members[cardi - 1] = e;
return 1;
}
int Remove(int e){
for(int j = 0; j < cardi; j++){
if(members[j] == e){
int *temp = new int[cardi];
for(int i = 0; i < cardi; i++) temp[i] = members[i];
members = (int*) realloc(members, --cardi * sizeof(int));
for(int i = 0, k = 0; i < cardi + 1; i++){
if(i == j) continue;
else{
members[k] = temp[i];
k++;
}
}
return 1;
}
}
return -1;
}
void Print(){
if(cardi == 0) cout<<"{}";
else{
cout<<"{";
for(int i = 0; i < cardi; i++) cout<<members[i]<<",";
cout<<"\b}";
}
}
MeraSet3 operator+(const MeraSet3 &other){
MeraSet3 result = *this;
for(int i = 0; i < other.cardi; i++) result.Insert(other.members[i]);
return result;
}
MeraSet3 operator-(const MeraSet3 &other){
MeraSet3 result = *this;
for(int i = 0; i < other.cardi; i++) result.Remove(other.members[i]);
return result;
}
MeraSet3(const MeraSet3 &other){
this->cardi = other.cardi;
this->members = new int[this->cardi];
for(int i = 0; i < this->cardi; i++) this->members[i] = other.members[i];
}
};
int main(){
MeraSet A, B, E;
A.Insert(1);
A.Insert(5);
B.Insert(1);
B.Insert(5);
MeraSet C = A + B;
A.Print(); cout<<" + "; B.Print(); cout<<" = "; C.Print(); cout<<endl;
C = A - B;
A.Print(); cout<<" - "; B.Print(); cout<<" = "; C.Print(); cout<<endl;
MeraSet3 D;
for(int i = 0; i < 101; i++) E.Insert(i); //Stops at 100
for(int i = 0; i < 200; i++) D.Insert(i); //Unlimited
return 0;
}
Comments
Leave a comment