#include <iostream>
#define SETNUMBER 3
using namespace std;
//类声明
class set
{
private:
bool charNumber[26]; //1代表有该字母,0代表没有
public:
set(void);
bool checkChar(char &);
bool addChar(char );
bool deleteChar(char);
void showChar(void)const;
void intersectionSet(const set &);
void unionSet(const set &);
void showComplementSet(void);
};
void showMenu(int x,char temp=NULL);
void updateSet(set *);
void operSet(int ,set *);
void operSetMenu(set *);
void clean(void);
//主函数
int main()
{
set test[SETNUMBER];
char choice;
showMenu(1);
while( cin>>choice )
{
if( choice=='1' )
{
system("cls");
char index='A';
for(int i=0;i<3;i++,index++)
{
cout<<"集合"<<index<<"包含的元素有: ";
test[i].showChar();
cout<<endl;
}
cout<<endl;
system("PAUSE");
}
else if( choice=='2' )
updateSet(test);
else if( choice=='3' )
operSetMenu(test);
else if( choice=='4' )
return 0;
else
{
clean();
system("cls");
cout<<"请正确输入!"<<endl;
system("PAUSE");
}
showMenu(1);
}
system("PAUSE");
return 0;
}
void clean(void)
{
char th;
cin.get(th);
while( th != '\n' )
{
cin.get(th);
continue;
}
}
void showMenu(int x,char temp)
{
if( x==1 )
{
system("cls");
cout<<"*****************************************"<<endl
<<"说明: "<<endl
<<"有三个集合可供操作,下列功能第一项对选定 "<<endl
<<"集合进行添加、删除元素,第二项功能对选定 "<<endl
<<"集合进行交并补的操作,每个选项都完成对输 "<<endl
<<"入的检验"<<endl
<<"*****************************************"<<endl;
cout<<endl;
cout<<"本系统有以下功能:"<<endl
<<" 1.显示所以集合包含的元素"<<endl
<<" 2.更新集合(添加/删除元素)"<<endl
<<" 3.对集合进行操作(交/并/补)"<<endl
<<" 4.退出"<<endl
<<"请输入(1-4): ";
}
if( x==2 )
{
system("cls");
cout<<"当前选择的集合为: "<<temp<<endl;
cout<<"更新集合的操作有以下几种,请选择:"<<endl
<<" 1.为集合"<<temp<<"添加元素: "<<endl
<<" 2.为集合"<<temp<<"删除元素: "<<endl
<<" 3.返回"<<endl
<<"请选择(1-3): ";
}
if( x==3 )
{
system("cls");
cout<<"对集合的操作有一下几种,请选择:"<<endl
<<" 1.交集"<<endl
<<" 2.并集"<<endl
<<" 3.补集"<<endl
<<" 4.返回"<<endl
<<"请选择(1-4): ";
}
}
void updateSet(set *setArray)
{
char mainchoice;
char subchoice;
set *operand = NULL;
system("cls");
cout<<"有A、B、C三个集合可供操作,选择Q返回主菜单"<<endl
<<"请选择(A,B,C,Q): ";
while( cin>>mainchoice )
{
system("cls");
if ( mainchoice == 'Q')
{
cout<<endl;
return;
}
else if( mainchoice!='A' & mainchoice!='B' & mainchoice!='C')
{
cout<<"请输入正确字符,大写字母A、B、C、Q!"<<endl;
cout<<"有A、B、C三个集合可供操作,选择Q返回主菜单"<<endl
<<"请选择(A,B,C,Q): ";
continue;
}
else
operand = &setArray[int(mainchoice)-65];
//选择集合后
showMenu(2,mainchoice);
while( cin>>subchoice )
{
if( subchoice=='1' )
{
clean();
char add;
cout<<endl<<"请输入添加字符(随意输入即可): ";
while( add = getchar() )
{
if(add == '\n')
break;
if(int(add) < 65 || int(add) > 90)
continue;
operand->addChar(add);
}
cout<<endl<<"集合"<<mainchoice<<"当前元素为: ";
operand->showChar();
}
else if( subchoice=='2' )
{
clean();
char dele;
cout<<endl<<"请输入删除字符(随意输入即可): ";
while( dele = getchar() )
{
if(dele == '\n')
break;
if(int(dele) < 65 || int(dele) > 90)
continue;
operand->deleteChar(dele);
}
cout<<endl<<"集合"<<mainchoice<<"当前元素为: ";
operand->showChar();
}
else if( subchoice=='3' )
{
clean();
cout<<endl;
break;
}
else
{
system("cls");
clean();
cout<<"请正确输入!"<<endl;
system("PAUSE");
showMenu(2,mainchoice);
continue;
}
cout<<endl<<endl;
system("PAUSE");
break;
}
system("cls");
cout<<"有A、B、C三个集合可供操作,选择Q返回主菜单"<<endl
<<"请选择(A,B,C,Q): ";
}
}
//集合操作函数
void operset(char temp,set *setArray)
{
set *operandone = NULL;
set *operandtwo = NULL;
char setone;
char settwo;
if( temp=='1' )
cout<<"请输入A、B、C中其中的两个集合进行交集运算(A,B,C)"<<endl;
else if( temp=='2' )
cout<<"请输入A、B、C中其中的两个集合进行并集运算(A,B,C)"<<endl;
cout<<"输入第一个集合: ";
while( cin>>setone )
{
clean();
if( setone!='A' & setone!='B' & setone!='C' )
cout<<"请重新输入第一个集合(A、B or C): ";
else
break;
}
operandone = &setArray[int(setone)-65];
cout<<"输入第二个集合: ";
while( cin>>settwo )
{
clean();
if( settwo!='A' & settwo!='B' & settwo!='C' )
cout<<"请重新输入第二个集合(A、B or C): ";
else
break;
}
operandtwo = &setArray[int(settwo)-65];
cout<<endl;
cout<<"集合"<<setone<<"的元素有: ";
operandone->showChar();
cout<<endl;
cout<<"集合"<<settwo<<"的元素有: ";
operandtwo->showChar();
cout<<endl;
if( temp=='1' )
{
cout<<endl<<"集合"<<setone<<"和集合"<<settwo<<"的交集为: ";
operandone->intersectionSet(*operandtwo);
}
else
{
cout<<endl<<"集合"<<setone<<"和集合"<<settwo<<"的并集为: ";
operandone->unionSet(*operandtwo);
}
cout<<endl;
}
//集合操作菜单
void operSetMenu(set *setArray)
{
char choice;
cout<<endl;
showMenu(3);
while( cin>>choice )
{
clean();
system("cls");
if( choice=='1' )
operset(choice,setArray);
else if( choice=='2' )
operset(choice,setArray);
else if( choice=='3' )
{
char ch;
cout<<"请输入A、B、C中其中的一个集合进行补集运算: ";
while( cin>>ch )
{
clean();
if( ch!='A' & ch!='B' & ch!='C' )
cout<<"请重新输入集合(A、B or C): ";
else
break;
}
set *operandone = &setArray[int(ch)-65];
cout<<endl<<"集合"<<ch<<"的补集为: ";
operandone->showComplementSet();
cout<<endl<<endl;
}
else if( choice=='4' )
{
cout<<endl;
break;
}
else
{
cout<<"请正确输入!";
}
cout<<endl;
system("PAUSE");
showMenu(3);
}
}
//默认构造函数
set::set(void)
{
for(int i = 0; i < 26; i++)
charNumber[i] = 0;
}
//检查字母
bool set::checkChar(char &ch)
{
if( int(ch)<65 | int(ch)>90 ) //检查大小写
return false;
else
return true;
}
//添加字母
bool set::addChar(char ch)
{
if( checkChar(ch) )
{
charNumber[int(ch)-65] = 1;
return true;
}
else
return false;
}
//删除字母
bool set::deleteChar(char ch)
{
if( checkChar(ch) )
{
charNumber[int(ch-65)] = 0;
return true;
}
else
return false;
}
//显示函数
void set::showChar(void)const
{
int num = 0;
for(int i = 0; i < 26; i++)
if( charNumber[i] )
cout << char(i+65);
else
num++;
if( num == 26 )
cout << "空集";
}
//交集函数
void set::intersectionSet(const set &set2)
{
int num = 0;
for(int i = 0; i < 26; i++)
if( charNumber[i] & set2.charNumber[i] )
cout << char(i+65);
else
num++;
if( num == 26 )
cout << "空集";
}
//并集函数
void set::unionSet(const set &set2)
{
int num = 0;
for(int i = 0; i < 26; i++)
if( charNumber[i] | set2.charNumber[i] )
cout << char(i+65);
else
num++;
if( num == 26 )
cout << "空集";
}
//补集函数
void set::showComplementSet(void)
{
int num = 0;
for(int i = 0; i < 26; i++)
if( charNumber[i] ^ 1 )
cout << char(i+65);
else
num++;
if( num == 26 )
cout << "空集";
}