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 }
Implement MeraSet2 class similar to last assignment, Add following members:
operator =; // assignment operator, makes deep copy
Copy constructor
operator []; // implement subscript operator, to display individual element of a set.
#include <iostream>
#include <time.h>
using namespace std;
class MeraSet{
int *member, count;
public:
MeraSet(): count(0) {}
int Insert(int e){
if(count == 100){
cout<<"Set is full!\n";
return -1;
}
if(count == 0){
member = new int[1];
count++;
member[0] = e;
return 1;
}
for(int i = 0; i < count; i++){
if(member[i] == e) return -1;
}
member = (int*) realloc(member, ++count * sizeof(int));
member[count - 1] = e;
return 1;
}
int Remove(int e){
for(int j = 0; j < count; j++){
if(member[j] == e){
int *temp = new int[count];
for(int i = 0; i < count; i++) temp[i] = member[i];
member = (int*) realloc(member, --count * sizeof(int));
for(int i = 0, k = 0; i < count + 1; i++){
if(i == j) continue;
else{
member[k] = temp[i];
k++;
}
}
delete [] temp;
return 1;
}
}
return -1;
}
void Print(){
if(count == 0) cout<<"{}";
else{
cout<<"{";
for(int i = 0; i < count; i++) cout<<member[i]<<",";
cout<<"\b}";
}
cout<<endl;
}
MeraSet(const MeraSet &another){
this->count = another.count;
this->member = new int[this->count];
for(int i = 0; i < this->count; i++) this->member[i] = another.member[i];
}
};
class MeraSet2{
int *member, count;
public:
MeraSet2(): count(0) {}
int Insert(int e){
if(count == 100){
cout<<"Set is full!\n";
return -1;
}
if(count == 0){
member = new int[1];
count++;
member[0] = e;
return 1;
}
for(int i = 0; i < count; i++){
if(member[i] == e) return -1;
}
member = (int*) realloc(member, ++count * sizeof(int));
member[count - 1] = e;
return 1;
}
int Remove(int e){
for(int j = 0; j < count; j++){
if(member[j] == e){
int *temp = new int[count];
for(int i = 0; i < count; i++) temp[i] = member[i];
member = (int*) realloc(member, --count * sizeof(int));
for(int i = 0, k = 0; i < count + 1; i++){
if(i == j) continue;
else{
member[k] = temp[i];
k++;
}
}
delete [] temp;
return 1;
}
}
return -1;
}
void Print(){
if(count == 0) cout<<"{}";
else{
cout<<"{";
for(int i = 0; i < count; i++) cout<<member[i]<<",";
cout<<"\b}";
}
cout<<endl;
}
void operator=(const MeraSet2 &another){
cout<<"\nUsing assignment operator\n";
this->count = another.count;
this->member = new int[this->count];
for(int i = 0; i < this->count; i++) this->member[i] = another.member[i];
}
int operator[](int i){
if(i < count && i >= 0) return this->member[i];
}
MeraSet2(const MeraSet2 &another){
cout<<"\nUsing copy constructor\n";
this->count = another.count;
this->member = new int[this->count];
for(int i = 0; i < this->count; i++) this->member[i] = another.member[i];
}
};
int main(){
MeraSet2 A, B;
srand(time(NULL));
for(int i = 0; i < 10; i++) A.Insert(rand() % 99 + 1);
A.Print();
B = A;
B.Print();
MeraSet2 C = MeraSet2(A);
C.Print();
return 0;
}
Comments
Leave a comment