Skip to content

Instantly share code, notes, and snippets.

@ebba0194
Created March 25, 2018 18:34
Show Gist options
  • Save ebba0194/afc16c7c56eb9658579e3fb666119ac8 to your computer and use it in GitHub Desktop.
Save ebba0194/afc16c7c56eb9658579e3fb666119ac8 to your computer and use it in GitHub Desktop.
/*Write a class IntSet for modeling sets of integers in the range 0 through 99. A set should be represented internally as an array of type bool: The ith array element will be true whenever integer i is in the set and will be false whenever integer i is not in the set. Include a no-argument constructor that initializes a set to the so-called “empty set,” i.e., a set whose array representation contains all false values.The program should allow for up to ten sets to be created during a given program run. Use any stand-alone functions you feel necessary.*/
#include <iostream>
using namespace std;
class IntSet
{
public:
IntSet();
bool set[99];
friend ostream& operator << (ostream& outputStream, IntSet& Object);
private:
};
IntSet::IntSet(){
cout << "I am a constructor that creates an empty (false) set." <<endl;
for (int i = 0; i < 99; i++){
set[i] = false;
}
}
const IntSet operator += (int, IntSet&); //add to set
const IntSet operator += (int num, IntSet& A){
A.set[num-1] = true;
}
const IntSet operator -= (int, IntSet&); //remove from set
const IntSet operator -= (int num, IntSet& Object){
Object.set[num-1] = false;
}
const IntSet operator + (IntSet, IntSet); //form the union of 2 sets
const IntSet operator + (IntSet A, IntSet B){
IntSet Result;
for (int i = 0; i < 99; i++){
if (A.set[i] == true || B.set[i] == true){
Result.set[i] == true;
}
}
return Result;
}
ostream& operator << (ostream& outputStream, IntSet& Object){
cout << "The set contains { ";
int trueCount=0;
for (int i = 0; i < 99; i++){
if (Object.set[i] == true){
cout << i+1 << ", ";
}
}
cout << " }" <<endl;
}
IntSet getObject (char);
int getOption();
int menuSwitch(int);
int main(){
IntSet A, B, C, E, F, G, H, I, J, K;
int option;
option = getOption();
while (option > 10 || option < 1){
cout << "ERROR: invalid selection.";
option = getOption();
}
int num;
char set, select;
switch(option){
case 1: //add a number to a set
cout << "Add numbers to which set?";
cin >> set;
while (select != 'n' && select != 'N'){
cout << "Enter number to add: ";
cin >> num;
num += A;
cout << "Continue? ";
cin >> select;
}
cout << A;
break;
case 2: // remove number from a set
while (select != 'n' && select != 'N'){
cout << "Enter number to remove: ";
cin >> num;
num -= A;
cout << "Continue? ";
cin >> select;
}
cout << A;
break;
case 3: // form the union of 2 sets
while (select != 'n' && select != 'N'){
cout << "Enter first set: ";
cin >> num;
cout << "Enter second set: ";
}
break;
case 4: // form the intersection of 2 sets
cout << "You picked 4!" <<endl;
break;
case 5: // form the difference of 2 sets
cout << "You picked 5!" <<endl;
break;
case 6: //determine if 2 sets are equal
cout << "You picked 6!" <<endl;
break;
case 7: // form the complement of a set
cout << "You picked 7!" <<endl;
break;
case 8: // determine if one set is a subset of another
cout << "You picked 8!" <<endl;
break;
case 9: // display a set
cout << "You picked 9!" <<endl;
break;
case 10: //exit
cout << "You picked 10!" <<endl;
break;
default:
break;
}
return 0;
}
int getOption(){
int option;
cout << "Select an option: " << endl
<< "1 - add numbers to a set" << endl
<< "2 - remove numbers from a set" << endl
<<"3 - form the union of two sets" << endl
<<"4 - form the intersection of two sets" << endl
<<"5 - form the difference of two sets" << endl
<<"6 - determine if two sets are equal" << endl
<<"7 - form the complement of an set" << endl
<<"8 - determine if one set is a subset of another set" << endl
<<"9 - display a set" << endl
<<"10 - Exit" << endl;
cin >> option;
return option;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment