Implement a class MeraSet, which abstracts a mathematical Set of integral numbers. Maximum members can be 100. 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.
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.
#include <iostream>
#include <time.h>
using namespace std;
class MeraSet{
int *items, number;
public:
MeraSet(): number(0) {}
int Insert(int e){
if(number == 100){
return -1;
}
if(number == 0){
items = new int[1];
number++;
items[0] = e;
return 1;
}
for(int i = 0; i < number; i++){
if(items[i] == e) return -1;
}
items = (int*) realloc(items, ++number * sizeof(int));
items[number - 1] = e;
return 1;
}
int Remove(int e){
for(int j = 0; j < number; j++){
if(items[j] == e){
int *arr = new int[number];
for(int i = 0; i < number; i++) arr[i] = items[i];
items = (int*) realloc(items, --number * sizeof(int));
for(int i = 0, k = 0; i < number + 1; i++){
if(i == j) continue;
else{
items[k] = arr[i];
k++;
}
}
delete [] arr;
return 1;
}
}
return -1;
}
void Print(){
if(number == 0) cout<<"{}";
else{
cout<<"{";
for(int i = 0; i < number; i++) cout<<items[i]<<",";
cout<<"\b}";
}
}
int cardinality(){
return number;
}
MeraSet operator+(const MeraSet &temp){
MeraSet result = *this;
for(int i = 0; i < temp.number; i++) result.Insert(temp.items[i]);
return result;
}
MeraSet operator-(const MeraSet &temp){
MeraSet result = *this;
for(int i = 0; i < temp.number; i++) result.Remove(temp.items[i]);
return result;
}
MeraSet(const MeraSet &temp){
this->number = temp.number;
this->items = new int[this->number];
for(int i = 0; i < this->number; i++) this->items[i] = temp.items[i];
}
};
class MeraSet3{
int *items, number;
public:
MeraSet3(): number(0) {}
int Insert(int e){
if(number == 0){
items = new int[1];
number++;
items[0] = e;
return 1;
}
for(int i = 0; i < number; i++){
if(items[i] == e) return -1;
}
items = (int*) realloc(items, ++number * sizeof(int));
items[number - 1] = e;
return 1;
}
int Remove(int e){
for(int j = 0; j < number; j++){
if(items[j] == e){
int *arr = new int[number];
for(int i = 0; i < number; i++) arr[i] = items[i];
items = (int*) realloc(items, --number * sizeof(int));
for(int i = 0, k = 0; i < number + 1; i++){
if(i == j) continue;
else{
items[k] = arr[i];
k++;
}
}
return 1;
}
}
return -1;
}
void Print(){
if(number == 0) cout<<"{}";
else{
cout<<"{";
for(int i = 0; i < number; i++) cout<<items[i]<<",";
cout<<"\b}";
}
}
int cardinality(){
return number;
}
MeraSet3 operator+(const MeraSet3 &temp){
MeraSet3 result = *this;
for(int i = 0; i < temp.number; i++) result.Insert(temp.items[i]);
return result;
}
MeraSet3 operator-(const MeraSet3 &temp){
MeraSet3 result = *this;
for(int i = 0; i < temp.number; i++) result.Remove(temp.items[i]);
return result;
}
MeraSet3(const MeraSet3 &temp){
this->number = temp.number;
this->items = new int[this->number];
for(int i = 0; i < this->number; i++) this->items[i] = temp.items[i];
}
};
int main(){
MeraSet A, B, C, E;
srand(time(NULL));
int max = rand() % 10;
for(int i = 0; i < max; i++) A.Insert(rand() % 6);
max = rand() % 10;
for(int i = 0; i < max; i++) B.Insert(rand() % 6);
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 < 200; i++){
E.Insert(i); //accepts a maximum of 100
D.Insert(i); //does not have a limit
}
cout<<"\nNumber of items in E: "<<E.cardinality();
cout<<"\nNumber of items in D: "<<D.cardinality();
return 0;
}
Comments
Leave a comment