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}
AB={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}
AB={}
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;
}