65,211
社区成员
发帖
与我相关
我的任务
分享#include <iostream>
using namespace std;
struct Node
{
int content;
Node *next;
};
class Set
{ Node *node;
Node *head;
public:
Set();
Set(const Set& s);
~Set();
bool is_empty() const
{
return (head==NULL);
}
bool is_element(int e) const
{
Node *q=head;
for(;q!=NULL;q=q->next)
if(q->content==e)
return true;
return false;
}
Set union2(const Set& s) const
{
Set bing(*this);
Node *p=s.head;
for(;p!=NULL;p=p->next)
if(!is_element(p->content))
bing.insert(p->content);
bing.display();
return bing;
}
Set difference(const Set& s) const
{
Set cha(*this);
Node *p=s.head;
for(;p!=NULL;p=p->next)
if(is_element(p->content))
cha.remove(p->content);
cha.display();
return cha;
}
};
void at(Set A,int element)
{
if(A.is_element(element))
cout<<"有该元素"<<endl;
else
cout<<"没有该元素"<<endl;
}
void kongji(Set A)
{
if(A.is_empty())
cout<<"是空集"<<endl;
else
cout<<"不是空集"<<endl;
}
int main()
{
Set A,B;
cout<<"请输入集合A中的元素,以-1结束:"<<endl;
creat(A);
cout<<"请输入集合B中的元素,以-1结束:"<<endl;
creat(B);
cout<<"请选择要做的操作:"<<endl;
cout<<"1.判断是否为空集"<<endl;
cout<<"3.判断某元素是否在集合中"<<endl;
cout<<"8.计算集合的并集"<<endl;
cout<<"10.计算集合的差"<<endl;
int pd;
cin>>pd;
switch(pd)
{ case 1: cout<<"A";
kongji(A);
cout<<"B";
kongji(B);
break;
case 3: cout<<"请输入该元素:";
int element;
cin>>element;
cout<<"集合A中";
at(A,element);
cout<<"集合B中";
at(B,element);
break;
case 8:A.union2(B);break;
case 10:cout<<"A-B为{";
A.difference(B);
cout<<"}"<<endl;
cout<<"B-A为{";
B.difference(A);
cout<<"}"<<endl;break;
default:cout<<"Input Error!"<<endl;
}
return 0;
}