编写一个程序,用于进行集合的并、差和交运算。例如:输入整数集合{9 5 4 3 6 7}和{2 4 6 9 },计算出它们进行集合的并、差和交运算后的结果。 【提示】 (1)可用以下表达式实现整数集合的基本运算: s1+s2 两个整数集合的并运算 s1-s2 两个整数集合的差运算 s1*s2 两个整数集合的交运算 (2)参考以下Set类的框架,用于完成集合基本运算所需的各项功能。 class set{ public: set::set(); void set::input(int d); int set::length(); int set::getd(int i); void set::disp(); set set::operator+(set s1); set set::operator-(set s1); set set::operator*(set s1); set set::operator=(set s1); protected: int len; int s[MAX]; };
时间: 2024-02-06 10:02:45 浏览: 19
好的,下面是代码实现:
```c++
#include <iostream>
using namespace std;
const int MAX = 100;
class set{
public:
set();
void input(int d);
int length();
int getd(int i);
void disp();
set operator+(set s1);
set operator-(set s1);
set operator*(set s1);
set operator=(set s1);
protected:
int len;
int s[MAX];
};
set::set(){
len = 0;
}
void set::input(int d){
s[len++] = d;
}
int set::length(){
return len;
}
int set::getd(int i){
return s[i];
}
void set::disp(){
for(int i = 0; i < len; i++){
cout << s[i] << " ";
}
cout << endl;
}
set set::operator+(set s1){
set s;
for(int i = 0; i < len; i++){
s.input(this->s[i]);
}
for(int i = 0; i < s1.length(); i++){
bool flag = true;
for(int j = 0; j < len; j++){
if(s1.getd(i) == this->s[j]){
flag = false;
break;
}
}
if(flag){
s.input(s1.getd(i));
}
}
return s;
}
set set::operator-(set s1){
set s;
for(int i = 0; i < len; i++){
bool flag = true;
for(int j = 0; j < s1.length(); j++){
if(this->s[i] == s1.getd(j)){
flag = false;
break;
}
}
if(flag){
s.input(this->s[i]);
}
}
return s;
}
set set::operator*(set s1){
set s;
for(int i = 0; i < len; i++){
for(int j = 0; j < s1.length(); j++){
if(this->s[i] == s1.getd(j)){
s.input(this->s[i]);
break;
}
}
}
return s;
}
set set::operator=(set s1){
this->len = s1.length();
for(int i = 0; i < len; i++){
this->s[i] = s1.getd(i);
}
return *this;
}
int main(){
set s1, s2;
s1.input(9);
s1.input(5);
s1.input(4);
s1.input(3);
s1.input(6);
s1.input(7);
s2.input(2);
s2.input(4);
s2.input(6);
s2.input(9);
cout << "set1:";
s1.disp();
cout << "set2:";
s2.disp();
cout << "set1 + set2:";
(s1 + s2).disp();
cout << "set1 - set2:";
(s1 - s2).disp();
cout << "set1 * set2:";
(s1 * s2).disp();
return 0;
}
```
输出结果为:
```
set1:9 5 4 3 6 7
set2:2 4 6 9
set1 + set2:9 5 4 3 6 7 2
set1 - set2:5 3 7
set1 * set2:9 4 6
```
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)