Question

In: Computer Science

Write a program that takes three sets ’A’, ’B’, ’C’ as input read from the file...

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.

Solutions

Expert Solution

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:


Related Solutions

Write a program that takes two sets ’A’ and ’B’ as input read from the file...
Write a program that takes two sets ’A’ and ’B’ as input read from the file prog1 input.txt. The first line of the file corresponds to the set ’A’ and the second line is the set ’B’. 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...
Write a C++ program that takes two sets ’A’ and ’B’ as input read from the...
Write a C++ program that takes two sets ’A’ and ’B’ as input read from the file prog1 input.txt. The first line of the file corresponds to the set ’A’ and the second line is the set ’B’. 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...
Write a C++ program that takes two sets ’A’ and ’B’ as input read from the...
Write a C++ program that takes two sets ’A’ and ’B’ as input read from the file prog1 input.txt. The first line of the file corresponds to the set ’A’ and the second line is the set ’B’. 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...
Done in C++, Write a program to read the input file, shown below and write out...
Done in C++, Write a program to read the input file, shown below and write out the output file shown below. Use only string objects and string functions to process the data. Do not use c-string functions or stringstream (or istringstream or ostringstream) class objects for your solution. Input File Cincinnati 27, Buffalo 24 Detroit 31, Cleveland 17 Kansas City 24, Oakland 7 Carolina 35, Minnesota 10 Pittsburgh 19, NY Jets 6 Philadelphia 31, Tampa Bay 20 Green Bay 19,...
JAVA Assignment: Project File Processing. Write a program that will read in from input file one...
JAVA Assignment: Project File Processing. Write a program that will read in from input file one line at a time until end of file and output the number of words in the line and the number of occurrences of each letter. Define a word to be any string of letters that is delimited at each end by either whitespace, a period, a comma or the beginning or end of the line. You can assume that the input consists entirely of...
Write a program that will read in from input file one line at a time until...
Write a program that will read in from input file one line at a time until end of file and output the number of words in the line and the number of occurrences of each letter. Define a word to be any string of letters that is delimited at each end by either whitespace, a period, a comma or the beginning or end of the line. You can assume that the input consists entirely of letters, whitespaces, commas and periods....
Write a C++ program that does the following: Read and input file containing the following PersonAName,...
Write a C++ program that does the following: Read and input file containing the following PersonAName, PersonBName, XA,YA, XB, YB where the coordinates of PersonA in a 100 by 100 room is XA, YA and the coordinates of PersonB is XB, YB. Use square root function in cmath sqrt() to calculate the shortest distance between two points. A file will be uploaded in this assignment that will list coordinates of two people. The program should use a function call that...
[In Python] Write a program that takes a .txt file as input. This .txt file contains...
[In Python] Write a program that takes a .txt file as input. This .txt file contains 10,000 points (i.e 10,000 lines) with three co-ordinates (x,y,z) each. From this input, use relevant libraries and compute the convex hull. Now, using all the points of the newly constructed convex hull, find the 50 points that are furthest away from each other, hence giving us an evenly distributed set of points.
Write a program that takes its input from a file of number type double and outputs...
Write a program that takes its input from a file of number type double and outputs the average of the numbers in the file to the screen. The file contains nothing but numbers of the type double separated by blanks and/ or line breaks. If this is being done as a class assignment, obtain the file name from your instructor. File name: pr01hw05input.txt 78.0 87.5 98.1 101.0 4.3 17.2 78.0 14.5 29.6 10.2 14.2 60.7 78.3 89.3 29.1 102.3 54.1...
C Programming file.c takes in one input argument that denotes a file to be read. It...
C Programming file.c takes in one input argument that denotes a file to be read. It needs to convert the contents of that file into a character array (char *) and then into a an unsigned character array (unsigned char *). Please fix and or complete the program, and explain any of the changes too: ---- #include <stdio.h> #include <stdlib.h> #include <string.h> int main(int argc, char *argv[]) { FILE *f; f = fopen(argv[1], "r"); if( !f ) { exit(1); }...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT