In: Computer Science
Write a program that takes three sets ’A’, ’B’, ’C’ as input read from the file prog2 input.txt. The first line of the file corresponds to the set ’A’, the second line is the set ’B’, and the third line is the set ’C’. Every element of each set is a character, and the characters are separated by space. Implement algorithms for the following operations on the sets. Each of these algorithms must be in separate methods or subroutines. The output should be written in the file prog2 output.txt and the standard output. Write a title before the output of each operation.
1. (A∪B)∪C.
2. A∪(B∩C).
3. (A∪B)∩(A∪C).
I am neww to C++ so please include COMMENTS.
THANK YOU IN ADVANCE.
Code:
#include <iostream>
#include <fstream>
#include <set>
#include <algorithm>
#include <string>
#include <sstream>
#include <vector>
using namespace std;
void operation1(set<char> A,set<char>
B,set<char> C,ofstream& fout)
{
std::set<char> result;
//First push all the result from A into the result set
and compare if there are any other elements in B which are not in
A
//If it is there then push that into result
for (char ch : A)
{
result.insert(ch);
}
for (char ch : B)
{
if(result.find(ch) ==
result.end())
{
result.insert(ch);
}
}
set<char> finalResult;
//first push all the elements from the result into
finalresult then check if there are any new elements in C
//If there are new elements then push it into
finalresult set
for (char ch : result)
{
finalResult.insert(ch);
}
for (char ch : C)
{
if (finalResult.find(ch) ==
finalResult.end())
{
finalResult.insert(ch);
}
}
cout << "\n(A U B) U C\n";
fout << "\n(A U B) U C\n";
for (char ch : finalResult)
{
cout << ch<<" ";
fout << ch<<" ";
}
}
void operation2(set<char> A, set<char>
B,set<char> C, ofstream& fout)
{
set<char> result;
//First perform the intersection between B and C
//This can be done by finding the common elements
between B and C
for (char ch : B)
{
//For each character check whether
it is present in other set
//if that is present then only
insert into the result set.
if(C.find(ch) != C.end())
{
result.insert(ch);
}
}
//Perform Union with the A
//check whether there are any new elements in A other
that result.
for (char ch : A)
{
if(result.find(ch) ==
result.end())
{
result.insert(ch);
}
}
cout << "\n\nA U (B ^ C)\n\n";
fout << "\n\nA U (B ^ C)\n\n";
for (char ch : result)
{
cout << ch<<" ";
fout << ch<<" ";
}
}
void operation3(set<char> A, set<char> B,
set<char> C, ofstream& fout)
{
std::set<char> result1;
std::set<char> result2;
//First push all the result from A into the result
set and compare if there are any other elements in B which are not
in A
//If it is there then push that into result
for (char ch : A)
{
result1.insert(ch);
}
for (char ch : B)
{
if (result1.find(ch) ==
result1.end())
{
result1.insert(ch);
}
}
//First push all the result from A into the result set
and compare if there are any other elements in C which are not in
A
//If it is there then push that into result
for (char ch : A)
{
result2.insert(ch);
}
for (char ch : C)
{
if (result2.find(ch) ==
result2.end())
{
result2.insert(ch);
}
}
set<char> finalResult;
//Then we have to find the common elements between
result1 and result2
for (char ch : result1)
{
if(result2.find(ch) !=
result2.end())
{
finalResult.insert(ch);
}
}
cout << "\n\n(A U B)^(A U C)\n\n";
fout << "\n\n(A U B) ^ (A U C)\n\n";
for (char ch : finalResult)
{
cout << ch << "
";
fout << ch << "
";
}
}
int main()
{
ifstream fin;
fin.open("input.txt");
ofstream fout;
fout.open("output.txt");
if (!fin.is_open())
return 0;
if (!fout.is_open())
return 0;
string str;
vector<set<char>> vset;
while (std::getline(fin, str))
{
char ch;
stringstream ss(str);
set<char> aSet;
while (ss >> ch)
{
aSet.insert(ch);
}
vset.push_back(aSet);
}
operation1(vset[0], vset[1], vset[2], fout);
operation2(vset[0], vset[1], vset[2], fout);
operation3(vset[0], vset[1], vset[2], fout);
return 0;
}
OUTPUT: