1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
|
Set::Set(const Set& s):pSize(s.pSize)
{ set_name(s.name);
numElements=s.numElements;
set= new int[s.pSize];
set_ptrset(s.set, s.pSize);
}
// In source file
Set::~Set()
{ if(set!=NULL)
{ delete []set;
set=NULL;
}
//cout<< "Hello!" << endl;
}
void Set::operator=(const Set& s)
{ delete []set;
set= new int[s.pSize];
name=s.name;
numElements=pSize=s.pSize;
for(int r=0; r<s.pSize; r++)
set[r]=s.set[r];
// In driver file
main()
{
char option;
Set *setptr;
setptr= new Set[1];
optionA(0, setptr);
do{
menu(option, setptr);
}while(option!='G');
}
void menu(char& option, Set &*setptr)
{ cout<< "Choose one of the option: " << endl;
cout<< "[A] Create a new set " << endl;
cout<< "[B] Delete an existing set " << endl;
cout<< "[C] Add an element to an "
<< "existing set " << endl;
cout<< "[D] Delete an element from an "
<< "existing set " << endl;
cout<< "[E] Operation (+,-,^) " << endl;
cout<< "[F] Display set " << endl;
cout<< "Any other letter to exit " << endl;
cin >> option;
do_menu(option, setptr);
cout<< endl;
}
void do_menu(char& option, Set &*setptr)
{ static int count=1;
switch(option)
{ case 'A':
optionA(count, setptr);
count++;
break;
case 'B':
optionB(setptr, count);
count--;
if(count<=0)
optionA(count, setptr);
count++;
break;
case 'C':
optionC(setptr, count);
break;
case 'D':
optionD(setptr, count);
break;
case 'E':
if(count>1)
{ optionE(count, setptr);
count++;
}
else
{ cout<< "Not enough sets!"
<< endl;
}
break;
case 'F':
optionF(setptr, count);
break;
default:
cout<< "Good Bye!" << endl;
option='G';
}
}
// Here is the important part:
void optionA(int count, Set &*setptr)
{ if(count>0)
{
Set *temp= new Set[count+1];
for(int a=0; a<count; a++)
temp[a]=setptr[a];
delete []setptr;
setptr= temp;
}
cin >> setptr[count];
}
|