#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, [Link]());
[Link](it - [Link]());
// print out content:
std::cout << "\nThe union of set A and set B has " << ([Link]()) << "
elements:\n{";
for (it = [Link](); it != [Link](); ++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, [Link]());
[Link](it - [Link]());
// print out content:
std::cout << "The intersection of set A and set B has " << ([Link]()) << "
elements:\n{";
for (it = [Link](); it != [Link](); ++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, [Link]());
[Link](it - [Link]());
// print out content:
std::cout << "The difference has " << ([Link]()) << "
elements:\n{";
for (it = [Link](); it != [Link](); ++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, [Link]());
[Link](it - [Link]());
// print out content:
std::cout << "\nThe difference between set B and set A
has " << ([Link]()) << " elements.\n{";
for (it = [Link](); it != [Link](); ++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,
[Link]());
[Link](it - [Link]());
// print out content:
std::cout << "The symmetric difference has " << ([Link]()) << "
elements:\n{";
for (it = [Link](); it != [Link](); ++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,
[Link]());
[Link](it - [Link]());
// print out content:
std::cout << "The symmetric difference between set B and set A has "
<< ([Link]()) << " elements: {";
for (it = [Link](); it != [Link](); ++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();
[Link]();
system("pause");
return 0;