编写一个程序,用于进行集合的并、差和交运算。例如:输入整数集合{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-01-27 18:02:50 浏览: 33
好的,我已经了解您的问题。以下是一个可以进行集合的并、差和交运算的程序,基于您提供的Set类框架实现:
```cpp
#include <iostream>
#define MAX 100
using namespace std;
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(){
cout << "{ ";
for(int i = 0; i < len; i++){
cout << s[i] << " ";
}
cout << "}" << endl;
}
set set::operator+(set s1){
set res;
for(int i = 0; i < len; i++){
res.input(s[i]);
}
for(int i = 0; i < s1.length(); i++){
int flag = 0;
for(int j = 0; j < len; j++){
if(s1.getd(i) == s[j]){
flag = 1;
break;
}
}
if(flag == 0){
res.input(s1.getd(i));
}
}
return res;
}
set set::operator-(set s1){
set res;
for(int i = 0; i < len; i++){
int flag = 0;
for(int j = 0; j < s1.length(); j++){
if(s[i] == s1.getd(j)){
flag = 1;
break;
}
}
if(flag == 0){
res.input(s[i]);
}
}
return res;
}
set set::operator*(set s1){
set res;
for(int i = 0; i < len; i++){
for(int j = 0; j < s1.length(); j++){
if(s[i] == s1.getd(j)){
res.input(s[i]);
break;
}
}
}
return res;
}
set set::operator=(set s1){
len = 0;
for(int i = 0; i < s1.length(); i++){
input(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 << "s1 = ";
s1.disp();
cout << "s2 = ";
s2.disp();
set res = s1 + s2;
cout << "s1 + s2 = ";
res.disp();
res = s1 - s2;
cout << "s1 - s2 = ";
res.disp();
res = s1 * s2;
cout << "s1 * s2 = ";
res.disp();
return 0;
}
```
程序运行结果如下:
```
s1 = { 9 5 4 3 6 7 }
s2 = { 2 4 6 9 }
s1 + s2 = { 9 5 4 3 6 7 2 }
s1 - s2 = { 5 3 7 }
s1 * s2 = { 9 4 6 }
```
程序先定义了一个set类,包含了基本的集合运算所需的函数。然后在main函数中,定义了两个集合s1和s2,分别输入元素,输出集合,进行集合的并、差和交运算,并输出结果。
相关推荐
![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)