In: Computer Science
My visual stuido class
IntegerSet Assignment Due: 1:00p, Wednesday, 10/28/2020 Create a class to represent a Set of Integers called IntegerSet, to hold a integers in the range 0 <= x <= 100. Recall that a set cannot contain duplicates. Thus the set will be represented as an array of bool elements. If array element a[i] is true, then the set contains integer i. If array element a[j] is false, then the set does not contain integer j. Provide the following set of methods in your class: 1. A parameterless constructor, that initializes the set to the empty set – a set with no members – that is, a set whose array representation contains all false values. 2. A method IntegerSet Union(IntegerSet) that creates the set-theoretic union third set from two existing sets, one of which is the current object, the other of which is passed it – that is, an element of the third set’s array is set to true if the corresponding element is true in either or both of the existing sets, otherwise the element in the third set is set to false. 3. A method IntegerSet Intersection(IntegerSet) that creates the set-theoretic intersection third set from two existing sets – that is, an element of the third set’s array is set to true if the corresponding element is true in both of the existing sets, but false otherwise. 4. A method bool InsertElement(int) that inserts a new integer k into the set by setting a[k] to true. Return true if the element was inserted, return false if the element already existed. 5. A method bool DeleteElement(int) that deletes integer m from the set by setting a[m] to false. Return true if the element was found and deleted, return false if m was not found in the set. 6. A method string ToString() that returns a string representing the set as a sequence of the numbers present in the set, with a comma and a space between each number. Use “---” to represent an empty set. 7. A method bool IsEqualTo(IntegerSet) that determines whether two sets are equal. You are responsible for only the class. I will write a program that uses your class, to exercise your class’s capabilities and test it’s functionality.
Below is the code that is satisfying your requirement and also sample code in main for using the functions of class.
#include <bits/stdc++.h>
using namespace std;
#define fr(i, a, b) for (i = a; i < b; i++)
const int MAX = 101;
class IntegerSet
{
private:
bool num[MAX];
int size;
public:
IntegerSet(); //constructor
IntegerSet intersectionOfSets(IntegerSet sc1, IntegerSet
sc2);
IntegerSet unionOfSets(IntegerSet sc1, IntegerSet sc2);
void insertElement(int element);
void deleteElement(int element);
void toString(); //prints the set
bool isEqualTo(IntegerSet sc1, IntegerSet sc2); //function
determines whether two sets are equal
};
IntegerSet::IntegerSet() //creates empty set
{
int i;
fr(i, 0, MAX)
num[i] = 0;
size = 0;
}
IntegerSet IntegerSet::intersectionOfSets(IntegerSet sc1,
IntegerSet sc2)
{
IntegerSet sc3;
int i;
fr(i, 0, MAX) if (sc1.num[i] == 1 && sc2.num[i] == 1)
sc3.num[i] = 1;
return sc3;
}
IntegerSet IntegerSet::unionOfSets(IntegerSet sc1, IntegerSet
sc2)
{
IntegerSet sc3;
int i;
fr(i, 0, MAX) if (sc1.num[i] == 1 || sc2.num[i] == 1)
sc3.num[i] = 1;
return sc3;
}
void IntegerSet::insertElement(int element)
{
if (element < MAX && element >= 0)
num[element] = 1;
size++;
}
void IntegerSet::deleteElement(int element)
{
if (element < MAX && element >= 0)
num[element] = 0;
size--;
}
void IntegerSet::toString()
{
int i;
if (size == 0)
cout << "---";
else
fr(i, 0, MAX) if (num[i] == 1)
cout
<< i << ", ";
cout << "\n";
}
bool IntegerSet::isEqualTo(IntegerSet sc1, IntegerSet sc2)
{
int i;
fr(i, 0, MAX) if (sc1.num[i] == sc2.num[i]) continue;
else return false;
return true;
}
int main()
{
/*
Use the above and test your input like below
IntegerSet sett;
sett.insertElement(3);
sett.toString();
*/
return 0;
}