64,635
社区成员
发帖
与我相关
我的任务
分享
#include<iostream>
using namespace std;
const int N=100;
class Set {
int *elem; //存放集合元素的指针
int count; //存放集合中的元素个数
public:
Set();
Set(int s[],int n);
Set::Set(const Set&s);
~Set();
int find(int x)const; //判断X是否在集合中
Set operator+(const Set&); //集合的并集
Set operator-(const Set&); //集合的差集
Set operator*(const Set&); //集合的交集
Set &operator=(const Set&);
void disp(); //输出集合元素
};
Set::Set() { //默认构造
elem=NULL;
count=0;
}
Set::Set(int s[],int n) { //构造
int i;
elem=new int[n];
for(i=0; i<n;i++)
elem[i]=s[i];
count=n;
}
Set::Set(const Set&s) { //拷贝构造
int i;
if (s.count>0) {
elem=new int[s.count];
for(i=0;i<s.count;i++)
elem[i]=s.elem[i];
} else elem=NULL;
count=s.count;
}
Set::~Set() { //析构
if (count) {delete []elem;count=0;}
}
int Set::find(int x)const {
for(int i=0;i<count;i++) {
if(elem[i]==x)
return 1;
}
return 0;
}
Set Set::operator+(const Set&s) { //集合的并集
int a[N],k,i;
for(i=0;i<count;i++) { //先将当前集合元素放入a数组中
a[i]=elem[i];
}
k=count;
for(i=0;i<s.count;i++) //将集合s中不在当前集合中的元素放入a
if(find(s.elem[i]))
continue;
else {
if (k==N-1) break;
a[k++]=s.elem[i];
}
return Set(a,k); //调用构造函数产生一个无名对象返回
}
Set Set::operator-(const Set&s) { //集合的差集
int a[N],k=0,i;
for(i=0;i<count;i++) //将当前集合中不在s集合中的元素放入a数
if(!s.find(elem[i]))
a[k++]=elem[i];
return Set(a,k); //调用构造函数产生一个无名对象返回
}
Set Set::operator*(const Set &s) { //集合的交集
int a[N],k=0,i;
for(i=0;i<count;i++) //扫描当前集合中所有元素
if(s.find(elem[i]))
a[k++]=elem[i]; //将属于两个集合的元素放在a数组中
return Set(a,k); //调用构造函数产生一个无名对象返回
}
Set &Set::operator=(const Set &s) { //重载赋值运算符
int i;
if (&s != this ) {
if (s.count>0) {
if (count==0) elem=new int[s.count];
for(i=0;i<s.count;i++)
elem[i]=s.elem[i];
}
count=s.count;
}
return *this;
}
void Set::disp() { //输出集合元素
int i;
for(i=0;i<count;i++)
cout<<elem[i]<<" ";
cout<<endl;
}
int main() {
int a[]={1,2,3,4,5};
int b[]={1,3,5,7,9};
Set s1(a,5),s2(b,5),s3;
cout<<"集合s1:";
s1.disp();
cout<<"集合s2:";
s2.disp();
s3=s1+s2;
cout<<"并集:";
s3.disp();
s3=s1*s2;
cout<<"交集:";
s3.disp();
s3=s1-s2;
cout<<"差集:";
s3.disp();
return 0;
}
//集合s1:1 2 3 4 5
//集合s2:1 3 5 7 9
//并集:1 2 3 4 5 7 9
//交集:1 3 5
//差集:2 4
//
#include<iostream>
using namespace std;
const int N=100;
class Set {
int *elem; // 存放集合元素的指针
int count; // 存放集合中的元素个数
public:
Set();
Set(int s[],int n);
Set::Set(const Set&s);
~Set();
int find(int x)const; //判断X是否在集合中
Set operator+(const Set&); //集合的并集
Set operator-(const Set&); //集合的差集
Set operator*(const Set&); //集合的交集
Set &operator=(const Set&);
void disp(); //输出集合元素
};
Set::Set() {
elem=NULL;
count=0;
} // 默认构造
Set::Set(int s[],int n) { // 构造
int i;
elem=new int[n];
for(i=0; i<n;i++)
elem[i]=s[i];
count=n;
}
Set::Set(const Set&s) { // 拷贝构造
int i;
elem=new int[s.count];
for(i=0;i<s.count;i++)
elem[i]=s.elem[i];
count=s.count;
}
Set::~Set() { // 析构
if (count) {delete []elem;count=0;}
}
int Set::find(int x)const {
for(int i=0;i<count;i++) {
if(elem[i]==x)
return 1;
}
return 0;
}
Set Set::operator+(const Set&s) { // 集合的并集
int a[N],k,i;
for(i=0;i<count;i++) { // 先将当前集合元素放入a数组中
a[i]=elem[i];
}
k=count;
for(i=0;i<s.count;i++) // 将集合s中不在当前集合中的元素放入a
if(find(s.elem[i]))
continue;
else {
if (k==N-1) break;
a[k++]=s.elem[i];
}
return Set(a,k); // 调用构造函数产生一个无名对象返回
}
Set Set::operator-(const Set&s) { // 集合的差集
int a[N],k=0,i;
for(i=0;i<count;i++) // 将当前集合中不在s集合中的元素放入a数
if(!s.find(elem[i]))
a[k++]=elem[i];
return Set(a,k); // 调用构造函数产生一个无名对象返回
}
Set Set::operator*(const Set &s) { // 集合的交集
int a[N],k=0,i;
for(i=0;i<count;i++) // 扫描当前集合中所有元素
if(s.find(elem[i]))
a[k++]=elem[i]; // 将属于两个集合的元素放在a数组中
return Set(a,k); // 调用构造函数产生一个无名对象返回
}
Set &Set::operator=(const Set &s) {
int i;
if (&s != this ) {
for(i=0;i<s.count;i++)
elem[i]=s.elem[i];
count=s.count;
}
return *this;
}
void Set::disp() { // 输出集合元素
int i;
for(i=0;i<count;i++)
cout<<elem[i]<<" ";
cout<<endl;
}
int main() {
int a[]={1,2,3,4,5};
int b[]={1,3,5,7,9};
Set s1(a,5),s2(b,5);
cout<<"集合s1:";
s1.disp();
cout<<"集合s2:";
s2.disp();
Set s3=s1+s2;
cout<<"并集:";
s3.disp();
s3=s1*s2;
cout<<"交集:";
s3.disp();
s3=s1-s2;
cout<<"差集:";
s3.disp();
return 0;
}
//输出结果:
//集合s1:1 2 3 4 5
//集合s2:1 3 5 7 9
//并集:1 2 3 4 5 7 9
//交集:1 3 5
//差集:2 4