#include <iostream>
using namespace std;
struct Node
{ int value;
Node * next;
};
class IntSet
{
public:
IntSet ();
IntSet (const IntSet& s);
~IntSet ();
bool is_empty() const; //判断是否为空集。
int size() const; //获取元素个数。
bool is_element(int e) const; //判断e是否属于集合。
bool is_subset(const IntSet & s) const; //判断s是否包含于集合。
bool is_equal(const IntSet & s) const; //判断集合是否相等。
void display() const; //显示集合中的所有元素。
IntSet& insert(int e); //将e加入到集合中。
IntSet& remove(int e); //把e从集合中删除。
IntSet union2(const IntSet & s) const; //计算集合的并集。
IntSet intersection2(const IntSet & s) const; //计算集合的交集。
IntSet difference2(const IntSet & s) const; //计算集合的差。
private:
int count;
Node *head;
};
IntSet ::IntSet ()
{ count=0;
head=NULL;
clear(IntSets)
}
IntSet::IntSet(const IntSet &s)
{ head = NULL;
count = 0;
Node *p=s.head;
for (int i=0; i<s.count; i++)
{ insert(p->value);
p = p->next;
}
}
IntSet::~IntSet()
{ Node *p;
while (head!=NULL)
{ p = head;
head = head->next;
delete p;
}
count=0;
}
bool IntSet::is_empty() const{ return count==0?true:false; }
int IntSet::size() const{ return count; }
bool IntSet::is_element(int e) const{ for (Node *p=head;p!=NULL;p=p->next)
if(p->value == e) return true;
return false;
}
bool IntSet::is_subset(const IntSet& s) const
{
for (Node *p=s.head;p!=NULL;p=p->next)
if(!is_element(p->value)) return false;
return true;
}
bool IntSet::is_equal(const IntSet& s) const
{
if(count!=s.count)
return false;
else if(is_subset(s))
return true;
return false;
}
void IntSet::display() const
{
for (Node *p=head;p!=NULL;p=p->next)
cout << p->value << ' ';
cout<<"\n";
cout << endl;
}
IntSet& IntSet::insert(int e)
{ if(!is_element(e))
{ Node *p=new Node;
p->value = e;
p->next = head;
head = p;
count++;
}
return *this;
}
IntSet& IntSet::remove(int e)
{
if(is_element(e))
{
Node *p=head;
Node *w;
for(;p!=NULL;p=p->next)
{
if(p->value==e)
{
w->next=p->next;
delete p;
count--;
break;
}
w=p;
}
}
return *this;
}
IntSet IntSet::union2(const IntSet& s) const
{
IntSet temp;
temp=*this;
Node *p=s.head;
for(;p!=NULL;p=p->next)
{
if(!is_element(p->value))
temp.insert(p->value);
}
return temp;
}
IntSet IntSet::intersection2(const IntSet& s) const
{
IntSet temp;
Node *p=s.head;
for(;p!=NULL;p=p->next)
{
if(is_element(p->value))
temp.insert(p->value);
}
return temp;
}
IntSet IntSet::difference2(const IntSet& s) const
{
IntSet temp;
Node *p=head;
for(;p!=NULL;p=p->next)
{
if (!s.is_element(p->value))
temp.insert(p->value);
p = p->next; }
return temp;
}
void main()
{
IntSet Big;
int i;
for (i=1;i<=50000;i++)
Big.insert(i);
IntSet a;
if (a.is_empty()) cout <<"Empty"; else cout <<"Not empty";
cout <<" ";
a.insert(1);a.insert(2);a.insert(4);a.insert(5);a.insert(4);
if (a.is_empty()) cout <<"Empty"; else cout <<"Not empty";
cout <<endl;
cout <<a.size() <<endl;
if (a.is_element(3)) cout <<"Is element"; else cout <<"Not element";
cout <<endl;
IntSet b=a;
b.remove(4);
if (b.is_subset(a)) cout <<"Is subset"; else cout <<"Not subset";
cout <<endl;
b.insert(4);
if (b.is_equal(a)) cout <<"Equal"; else cout <<"Not equal";
cout <<endl;
if (b.is_subset(a)) cout <<"Is subset"; else cout <<"Not subset";
cout <<endl;
b.remove(1);b.remove(2);b.insert(10);
IntSet union2test;
union2test.insert(1);union2test.insert(2);union2test.insert(4);union2test.insert(5);union2test.insert(10);
if (union2test.is_equal(a.union2(b))) cout <<"OK"; else cout <<"NO";
cout <<endl;
IntSet inter2test;
inter2test.insert(4);inter2test.insert(5);
if (inter2test.is_equal(a.intersection2(b))) cout <<"OK"; else cout <<"NO";
cout <<endl;
IntSet diff2test;
diff2test.insert(1);diff2test.insert(2);
if (diff2test.is_equal(a.difference2(b))) cout <<"OK"; else cout <<"NO";
cout <<endl;
}
duoxiangshi.rar_多项式重载
版权申诉
74 浏览量
2022-09-14
21:00:03
上传
评论
收藏 847KB RAR 举报
weixin_42653672
- 粉丝: 93
- 资源: 1万+
最新资源
- Python 手写实现 iD3 决策树算法-根据信息增益公式.zip
- 411675952289057车联助手-小窗版(三星)3.5.1.apk
- 三种快速排序方法合并在一个文件中以便直接运行的Python代码示例
- 937712277954201实习5.word
- 2程序语言基础知识pdf1_1716337722703.jpeg
- 简单的Python示例,演示了如何使用TCP/IP协议进行基本的客户端和服务器通信
- 考试.sql
- keil2 + proteus + 8051.exe
- 1961ee27df03bd4595d28e24b00dde4e_744c805f7e4fb4d40fa3f695bfbab035_8(1).c
- mediapipe-0.9.0.1-cp37-cp37m-win-amd64.whl.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈