Sei sulla pagina 1di 6

#include <iostream> // std::cout

#include <algorithm> // std::set_union, std::sort, std::set_intersection,


std::set_difference, std::set_symmetric_difference
#include <vector> // std::vector
using namespace std;

class set
{
public:
int x, seta[100], no, y, setb[100], no2, ch;

void set_a()
{
system("CLS");
cout << "number of elements in set A: ";
cin >> no;
cout << "Enter elements in set A:\n";
for (x = 0; x < no; x++)
{
cin >> seta[x];
}
}
void set_b()
{
system("CLS");
cout << "Number of elements in set B: ";
cin >> no2;
cout << "Enter elements in the set B:\n";
for (y = 0; y < no2; y++)
{
cin >> setb[y];
}
system("CLS");
}
void stored() //the sets being inputted are being stored
{
cout << "\n\nElements in set A: { ";
for (x = 0; x < no; x++) //for every loop, the number will be
displayed until x reached the maximum number of elements
{
cout << seta[x] << " ";
}
cout << "}";

cout << "\n\nElements in set B: { "; //second set


for (y = 0; y < no2; y++)
{
cout << setb[y] << " ";
}
cout << "}"<< endl;
menu();
}
void menu() //displays the main menu where the user can choose the following set
opearations
{
cout << "\n- * - * - * - * - *- * - * -";
cout << "\n OPERATIONS\n";
cout << "- * - * - * - * - *- * - * -\n";
cout << "[1] Union\n";
cout << "[2] Intersection\n";
cout << "[3] Difference\n";
cout << "[4] Symmetric Difference\n";
cout << "[5] Exit\n";
cout << "choice: ";
cin >> ch;
if (ch == 1)
{
set_union();
}
else if (ch == 2)
{
set_intersection();
}
else if (ch == 3)
{
set_diff();
}
else if (ch == 4)
{
set_symdiff();

}
else if (ch == 5)
{
exit();
}
else //if none of the choices is typed, it will go ask the user to type
again
{
system("CLS");
cout << "Wrong input! Try again!";
stored();
}
}
void set_union()
{
system("CLS");
cout << " - - - - - - - - - - - - - - - -\n";
cout << " U N I O N\n";

cout << " - - - - - - - - - - - - - - - -\n";

std::vector<int> v(100); // 0 0 0 0 0 0 0 0 0
0
std::vector<int>::iterator it;

std::sort(seta, seta + x); //sorts set A in ascending order


std::sort(setb, setb + y); //sorts set B in ascending order

it = std::set_union(seta, seta + x, setb, setb + y, v.begin());

v.resize(it - v.begin());

// print out content:


std::cout << "\nThe union of set A and set B has " << (v.size()) << "
elements:\n{";
for (it = v.begin(); it != v.end(); ++it)
std::cout << ' ' << *it;
std::cout << " }\n";
tryagain();
}
void set_intersection()
{
system("CLS");
cout << " - - - - - - - - - - - - - - - - - - - - - -\n";
cout << " I N T E R S E C T I O N\n";

cout << " - - - - - - - - - - - - - - - - - - - - - -\n";


std::vector<int> v(100); // 0 0 0 0 0 0 0 0 0
0
std::vector<int>::iterator it;

std::sort(seta, seta + x); //sorts set A in ascending order


std::sort(setb, setb + y); //sorts set B in ascending order

it = std::set_intersection(seta, seta + x, setb, setb + y, v.begin());

v.resize(it - v.begin());

// print out content:


std::cout << "The intersection of set A and set B has " << (v.size()) << "
elements:\n{";
for (it = v.begin(); it != v.end(); ++it)
std::cout << ' ' << *it;
std::cout << " }" << endl;
tryagain();
}
void set_diff()
{
system("CLS");
cout << " - - - - - - - - - - - - - - - - - - - -\n";
cout << " D I F F E R E N C E\n";

cout << " - - - - - - - - - - - - - - - - - - - -\n";


cout << " [1] A-B [2] B-A" << endl << "Choice: ";
cin >> ch;
if (ch == 1)
{
std::vector<int> v(100); // 0 0
0 0 0 0 0 0 0 0
std::vector<int>::iterator it;

std::sort(seta, seta + x); //sorts set A in


ascending order
std::sort(setb, setb + y); //sorts set B in ascending
order

it = std::set_difference(seta, seta + x, setb, setb +


y, v.begin());

v.resize(it - v.begin());

// print out content:


std::cout << "The difference has " << (v.size()) << "
elements:\n{";
for (it = v.begin(); it != v.end(); ++it)
std::cout << ' ' << *it;
std::cout << " }" << endl;
tryagain();
}

else if (ch == 2)
{
std::vector<int> v(100); // 0 0
0 0 0 0 0 0 0 0
std::vector<int>::iterator it;

std::sort(seta, seta + x); //sorts set A in


ascending order
std::sort(setb, setb + y); //sorts set B in ascending
order

it = std::set_difference(setb, setb + y, seta, seta +


x, v.begin());

v.resize(it - v.begin());

// print out content:

std::cout << "\nThe difference between set B and set A


has " << (v.size()) << " elements.\n{";
for (it = v.begin(); it != v.end(); ++it)
std::cout << ' ' << *it;
std::cout << " }" << endl;
tryagain();
}
}
void set_symdiff()
{
system("CLS");
cout << " - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n";
cout << " S Y M M E T R I C D I F F E R E N C E\n";

cout << " - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - -\n";


cout << " [1] A-B [2] B-A" << endl << "Choice: ";
cin >> ch;
if (ch == 1)
{
std::vector<int> v(100); // 0 0 0 0 0 0 0
0 0 0
std::vector<int>::iterator it;

std::sort(seta, seta + x); //sorts set A in ascending order


std::sort(setb, setb + y); //sorts set B in ascending order

it = std::set_symmetric_difference(seta, seta + x, setb, setb + y,


v.begin());

v.resize(it - v.begin());

// print out content:


std::cout << "The symmetric difference has " << (v.size()) << "
elements:\n{";
for (it = v.begin(); it != v.end(); ++it)
std::cout << ' ' << *it;
std::cout << " }" << endl;
tryagain();
}
else if (ch==2)
{
std::vector<int> v(100); // 0 0 0 0 0 0 0
0 0 0
std::vector<int>::iterator it;

std::sort(seta, seta + x); //sorts set A in ascending order


std::sort(setb, setb + y); //sorts set B in ascending order

it = std::set_symmetric_difference(setb, setb + y, seta, seta + x,


v.begin());

v.resize(it - v.begin());

// print out content:


std::cout << "The symmetric difference between set B and set A has "
<< (v.size()) << " elements: {";
for (it = v.begin(); it != v.end(); ++it)
std::cout << ' ' << *it;
std::cout << " }" << endl;
tryagain();
}
}
void tryagain()
{
cout << endl;
cout << "Another operation? [1] Yes [2] No" << endl;
cout << "choice: ";
cin >> ch;
if (ch == 1)
{
system("CLS");
stored();
}
else if (ch == 2)
{
exit();
}
else
{
cout << "Wrong input! Try Again!";
tryagain();
}
}
void exit()
{
system("CLS");
cout << "Thank you for using this program! Come back again!";
}

};
int main()
{
set object;
object.set_a();
object.set_b();
object.stored();
system("pause");
return 0;

Potrebbero piacerti anche