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...
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,...
[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...
Write a program in Java that will take as input two sets A and B, and...
Write a program in Java that will take as input two sets A and B, and returns a list of all functions from A to B.
Modify this program so that it takes in input from a TEXT FILE and outputs the...
Modify this program so that it takes in input from a TEXT FILE and outputs the results in a seperate OUTPUT FILE. (C programming)! Program works just need to modify it to take in input from a text file and output the results in an output file. ________________________________________________________________________________________________ #include <stdio.h> #include <string.h> // Maximum string size #define MAX_SIZE 1000 int countOccurrences(char * str, char * toSearch); int main() { char str[MAX_SIZE]; char toSearch[MAX_SIZE]; char ch; int count,len,a[26]={0},p[MAX_SIZE]={0},temp; int i,j; //Take...
In C++, write a program that accepts a text file of ASCII words from standard input...
In C++, write a program that accepts a text file of ASCII words from standard input and store them and the amount of times the word appears in the file in a hash table using external chaining. Then print the words and their counts sorted based on alphabetical order and print them again in decreasing numerical order based on the amount of times the word appears in the file. Space, tab, and new line all count as space characters. The...
Write a modularized, menu-driven program to read a file with unknown number of records. Input file...
Write a modularized, menu-driven program to read a file with unknown number of records. Input file has unknown number of records of inventory items, but no more than 100; one record per line in the following order: item ID, item name (one word), quantity on hand , and a price All fields in the input file are separated by a tab (‘\t’) or a blank ( up to you) No error checking of the data required Create a menu which...
Write a short C++ program that takes all the lines input to standard input and writes...
Write a short C++ program that takes all the lines input to standard input and writes them to standard output in reverse order. That is, each line is output in the correct order, but the ordering of the lines is reversed. Please use vector datatype standaard library #include <vector> Thanks
Write a c++ program that does the following, read temperatures from a file name temp.txt into...
Write a c++ program that does the following, read temperatures from a file name temp.txt into an array, and after reading all the temperatures, output the following information: the average temperature, the minimum temperature, and the total number of temperatures read. Thank you!
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT