//#include <iostream>
#include <iostream>
#include <vector>
using namespace std;
const int MAX_SIZE = 100;
class Lab5
{
private:
int setAry[MAX_SIZE];
int size;
public:
Lab5()
{
size = 0;
for (int index = 0; index < MAX_SIZE; index++)
{
setAry[index] = 0;
}
}
bool IsNotDuplicate(int e)
{
int i;
bool DuplicationStatus = true;
for (i = 0; i <= size; i++) {
if (e == setAry[i])
{
DuplicationStatus = false;
}
}
return DuplicationStatus;
}
bool addElement(int e)
{
if (IsNotDuplicate(e))
{
setAry[size] = e;
size++;
return true;
}
}
int getElement(int p)
{
if (p >= 0 && p < size)
return setAry[p];
else
return -1;
}
int getSize()
{
cout << "This set has " << size << " elements" ;
return size;
}
bool isSubset(Lab5 b)
{
int h = 0;
for (int x = 0; x < size; x++)
{
for (int y = 0; y < b.size; y++)
{
if (setAry[x] != b.setAry[y])
{
continue;
}
else if (setAry[x] == b.setAry[y])
{
h++;
break;
}
}
}
if (h == size)
{
return true;
}
else return false;
}
bool isProper(Lab5 &b)
{
int h = 0;
if (isSubset(b) == true)
{
for (int x = 0; x < size; x++)
{
for (int y = 0; y < b.size; y++)
{
if (b.setAry[y] == setAry[x])
h++;
}
}
}
if (h == b.size)
{
return false;
}
else return true;
}
bool doesExist(int a, Lab5& set2) {
bool does = false;
for (int x = 0; x < size; x++)
{
if (set2.setAry[x] == a)
does = true;
}
return does;
}
void printOrderedPairs(Lab5 &b)
{
for (int x = 0; x < size; x++)
{
for (int y = 0; y < b.size; y++)
{
cout << "( " << setAry[x] << ", " << b.setAry[y] << ")";
}
}
}
void intersection(Lab5 & set2) {
int d = 0;
int *ptr = new int[20];
for (int i = 0; i < size; i++)
{
if (doesExist(setAry[i], set2))
{
ptr[d] = setAry[i];
d++;
}
}
size = d;
for (int x = 0; x < size; x++)
setAry[x] = ptr[x];
}
void unionOps(Lab5 & set2) {
int d = 0;
int *ptr = new int[20];
for (int i = 0; i < size; i++)
{
if (!doesExist(set2.setAry[i], *this))
{
ptr[d] = set2.setAry[i];
d++;
}
}
for (int x = 0; x < d; x++)
{
this->addElement(ptr[x]);
ptr[x - size];
}
}
void selectionSort()
{
int i, j, min, temp;
for (i = 0; i < size - 1; i++) {
min = i;
for (j = i + 1; j < size; j++)
if (setAry[j] < setAry[min])
min = j;
temp = setAry[i];
setAry[i] = setAry[min];
setAry[min] = temp;
}
}
void printSet() {
int x;
cout << "{ ";
for (x = 0; x < size; x++)
cout << setAry[x] << " ,";
cout << "\b" << "}"<< endl;
}
};
int main() {
Lab5 setA, setB, setC;
int inputA, inputB,CounterA=0,CounterB=0 , ElementCouneterA =1 , ElementCouneterB=1;
bool success = false;
cout << " \n Enter positive numbers \n Enter 0 to go to next set" << endl;
cout << "\n----- First Set -----" << endl;
do
{
cout << "Enter Element #" << ElementCouneterA << ": ";
cin >> inputA;
while (inputA <= 0 && CounterA <= 0)
{
cout << " WRONG INPUT! (ONLY positive integers)" << endl;
cin >> inputA;
}
CounterA++;
if (CounterA > 0) {
success = setA.addElement(inputA);
success = setC.addElement(inputA);
}
if (success)
{
ElementCouneterA++;
}
else ElementCouneterA = ElementCouneterA;
} while (inputA>0);
cout << "\n----- Second Set -----"<<endl;
do
{
cout << " Enter Element #" << ElementCouneterB << ": ";
cin >> inputB;
while (inputB <= 0 && CounterB <= 0)
{
cout << " WRONG INPUT!!! ( ONLY positive integers )" << endl;
cin >> inputB;
}
CounterB++;
if (CounterB > 0) {
success = setB.addElement(inputB);
}
if (success)
{
ElementCouneterB++;
}
else ElementCouneterB = ElementCouneterB;
} while (inputB>0);
cout << endl << "---------------------" << endl;
cout << "The First set: " ;
setA.selectionSort();
setA.printSet();
cout << "The Second set: ";
setB.selectionSort();
setB.printSet();
cout << "set C is: " ;
setC.selectionSort();
setC.printSet();
setA.unionOps(setB);
cout << "Intersection set: ";
setC.selectionSort();
setC.printSet();
cout << "Union set: ";
setA.selectionSort();
setA.printSet();
setC.intersection(setB);
system("pause");
return 0;
}