码图 (多态)Set类(C++)

本文详细介绍了C++中Set类的实现,包括构造、析构、成员函数及运算符重载。通过实例展示了如何使用Set类进行集合操作,如元素添加、删除、集合并、交、差等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题 号: 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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值