编写一个程序,用于进行集合的并、差和交运算。例如:输入整数集合{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-14 16:02:59 浏览: 21
好的,以下是你需要的程序:
```
#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(){
cout << "{";
for(int i=0; i<len; i++){
if(i != 0){
cout << " ";
}
cout << s[i];
}
cout << "}" << endl;
}
set set::operator+(set s1){
set s;
for(int i=0; i<len; i++){
s.input(getd(i));
}
for(int i=0; i<s1.length(); i++){
bool flag = true;
for(int j=0; j<len; j++){
if(s1.getd(i) == getd(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(getd(i) == s1.getd(j)){
flag = false;
break;
}
}
if(flag){
s.input(getd(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(getd(i) == s1.getd(j)){
s.input(getd(i));
break;
}
}
}
return s;
}
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;
int d;
cout << "请输入第一个集合,以-1结束:" << endl;
while(true){
cin >> d;
if(d == -1){
break;
}
s1.input(d);
}
cout << "请输入第二个集合,以-1结束:" << endl;
while(true){
cin >> d;
if(d == -1){
break;
}
s2.input(d);
}
cout << "第一个集合为:";
s1.disp();
cout << "第二个集合为:";
s2.disp();
set s3 = s1 + s2;
cout << "两个集合的并为:";
s3.disp();
s3 = s1 - s2;
cout << "两个集合的差为:";
s3.disp();
s3 = s1 * s2;
cout << "两个集合的交为:";
s3.disp();
return 0;
}
```
程序中使用了Set类的框架,实现了集合的并、差和交运算。输入两个集合后,程序会依次输出两个集合、它们的并、差和交的结果。
相关推荐
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)