(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-07 16:02:27 浏览: 31
这个Set类的框架中,有一些问题需要修改:
- 在第9行和第12行,重载乘号运算符和重载赋值运算符的符号都写错了,应该分别是`*`和`=`。
- 在第14行,应该是`int set::getd(int i)`,而不是`int set::get(int i)`。
修改后的Set类的框架如下:
```c++
class set{
public:
set::set(); // 构造函数
void set::input(int d); // 向集合中添加元素
int set::length(); // 获取集合长度
int set::getd(int i); // 获取集合中第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]; // 存储集合元素的数组
};
```
你可以根据自己的需要进行修改和扩展,添加其他的成员函数来实现集合的基本运算。
相关问题
编写一个程序,用于进行集合的并、差和交运算。例如:输入整数集合{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]; };
好的,我已经了解您的问题。以下是一个可以进行集合的并、差和交运算的程序,基于您提供的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,分别输入元素,输出集合,进行集合的并、差和交运算,并输出结果。
编写一个程序,用于进行集合的并、差和交运算。例如:输入整数集合{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]; };
好的,以下是一个简单的实现,使用了Set类的框架,可以实现集合的并、差、交运算:
```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) {
for(int i=0; i<len; i++) {
if(s[i] == d) {
return;
}
}
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 s;
for(int i=0; i<len; i++) {
s.input(getd(i));
}
for(int i=0; i<s1.length(); i++) {
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(s1.getd(j) == getd(i)) {
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(s1.getd(j) == getd(i)) {
s.input(s1.getd(j));
break;
}
}
}
return s;
}
Set Set::operator=(Set s1) {
len = s1.length();
for(int i=0; i<len; i++) {
s[i] = s1.getd(i);
}
return s1;
}
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);
Set s3 = s1 + s2;
Set s4 = s1 - s2;
Set s5 = s1 * s2;
cout << "S1: ";
s1.disp();
cout << "S2: ";
s2.disp();
cout << "S1 + S2: ";
s3.disp();
cout << "S1 - S2: ";
s4.disp();
cout << "S1 * S2: ";
s5.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 }
```
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![ppt](https://img-home.csdnimg.cn/images/20210720083527.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)