0% found this document useful (0 votes)
143 views6 pages

#Include #Include #Include Using Namespace Class Public Int Void

This C++ program defines a class called "set" to perform set operations on two integer sets A and B input by the user. The program displays a menu allowing the user to calculate the union, intersection, difference, or symmetric difference of the two sets. It uses standard algorithms like set_union, set_intersection, set_difference, and set_symmetric_difference along with vectors and iterators to perform the calculations and output the results.

Uploaded by

Wino Osorio
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
143 views6 pages

#Include #Include #Include Using Namespace Class Public Int Void

This C++ program defines a class called "set" to perform set operations on two integer sets A and B input by the user. The program displays a menu allowing the user to calculate the union, intersection, difference, or symmetric difference of the two sets. It uses standard algorithms like set_union, set_intersection, set_difference, and set_symmetric_difference along with vectors and iterators to perform the calculations and output the results.

Uploaded by

Wino Osorio
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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;

You might also like