Implement and test an Intset class. Each object of this class will represent a set of integers, storing the different elements as integers. Provide constructors and some operations, such as Empty(), IsEmpty(),, IsMemberOf(int), IsEqual(IntSet&), Add(int), Sub(int), Intersection(IntSet&), Merge(IntSet&) and Print(), and so on.
//本程序用VC-SP6编写,调试通过
#include<iostream>
#include<iomanip>
using namespace std;
const int size=20;
class intest
{
public:
void reset()
{
top=0;
}
void empty();
int isempty();
int ismemberof(int);
int isequal(intest &);
int add(int);
int sub(int);
intest intersection(intest &);
intest merge(intest &);
void print();
private:
int a[size];
int top;
};
void intest::empty()
{
top=0;
cout<<"the intest is empty."<<endl;
}
int intest::isempty()
{
if(top==0)
return 1;
else
return 0;
}
int intest::add(int element)
{ int i;
if(top>0)
{
for(i=0;i<=top-1;i++)
{
if(a[i]==element)
{
return 0;
break;
}
}
if(i==top&&top<=size)
{ a[i]=element;
top++;
return 1;
}
if(i==top&&top>size)
{ cout<<"the intest is full."<<endl;
return 0;
}
}
else
{
a[0]=element;
top++;
return 1;
}
}
int intest::ismemberof(int element)
{ int i;
for(i=0;i<=top;i++)
if(a[i]==element)
{ return 1;
break;
}
if(i==top+1)
return 0;
}
int intest::sub(int element)
{
int i;
for(i=0;i<=top-1;i++)
if(a[i]==element)
{
for(int j=i+1;j<=top;j++)
a[j-1]=a[j];
top--;
return 1;
}
if (i==top)
return 0;
}
void intest::print()
{
int i;
if(!isempty())
{cout<<"{";
for(i=0;i<=top-1;i++)
cout<<a[i]<<setw(3);
cout<<"}"<<endl;
}
else
cout<<"the intest is empty."<<endl;
}
int intest::isequal(intest &temp)
{ int i;
if(temp.top == top)
{
for( i = 0; i <= temp.top; i++)
{
if(!ismemberof(temp.a[i]))
return 0;
}
}
else
return 0;
return 1;
}
intest intest::intersection(intest &temp)
{
intest temper;
temper.reset();
for(int j=0; j<=temp.top-1; j++)
{
if(ismemberof(temp.a[j]))
{
temper.add(temp.a[j]);
}
}
return temper;
}
intest intest::merge(intest &temp)
{ //将两个集合合并,结果放入本集合中
int count = 0;
intest temper(temp);
for(int i=0; i<=temper.top-1;i++)
{
if(!ismemberof(temper.a[i]))
count++;
}
count += top;
if(count > 20)
{ //如果两集合并集超出了可以存储的范围,返回0
cout << "the merge of two intests too big" << endl;
return temp;
}
else
{
for(i = 0; i <=top-1; i++)
{
if(!temper.ismemberof(a[i]))
temper.add(a[i]);
}
return temper;
}
}
int main()
{
class intest number1,number2;
int type,element;
intest * temp_intest_1,* temp_intest_2;
number1.reset();
number2.reset();
cout<<"please choice a command for number1:"<<endl;
cout<<"***********************************"<<endl;
cout<<"1. empty the intest."<<endl;
cout<<"2. to judge an element is in the intest."<<endl;
cout<<"3. to judge the intest is empty."<<endl;
cout<<"4. add an element to the intest."<<endl;
cout<<"5. delete an element in the intest."<<endl;
cout<<"6. print the intest."<<endl;
cout<<"0. exit"<<endl;
cout<<"***********************************"<<endl;
cin>>type;
while (type!=0)
{
switch(type)
{
case 1: number1.empty();
break;
case 2:cout<<"please enter an element."<<endl;
cin>>element;
if(number1.ismemberof(element))
cout<<"yes,it is a member of the intest."<<endl;
else
cout<<"sorry,it is not in the intest."<<endl;
break;
case 3:if(number1.isempty())
cout<<"yes,it is empty."<<endl;
else
cout<<"no,it is not empty."<<endl;
break;
case 4:
cout<<"please enter an element.(only numbers)"<<endl;
cin>>element;
if(number1.add(element))
cout<<"add sucessfully."<<endl;
else
cout<<"the intest is full or the element is already in it."<<endl;
break;
case 5:
cout<<"please enter an element.(only numbers)"<<endl;
cin>>element;
if(number1.sub(element))
cout<<"delete sucessfully."<<endl;
else
cout<<"the element is not in the intest."<<endl;
break;
case 6:
number1.print();
break;
}
cin>>type;
}
cout<<"please choice a command for number2:"<<endl;
cout<<"***********************************"<<endl;
cout<<"1. empty the intest."<<endl;
cout<<"2. to judge an element is in the intest."<<endl;
cout<<"3. to judge the intest is empty."<<endl;
cout<<"4. add an element to the intest."<<endl;
cout<<"5. delete an element in the intest."<<endl;
cout<<"6. print the intest."<<endl;
cout<<"0. exit"<<endl;
cout<<"***********************************"<<endl;
cin>>type;
while (type!=0)
{
switch(type)
{
case 1: number2.empty();
break;
case 2:cout<<"please enter an element."<<endl;
cin>>element;
if(number2.ismemberof(element))
cout<<"yes,it is a member of the intest."<<endl;
else
cout<<"sorry,it is not in the intest."<<endl;
break;
case 3:if(number2.isempty())
cout<<"yes,it is empty."<<endl;
else
cout<<"no,it is not empty."<<endl;
break;
case 4:
cout<<"please enter an element.(only numbers)"<<endl;
cin>>element;
if(number2.add(element))
cout<<"add sucessfully."<<endl;
else
cout<<"the intest is full or the element is already in it."<<endl;
break;
case 5:
cout<<"please enter an element.(only numbers)"<<endl;
cin>>element;
if(number2.sub(element))
cout<<"delete sucessfully."<<endl;
else
cout<<"the element is not in the intest."<<endl;
break;
case 6:
number2.print();
break;
}
cin>>type;
}
cout<<"now,please input a command for the two intests:"<<endl;
cout<<"***********************************************"<<endl;
cout<<"1. merge the two intests."<<endl;
cout<<"2. intersection for the two intests."<<endl;
cout<<"3. whether the two intests are equal."<<endl;
cout<<"0. exit."<<endl;
cout<<"***********************************************"<<endl;
cin>>type;
while(type!=0)
{
switch(type)
{
case 1:
temp_intest_1 = new intest(number1.merge(number2));
(* temp_intest_1).print();
break;
case 2:
temp_intest_2 = new intest(number1.intersection(number2));
(* temp_intest_2).print();
break;
case 3:
if(number1.isequal(number2))
cout << "the two intests are equal" << endl;
else
cout << "the two intests are not equal" << endl;
break;
}
cin>>type;
}
return 0;
}