【C++运算符重载】集合类实现

description

定义一个集合类,集合的大小是不确定的,需要使用指针数据成员,
重载运算符“+”,“”,“-”实现集合的并、交、差运算,对赋值运算符进行重载,定义拷贝构造函数,重载流运算符实现集合的输入、输出。注意,假设A={1,3,5},B={1,5,9},两个集合的差运算A-B,是指从集合A中减去集合B的元素,上面集合A-B的结果是{3}
class settype
{
public:
settype() :n(0), set(NULL)
{}; //构造函数,默认空集,n=0, set=NULL;
settype(const settype& B); //拷贝构造函数
~settype();//析构函数
void setdata(int
a, int num); //设值函数
void getdata(int* a) const; //读值函数 int get_n() const; //获取集合当前元素数目
settype operator+(const settype& B) const; //重载运算符+,实现集合并运算
settype operator*(const settype& B) const; //重载运算符*,实现集合交运算
settype operator-(const settype& B) const; //重载运算符-,实现集合差运算
settype operator=(const settype& B); //重载运算符=
private:
int* set; //数组指针
int n; //元素的个数
};
int main() {
settype A, B, C;
cin >> A >> B;
cout << “A = ” << A;
cout << “B = ” << B;
C = A + B;
cout << “A + B = ” << C;
C = A*B;
cout << “AB=” << C;
C = A - B;
cout << “A - B = ” << C;
return 0;
}

Input

输入有4行, 第1行输入集合A 的元素个数,第2行输入A 的元素值(数据之间用空格分开),第3行输入集合
B的元素个数,第4行输入B 的元素值,注意集合A,B的元素个数不能超过10

Output

输出有5行,分别输出集合A,集合B,A+B的结果, AB的结果, A-B的结果,具体输出格式见Sample Output
Sample Input
5
1 3 5 7 9
3
1 4 6
Sample Output
A={1,3,5,7,9}
B={1,4,6}
A+B={1,3,5,7,9,4,6}
A
B={1}
A-B={3,5,7,9}

HINT

Input:
3
1 3 5
3
1 3 5
Output:
A={1,3,5}
B={1,3,5}
A+B={1,3,5}
AB={1,3,5}
A-B={}
Input:
3
1 3 5
2
2 4
Output:
A={1,3,5}
B={2,4}
A+B={1,3,5,2,4}
A
B={}
A-B={1,3,5}

课后重新写了一遍代码

#include <iostream>
using namespace std;
class settype
{
public:
	settype() :n(0), set(NULL)//构造函数,默认空集,n=0, set=NULL;
	{};
	settype(const settype& B); //拷贝构造函数
	~settype();//析构函数
	void setdata(int* a, int num); //设值函数
	void getdata(int* a) const; //读值函数 
	int get_n() const; //获取集合当前元素数目
	bool exist(int x) const;
	settype operator+(const settype& B) const; //重载运算符+,实现集合并运算
	settype operator*(const settype& B) const; //重载运算符*,实现集合交运算
	settype operator-(const settype& B) const; //重载运算符-,实现集合差运算
	settype operator=(const settype& B); //重载运算符= 
private:
	int* set; //数组指针
	int n; //元素的个数 
};
settype::settype(const settype& B)
{
	n = B.n;
	//delete[]set;不能delete拷贝构造函数创造并初始化一个新对象
	set = new int[n];
	for (int i = 0; i < n; i++)
		set[i] = B.set[i];

}
settype::~settype()
{
	delete[] set;
}
void settype::setdata(int* a, int num)
{
	n = num;
	delete[]set;
	set = new int[n];
	for (int i = 0; i < n; i++)
		set[i] = a[i];
}
void settype::getdata(int* a) const
{
	for (int i = 0; i < n; i++)
		a[i] = set[i];
}
int settype::get_n() const
{
	return n;
}
bool settype::exist(int x) const
{
	for (int i = 0; i < n; i++)
	{
		if (set[i] == x) return true;
	}
	return false;
}
settype settype::operator+(const settype& B) const
{
	settype temp;
	int* p_arr;
	int count = 0;//记录p_arr下标
	p_arr = new int[n + B.n];
	for (int i = 0; i < n; i++)
		p_arr[count++] = set[i];
	for (int i = 0; i < B.n; i++)
		if (!exist(B.set[i])) p_arr[count++] = B.set[i];
	temp.setdata(p_arr, count);
	delete[] p_arr;
	return temp;
}
settype settype::operator*(const settype& B) const
{
	settype temp;
	int* p_arr;
	p_arr = new int[n];
	int count = 0;
	for (int i = 0; i < n; i++)
		if (exist(B.set[i])) p_arr[count++] = set[i];
	temp.setdata(p_arr, count);
	delete[]p_arr;
	return temp;
}
settype settype::operator-(const settype& B) const
{
	settype temp;
	int* p_arr;
	p_arr = new int[n];
	int count = 0;
	for (int i = 0; i < n; i++)
		if (!exist(B.set[i])) p_arr[count++] = set[i];
	temp.setdata(p_arr, count);
	delete[]p_arr;
	return temp;
}
settype settype::operator=(const settype& B)
{
	if (this != &B)//不能是原来的
	{
		n = B.n;
		delete[]set;
		set = new int[n];
		for (int i = 0; i < n; i++)
			set[i] = B.set[i];
	}
	return *this;
}
istream& operator>>(istream& is, settype& obj)
{
	int num, * p_arr;
	is >> num;
	p_arr = new int[num];
	for (int i = 0; i < num; i++)
		is >> p_arr[i];
	obj.setdata(p_arr, num);
	delete[] p_arr;
	return is;
}
ostream& operator<<(ostream& os, const settype& obj)
{
	int num, * p_arr;
	num = obj.get_n();
	if (num == 0) os << "{}" << endl;
	else
	{
		p_arr = new int[num];
		obj.getdata(p_arr);
		os << "{";
		for (int i = 0; i < num - 1; i++)
			os << p_arr[i] << ",";
		os << p_arr[num - 1] << "}" << endl;
		delete[] p_arr;
	}
	return os;
}
int main() {
	settype A, B, C;
	cin >> A >> B;
	cout << "A=" << A;
	cout << "B=" << B;
	C = A + B;
	cout << "A+B=" << C;
	C = A*B;
	cout << "AB=" << C;
	C = A - B;
	cout << "A-B=" << C;
	return 0;
}
  • 9
    点赞
  • 47
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
好的,根据您的要求,我来实现一个重载运算符-的函数,用来求两个集合的差。 首先,我们需要明确集合的定义。集合是指元素不重复的一种数据结构,因此我们可以使用一个动态数组来表示集合,并通过类模板来实现。 我们可以将集合的差运算定义为:集合A与集合B的差,是由所有属于集合A但不属于集合B的元素组成的集合。因此,我们可以设计一个重载运算符-的函数模板,用来求两个集合的差。 以下是实现重载运算符-的函数模板: ```cpp template<typename T> Set<T> operator-(const Set<T>& A, const Set<T>& B) { Set<T> result; for (auto iter = A.begin(); iter != A.end(); ++iter) { if (!B.contains(*iter)) { result.insert(*iter); } } return result; } ``` 这个函数模板接受两个Set类型的引用参数A和B,返回一个Set类型的对象result,表示集合A与集合B的差。该函数的实现思路是:遍历集合A,对于集合A中的每一个元素,判断它是否属于集合B,如果不属于,则将其插入到result中。 在这个函数模板中,我们使用了Set类的成员函数contains和insert,分别用来判断元素是否属于集合和将元素插入到集合中。 接下来,我们可以使用以下代码来测试这个重载函数: ```cpp Set<int> A, B; A.insert(1); A.insert(2); A.insert(3); B.insert(2); B.insert(4); auto C = A - B; for (auto iter = C.begin(); iter != C.end(); ++iter) { cout << *iter << " "; // 输出1 3 } ``` 希望对您有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值