填空题
以下程序通过重载运算符+、*实现集合(用数组表示)的并(u),交(n)运算。集的元素不能相同。两个集合的并包含了两个集合的所有元素。两个集合的交仅包含集合中共同存在的元素。设sl={1,2,3,4,5,6},s2={3,5,7,9,11}。s1 u s2={1,4,5,6,7,9,11},s1 n s2={3,5}。
[程序](4分)
#include
class Set{
float x[20];
int size;
public:
int In(float e,int n) //元素e已在集合x中,则返回1,否则返回0
{ int flag=0;
for(int i=o;i
return flag;
}
Set(float a[],int n)
{ x[0]=a[0];size=1;
for(int i=1;i
if(In(a[i],size)==0){
( 1(23) 2) ;
size++;
}
}
Set3{size=0;}
Set operator +(Set);
Set operator *(Set);
Set &operator =(Set&);
int GetSet(float y[])
{ for(int i=0;i
return size;
}
void print4
{ for(int i=0;i
cout<<'/n'<<"size=”<
};
}
Set Set::operator +(Set a)
{ Set tem;
for(int i=0;i 5(24) 6;
tem.size=size;
for(i=0;i
if(tem.In(a.x[i]i,tern.size)==0)tem.x[tem.size++]=a.x[i];
return tem;
}
Set Set::operator *(Set a)
{ Set tem;
tem.size=0;
for(int i=0;i
if(a.In(x[i],a.size)==1)
tem.x[tem.size++]=x[i];
retum tem;
} Set &Set::operator =(Set &a)
{for(int i=0;i
7(25) 8;
return *this;
}
void main(void)
{float b1[6]={1,2,3,4,5,6};
float b2[6]={3,5,7,9,11},b3[6];
Set a1(b1,6),a2(b2,5),a3,a4,a5;
a3=a1+a2;a3.print9;
a5=a1*a2;a5.print10;
int n=a1.GetSet(b3);
for(int i=0;i
cout<<'/n'<
}