源代码如下:
#include<iostream.h>
#include<string.h>
class jihe
{//类集合jihe的定义
private:
char *yuansu; //集合的元素值,以字符指针表示
int geshu; //集合中元素个数
bool biaoshi[26]; //标识A-Z相关元素的使用情况,false表未用,即无此元素,true表已用,即有此元素
char * chuli(char *ch)
{//处理
char *temp=new char[strlen(ch)];
int i,j=0;
for(i=0;i<strlen(ch);i++)
{
if(ch>='A' && ch<='Z' && biaoshi[ch-'A']==false)
{
temp[j]=ch;
j++;
biaoshi[ch-'A']=true;
}
}
temp[j]='\0';
return temp;
}
public:
jihe()
{//无参构造函数,实现初始化
yuansu='\0';
geshu=0;
for(int i=0;i<26;i++)
{
biaoshi=false;
}
}
jihe(char *ch)
{//有参构造函数,以一个字符指针指向的值作为参数,实现初始化
yuansu=chuli(ch);
geshu=strlen(yuansu);
for(int i=0;i<26;i++)
{
biaoshi=false;
}
}
void shuru()
{//输入集合中的全部元素,并去除不符合要求及重复的元素
char *t=new char[20];
cout<<"请输入一集合的所有元素('A'-'Z'):";
cin>>t;
yuansu=chuli(t);
geshu=strlen(yuansu);
}
char *ys()
{
return yuansu;
}
bool *bs()
{
return biaoshi;
}
int gs()
{
return geshu;
}
jihe* buji()
{//求补集,返回一个jihe对象
char *temp=new char[26];
int j=0;
for(int i=0;i<26;i++)
{
if(biaoshi==false)
{
temp[j]='A'+i;
j++;
}
}
temp[j]='\0';
return new jihe(temp);
}
jihe* jiaoji(jihe t)
{//求交集,返回一个jihe对象
char *temp=new char[26];
int j=0;
bool *bs=t.bs();
for(int i=0;i<26;i++)
{
if(biaoshi!=false && bs!=false)
{
temp[j]='A'+i;
j++;
}
}
temp[j]='\0';
return new jihe(temp);
}
jihe* bingji(jihe t)
{//求并集,返回一个jihe对象
char *temp=new char[26];
int j=0;
bool *bs=t.bs();
for(int i=0;i<26;i++)
{
if(biaoshi!=false || bs!=false)
{
temp[j]='A'+i;
j++;
}
}
temp[j]='\0';
return new jihe(temp);
}
void display()
{//输出显示集合中的元素
cout<<"{";
for(int i=0;i<geshu;i++)
{
cout<<yuansu;
if(i<geshu-1)cout<<" ";
}
cout<<"}"<<endl;
}
};
void main()
{
int i;
jihe jh[5],*temp;
int jhgeshu=0;
while(true)
{
cout<<"----------集合运算----------"<<endl;
cout<<"1.输入一集合的全部元素"<<endl;
cout<<"2.显示一集合的全部元素"<<endl;
cout<<"3.输出一给定集合的补集"<<endl;
cout<<"4.输出两给定集合的交集与并集"<<endl;
cout<<"0.退出"<<endl;
cout<<"请选择一选项(输入序号):";
cin>>i;
switch(i)
{
case 1:
jhgeshu++;
if(jhgeshu-1<5)
{
jh[jhgeshu-1].shuru();
}
else
{
cout<<"本程序已输入了5个集合,达到最大数\n";
}
break;
case 2:
if(jhgeshu>0)
{
int t;
cout<<"已输入的集合个数为:"<<jhgeshu<<endl;
cout<<"请输入要显示的集合序号(1-"<<jhgeshu<<"):";
cin>>t;
if(t>0 && t<=jhgeshu)
{
cout<<"第"<<t<<"个集合为:";
jh[t-1].display();
}
else
{
cout<<"输入超范围\n";
}
}
else
{
cout<<"还未有集合存在,请选择选项1输入一个集合\n";
}
break;
case 3:
if(jhgeshu>0)
{
int t;
cout<<"已输入的集合个数为:"<<jhgeshu<<endl;
cout<<"请输入要显示补集的集合序号(1-"<<jhgeshu<<"):";
cin>>t;
if(t>0 && t<=jhgeshu)
{
cout<<"第"<<t<<"个集合";
jh[t-1].display();
cout<<"补集为:";
//temp=jh[t-1].buji();
// temp->display();
jh[t-1].buji()->display ();
}
else
{
cout<<"输入超范围\n";
}
}
else
{
cout<<"还未有集合存在,请选择选项1输入一个集合\n";
}
break;
case 4:
if(jhgeshu>1)
{
int t1,t2;
cout<<"已输入的集合个数为:"<<jhgeshu<<endl;
cout<<"请输入要显示交集和并集的两个集合序号(1-"<<jhgeshu<<"):";
cin>>t1>>t2;
if(t1>0 && t1<=jhgeshu && t2>0 && t2<=jhgeshu)
{
cout<<"第"<<t1<<"和"<<t2<<"个集合为:\n";
jh[t1-1].display();
jh[t2-1].display();
cout<<"交集为:";
temp=jh[t1-1].jiaoji(jh[t2-1]);
temp->display();
cout<<"并集为:";
temp=jh[t1-1].bingji(jh[t2-1]);
temp->display();
}
else
{
cout<<"输入超范围\n";
}
}
else if(jhgeshu>0)
{
cout<<"集合个数不够,请再输入\n";
}
else
{
cout<<"还未有集合存在,请选择选项1输入一个集合\n";
}
break;
case 0:
return;
}
}
}