Answer to Question #189396 in C++ for zain ul abdeen

Question #189396

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.


1
Expert's answer
2021-05-06T12:46:44-0400
 #include <iostream>


using namespace std;


class MeraSet
{
public:
	MeraSet();
	int Insert(int e);
	int Size() { return size; }
	int Remove(int e);
	void Print();
	MeraSet operator+(const MeraSet& rhs);
	MeraSet operator-(const MeraSet& rhs);
private:
	int GetElement(int index) const
	{
		int k = data[index];
		return k;
	}
	const int capacity = 100;
	int* data;
	int size;
};


MeraSet::MeraSet()
{
	data = new int[capacity];
	size = 0;
}


int MeraSet::Insert(int e)
{
	for (int i = 0; i < size; i++)
	{
		if (e == data[i]) return -1;
	}
	if (size == 100) return -1;
	data[size] = e;
	size++;
	return 1;
}


int MeraSet::Remove(int e)
{
	for (int i = 0; i < size; i++)
	{
		if (e == data[i])
		{
			for (int j = i; j < size-1; j++)
			{
				data[j] = data[j + 1];
			}
			size--;
			return 1;
		}
	}
	return -1;
}


void MeraSet::Print()
{
	cout << "{";
	for (int i = 0; i < size; i++)
	{
		if (i == size - 1)
		{
			cout << data[i] << "}" << endl;
			break;
		}
		cout << data[i] << ", ";
	}
}


MeraSet MeraSet::operator+(const MeraSet& rhs)
{
	MeraSet temp;
	for (int i = 0; i < this->size; i++)
	{
		temp.Insert(this->GetElement(i));
	}
	for (int i = 0; i < rhs.size; i++)
	{
		temp.Insert(rhs.GetElement(i));
	}
	return temp;
}


MeraSet MeraSet::operator-(const MeraSet& rhs)
{
	MeraSet temp;
	for (int i = 0; i < this->size; i++)
	{
		temp.Insert(this->GetElement(i));
	}
	for (int i = 0; i < rhs.size; i++)
	{
		temp.Remove(rhs.GetElement(i));
	}
	return temp;
}


int main()
{
	MeraSet MySet;
	MeraSet MySet2;
	for (int i = 0; i < 30; i++)
	{
		MySet.Insert(i);
	}
	cout << "Set = ";
	MySet.Print();
	cout << "Set size: " << MySet.Size() << endl;
	MySet.Remove(2);
	cout << "Set = ";
	MySet.Print();
	cout << "Set sizq: " << MySet.Size() << endl;
	for (int i = 50; i < 80; i++)
	{
		MySet.Insert(i);
	}
	MeraSet MySet3(MySet + MySet2);
	cout << "Set = ";
	MySet3.Print();
	for (int i = 0; i < 30; i++)
	{
		MySet2.Insert(i);
	}
	MeraSet MySet4(MySet3 - MySet2);
	cout << "Set = ";
	MySet4.Print();


	return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS