【C++运算符重载】集合类实现(不要直接copy)

Description

定义一个集合类,集合的大小是不确定的,需要使用指针数据成员, 重载运算符“+”,“*”,“-”实现集合的并、交、差运算,对赋值运算符进行重载,定义拷贝构造函数,重载流运算符实现集合的输入、输出。注意,假设A={1,3,5},B={1,5,9},两个集合的差运算A-B,是指从集合A中减去集合B的元素,上面集合A-B的结果是{3}

类的定义如下:

class settype 
{
public:
 settype();      //构造函数,默认空集,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;                    //元素的个数
 };

main函数如下:

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<<"A*B="<<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的结果, A*B的结果, 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}

A*B={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}

上代码!

// ConsoleApplication3.cpp : 定义控制台应用程序的入口点。
//

#include "iostream"
using namespace std;
class settype
{
public:
	settype();      //构造函数,默认空集,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;                    //元素的个数
};

settype::settype()
{
	n = 0;
	set = NULL;
}

settype::settype(const settype& B)
{
	n = B.n;
	//delete[] set;//我之前这里放进去,就错的离谱了。也不知道为啥
	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;
}

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;
}

settype settype::operator + (const settype& B) const
{
	settype temp;
	int co = 0;
	int *ppp;
	ppp = new int[n + B.n];

	for (int i = 0; i < n; i++)
		ppp[co++] = set[i];
	for (int i = 0; i < B.n; i++)
	{
		int flag = 0;
		for (int j = 0; j < n; j++)
		{
			if (ppp[j] == B.set[i]){ flag = 1; break; }
		}
		if (flag == 0)
			ppp[co++] = B.set[i];
	}temp.setdata(ppp, co);
		delete[]ppp;
		return temp;
}

settype settype::operator*(const settype& B) const
{
	settype temp;
	int co = 0;
	int *ppp;
	ppp = new int[B.n];

	for (int i = 0; i < B.n; i++)
	{
		int flag = 0;
		for (int j = 0; j < n;j++)
		if (B.set[i] == set[j]){ppp[co++]=set[i] ; break; }
	}
	temp.setdata(ppp, co);
	delete[]ppp;
	return temp;
}

settype settype::operator-(const settype& B) const
{
	settype temp;
	int co = 0;
	int *ppp;
	ppp = new int[n + B.n];

	for (int i = 0; i < n; i++)
	{
		int flag = 0;
		for (int j = 0; j < B.n; j++)
		{
			if (set[i] == B.set[j]){ flag = 1; break; }
		}
		if (flag == 0)ppp[co++] = set[i];
	}

	temp.setdata(ppp, co);
	delete[]ppp;
	return temp;
}

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 << "A*B=" << C;

	C = A - B;
	cout << "A-B=" << C;
	return 0;
}

个人感觉这道题融合了之前几乎所有教的知识点,刚上手的我写起来确实很吃力,我先看着书写了一遍,然后完全没有辅助的自己写了一遍,就很有成就感。代码这东西还是得多练,hhh。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值