题 号: 165 (多态)Set类(C++) 语言要求: C++
引入头文件CSet.h
,它的内容如下:
#include <iostream>
using namespace std;
class Set{
private:
int n;
int * pS; //集合元素
public:
Set(){n = 0;pS =NULL;}
Set(Set &s){
n = s.n;
if (n !=0)
{
pS= new int[n+1];
for (int i =1;i<=n;i++) //集合的下标从1开始,集合中不能有重复元素
pS[i] = s.pS[i];
}
}
~Set(){
if (pS)
{
delete []pS;
pS = NULL;
n =0;
}
}
void ShowElement()const{ //输出集合的元素
int temp = 0;
for(int i=1;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(pS[i] > pS[j])
{
temp = pS[i];
pS[i] = pS[j];
pS[j] = temp;
}
}
}
cout<<"{";
for(int i =1;i<n;i++)
cout <<pS[i]<<",";
if (IsEmpty())
cout<<"}"<<endl;
else cout<<pS[n]<<"}"<<endl;
}
bool IsEmpty()const{return n?false:true;} //判断集合是否为空
int size(){return n;}
bool IsElement(int e)const {
for (int i =1;i<=n;i++)
if (pS[i] ==e)
return true;
return false;
}
bool operator <=(const Set &s)const;//this <= s判断当前集合是否包于集合s
bool operator ==(const Set &s)const; //判断集合是否相等
Set & operator +=(int e); // 向集合中增减元素e
Set & operator -=(int e); //删除集合中的元素e
Set operator |(const Set &s)const; //集合并
Set operator &(const Set &s)const;//集合交
Set operator -(const Set &s)const; //集合差
};
完成Set
类,实现运算符的重载。
重载操作符+=
,向集合中增减元素e
,例如:
Set s;
s +=1;
s.ShowElement();//{1}
重载操作符-=
,删除集合中元素e
,例如:
Set s;
s +=1,s+=2;
s.ShowElement();//{1,2}
s -=1;
s.showElement();//{2}
重载操作符<=
,判断当前集合是否包于另一个集合,例如:
Set s1,s2,s3;
s1 +=1; s2+=1;s2+=3; s3+=2;
s1 <=s2;//true
s3 <=s2//false;
重载操作符==
,判断集合是否相等,例如:
Set s1 s2;
s1 == s2;//true
s1+=1;s2+=2;
s1 ==s2 ;//false;
重载操作符|
,集合并,例如:
Set s1 s2;
s1+=1;s2+=2;
s1|s2 ;//{1,2}
重载操作符&
,集合交,例如:
Set s1 s2;
s1+=1;s2+=2;s2+=1;
s1&s2 ;//{1}
重载操作符-
,集合差,例如:
Set s1 s2;
s1+=1;s1+=3;s2+=2;s2+=1;
s1-s2 ;//{3}
Code:cpp
#include <iostream>
#include "CSet.h"
using namespace std;
Set & Set:: operator +=(int e){
int n = this -> n;bool f = 0;
for(int i = 1; i <= n; ++i)if(this -> pS[i] == e)f = 1;
if(!f){
int *p = new int[n+2];
for(int i = 1; i <= n; ++i){
p[i] = this -> pS[i];
}
p[n+1] = e;
this -> pS = p;
this -> n++;
}
return *this;
}
Set & Set:: operator -=(int e){
int n = this -> n;bool f = 0;
for(int i = 1; i <= n; ++i)if(this -> pS[i] == e)f = 1;
if(f){
int k = 1;
int *p = new int[n];
for(int i = 1; i <= n; ++i){
if(this -> pS[i] == e)continue;
p[k++] = this -> pS[i];
}
this -> n--;
}
return *this;
}
bool Set::operator <=(const Set &s)const{
if(this -> n > s.n)return 0;
bool f = 0;
for(int i = 1; i <= this -> n; ++i){
f = 0;
int v = this -> pS[i];
for(int j = 1; j <= s.n; ++j){
if(v == s.pS[j])f = 1;
}
if(!f)return 0;
}
return 1;
}
bool Set::operator ==(const Set &s)const{
if(this -> n != s.n)return 0;
bool f = 0;
for(int i = 1; i <= this -> n; ++i){
f = 0;
int v = this -> pS[i];
for(int j = 1; j <= s.n; ++j){
if(v == s.pS[j])f = 1;
}
if(!f)return 0;
}
return 1;
}
Set Set::operator |(const Set &s)const{
int cnt = 0;
int *p = new int[this->n+1+s.n+1];
for(int i = 1; i <= this -> n; ++i){
p[++cnt] = this -> pS[i];
}
for(int i = 1; i <= s.n; ++i){
int v = s.pS[i];
bool f = 0;
for(int j = 1; j <= this -> n; ++j){
if(v == this -> pS[j]){
f = 1;break;
}
}
if(!f){
p[++cnt] = v;
}
}
Set s1;
s1.n = cnt;
s1.pS = p;
return s1;
}
Set Set::operator &(const Set &s)const{
int cnt = 0;
int *p = new int[this->n+1+s.n+1];
for(int i = 1; i <= s.n; ++i){
int v = s.pS[i];
for(int j = 1; j <= this -> n; ++j){
if(v == this -> pS[j])p[++cnt] = v;
}
}
Set s1;
s1.n = cnt;
s1.pS = p;
return s1;
}
Set Set::operator -(const Set &s)const{
int cnt = 0;
int *p = new int[this->n+1+s.n+1];
for(int i = 1; i <= this -> n; ++i){
int v = this -> pS[i];
bool f = 0;
for(int j = 1; j <= s.n; ++j){
if(v == s.pS[j])f = 1;
}
if(!f)p[++cnt] = v;
}
Set s1;
s1.n = cnt;
s1.pS = p;
return s1;
}