题目描述:
引入头文件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
++)
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;
bool operator ==(const Set
&s
)const;
Set
& operator +=(int e
);
Set
& operator -=(int 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();
重载操作符-=,删除集合中元素e,例如:
Set s
;
s
+=1,s
+=2;
s
.ShowElement();
s
-=1;
s
.showElement();
重载操作符<=,判断当前集合是否包于另一个集合,例如:
Set s1
,s2
,s3
;
s1
+=1; s2
+=1;s2
+=3; s3
+=2;
s1
<=s2
;
s3
<=s2
重载操作符==,判断集合是否相等,例如:
Set s1 s2
;
s1
== s2
;
s1
+=1;s2
+=2;
s1
==s2
;
重载操作符|,集合并,例如:
Set s1 s2
;
s1
+=1;s2
+=2;
s1
|s2
;
重载操作符&,集合交,例如:
Set s1 s2
;
s1
+=1;s2
+=2;s2
+=1;
s1
&s2
;
重载操作符-,集合差,例如:
Set s1 s2
;
s1
+=1;s1
+=3;s2
+=2;s2
+=1;
s1
-s2
;
解答:
#include<iostream>
#include"CSet.h"
bool Set
::operator <=(const Set
& s
)const
{
bool is_in
= false;
int i
= 1, j
;
for (i
= 1; i
<= n
; i
++)
{
is_in
= false;
for (j
= 1; j
<= s
.n
; j
++)
if (s
.pS
[j
] == pS
[i
])
{
is_in
= true; break;
}
if (!is_in
) return false;
}
return true;
}
bool Set
::operator ==(const Set
& s
)const
{
bool outcome
= false;
if (s
.n
!= n
) return outcome
;
outcome
= ((*this) <= s
);
return outcome
;
}
Set
& Set
::operator +=(int e
)
{
int i
; bool is_in
= false;
for (i
= 1; i
<= n
; i
++)
{
if (e
== pS
[i
])
{
is_in
= true; break;
}
}
if (!is_in
)
{
int* temp
= pS
;
n
++;
pS
= new int[n
+ 1];
for (i
= 1; i
< n
; i
++)
pS
[i
] = temp
[i
];
pS
[n
] = e
;
}
return *this;
}
Set
& Set
::operator -=(int e
)
{
int i
, pos
= 0;
for (i
= 1; i
<= n
; i
++)
{
if (pS
[i
] == e
)
{
pos
= i
; break;
}
}
if (pos
== 0) return *this;
for (i
= pos
; i
< n
; i
++)
pS
[i
] = pS
[i
+ 1];
n
--;
return *this;
}
Set Set
::operator |(const Set
& s
)const
{
int cnt
= 0;
int* temp
= new int[this->n
+ s
.n
+ 1];
for (int i
= 1; i
<= n
; i
++)
temp
[++cnt
] = pS
[i
];
int i
, j
; bool is_in
= false;
for (i
= 1; i
<= s
.n
; i
++)
{
is_in
= false;
for (j
= 1; j
<= n
; j
++)
if (s
.pS
[i
] == pS
[j
])
{
is_in
= true; break;
}
if (!is_in
) temp
[++cnt
] = s
.pS
[i
];
}
Set uni
;
uni
.n
= cnt
;
uni
.pS
= temp
;
return uni
;
}
Set Set
::operator &(const Set
& s
)const
{
int* temp
= new int[min(n
, s
.n
) + 1];
int cnt
= 0;
int i
, j
; bool is_in
= false;
for (i
= 1; i
<= s
.n
; i
++)
{
is_in
= false;
for (j
= 1; j
<= n
; j
++)
if (s
.pS
[i
] == pS
[j
]) { is_in
= true; break; }
if (is_in
) temp
[++cnt
] = s
.pS
[i
];
}
Set con
;
con
.n
= cnt
;
con
.pS
= temp
;
return con
;
}
Set Set
::operator -(const Set
& s
)const
{
int* temp
= new int[n
+ 1];
int i
, j
, cnt
= 0;
bool is_in
= false;
for (i
= 1; i
<= n
; i
++)
{
is_in
= false;
for (j
= 1; j
<= s
.n
; j
++)
if (s
.pS
[j
] == pS
[i
]) { is_in
= true; break; }
if (!is_in
) temp
[++cnt
] = pS
[i
];
}
Set dif
;
dif
.n
= cnt
;
dif
.pS
= temp
;
return dif
;
}