Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Design and write a C++ class (Also please leave comments in the code to explain

ID: 667516 • Letter: D

Question


Design and write a C++ class (Also please leave comments in the code to explain what each code does) that implements a Set abstract data type and a menu-driven program that exercises the class. A typedef statement will identify the type of Items in the class's Sets. The program will manipulate sets of integers.
The program will maintain an array of three sets, identified by the integers 0, 1, and 2, and it will call class functions to carry out operations on the sets.
INPUT
The program will be interactive; it will respond to commands entered by the user.
OUTPUT
The program will prompt for inputs and will report the contents of sets in response to the user's commands.
ERRORS
The program should ignore non-command letters.
EXAMPLE
A run of the program might look like this:
    csci2> sets
    Enter a command according to this menu:
      e n        - Make set n empty.
      i v n      - Insert the value v in set n.
      r v n      - Remove the value v from set n.
      u n1 n2 n3 - Assign the union of sets n1 and n2 to set n3.
      s n        - Report the size of set n.
      p v n      - Is value v present in set n?
      w n        - Write the contents of set n to the terminal.
      h          - See this menu.
      q          - Quit.

    -> i 5 0
    -> i 8 0
    -> i 256 0
    -> p 0
    Set 0 = { 5, 8, 256 }
    -> r 8 0
    -> w 0
    Set 0 = { 5, 256 }
    -> w 2
    Set 2 = { }
    -> i 10 1
    -> i 5 1
    -> i 7 1
    -> w 1
    Set 1 = { 10, 5, 7 }
    -> u 0 1 2
    -> w 2
    Set 2 = { 5, 256, 10, 7 }
    -> s 2
    Set 2 contains 4 values.
    -> p 18 2
    Set 2 does not contain 18.
    -> q
   
OTHER REQUIREMENTS
In a class, implement a Set abstract data type whose values are sets of Items, with member functions corresponding to the operations listed in the example's menu:
A default constructor that initializes a newly declared Set to be empty.
A function that re-initializes a Set to be empty.
A function that inserts a value into a Set. If the Set already contains that value, the Set is unchanged.
A function that removes a value from a Set. If the Set does not initially contain the value, the Set is unchanged.
A function that forms the union of two Sets. It should be possible to assign that union to one of the original Sets.
A function that reports the size of a Set.
A function that reports if a value is present in a Set.
A function that writes the contents of a Set (in any order) to the terminal.
In the client program, use a constant to set the number of sets the program will manipulate.
HINTS
In each operation, consider the empty set.
The menu will be printed by a function in the client program.
Question: How might a function work that identifies the intersection of two Sets? If you like, you can include such a function in your class and exercise it in the program.

Explanation / Answer

#include <bits/stdc++.h>
using namespace std;

vector<int> v1;
vector<int> v2;
vector<int> v3;

int main(){
   while(true){
       string comm;
       cout << "-> ";
       getline(cin,comm);
       // quit
       if (comm == 'q')
           break;
      
       // Make set n empty.
       else if (comm[0] == 'e'){
           int n = comm[2] - '0';
           if (n == 1)           v1.clear();      
           else if (n == 2)   v2.clear();  
           else                v3.clear();  
       }

       // Insert the value v in set n.
       else if (comm[0] == 'i'){
           stringstream line(comm);
           string temp;
           vector<string> Token;
           while (line >> temp){                            // Split the a string with spaces and store in a vector Token.
               Token.push_back(temp);          
           }
           int num = atoi(Token[2].c_str());
           if (num == 1){
               v1.push_back(Token[1].c_str());
           }
           else if (num == 2){
               v2.push_back(Token[1].c_str());  
           }
           else{
               v3.push_back(Token[1].c_str());
           }
       }

       // Remove the value v from set n.
       else if (comm[0] == 'r'){
           stringstream line(comm);
           string temp;
           vector<string> Token;
           while (line >> temp){                            // Split the a string with spaces and store in a vector Token.
               Token.push_back(temp);          
           }
           int num = atoi(Token[2].c_str());
           int val = atoi(Token[1].c_str());
           if (num == 1){
               for (int i = 0; i < v1.size(); i++){
                   if (val == v1[i]){
                       v1.erase(v1.begin()+i)
                       break;
                   }
               }
           }
           else if (num == 2){
               for (int i = 0; i < v2.size(); i++){
                   if (val == v2[i]){
                       v2.erase(v2.begin()+i)
                       break;
                   }
               }
           }
           else{
               for (int i = 0; i < v3.size(); i++){
                   if (val == v3[i]){
                       v3.erase(v3.begin()+i)
                       break;
                   }  
               }
           }
       }  

       // Report the size of set n.
       else if (comm[0] == 's'){
           int n = comm[2] - '0';
           cout << "Set " << n << " contains ";
           if (n == 1) cout << v1.size() << " items";
           if (n == 2) cout << v2.size() << " items";
           if (n == 3) cout << v3.size() << " items";
       }

       // Write the contents of set n to the terminal.
       else if (comm[0] == 'w'){
           int n = comm[2] - '0';
           cout << "Set " << n << " {";
           if (n == 1){
               for (int i = 0; i < v1.size(); i++){
                   cout << v1[i] << ", ";
               }
               cout << " }" << endl;
           }
           else if (n == 2){
               for (int i = 0; i < v2.size(); i++){
                   cout << v2[i] << ", ";
               }
               cout << " }" << endl;
           }
           else if (n == 3){
               for (int i = 0; i < v3.size(); i++){
                   cout << v3[i] << ", ";
               }
               cout << " }" << endl;
           }
       }

       // - See this menu
       else if (comm[0] == 'h'){
           cout << "Enter a command according to this menu: " << endl;
           cout << "e n - Make set n empty. " << endl;
           cout << "i v n - Insert the value v in set n. " << endl;
           cout << "r v n - Remove the value v from set n. " << endl;
           cout << "u n1 n2 n3 - Assign the union of sets n1 and n2 to set n3. " << endl;
           cout << "s n - Report the size of set n." << endl;
           cout << "p v n - Is value v present in set n? " << endl;
           cout << "w n - Write the contents of set n to the terminal. " << endl;
           cout << " h - See this menu " << endl;
           cout << "q - Quit." << endl;
       }
   }
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote