一、设计一个整数集合类Set
1、数据成员:string name;//存放集合元素名称
int num;//集合中整数值
构造函数:Set(string s. int a)
成员函数:get函数
void disp(//输出集合所有元素
二、继承类SetFloat它是Set的派生类(子类)公有继承;数据成员:float f://集合中单精度浮点数
构造函数:
SetFloat (string s,int a,float m)//重载父类成员函数:getF和setFvoid disp ()//重载父类函数
3)成员函数:用成员重载的方式完成两个集合类中整数对象的相减
4)友元函数:用友员重载的方式完成两个集合类中浮点对象的相加运算
5)编写主函数进行测试三、编写主函数进行测试
求完整代码,谢谢啦
c++设计一个整数集合类Set
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
1条回答 默认 最新
- 五一编程 2022-07-07 11:32关注
Set.h
#include<iostream> #include<stdio.h> #include<algorithm> #ifndef SET_H_INCLUDED #define SET_H_INCLUDED using namespace std; class Set { private: double* set;//the pointer to double array int number;//the number of elements in array public: Set()//constructor with no args { set=NULL; number=0; } Set(const int number,const double* set)//constructor with tow args { if(number>0&&set!=NULL) { this->number=number; double tmp[number]; this->set=new double[number]; for(int i=0;i<number;i++) { this->set[i]=set[i]; } sort(this->set,this->set+number); }else if(number==0||set==NULL) { this->set=NULL; this->number=0; printf("An empty set will be created.\n"); }else if(number<0) { printf("%d must be greater than or equal to zero.\n An empty set will be created.\n",number); this->set=NULL; this->number=0; } } void print()const//the print function { for(int i=0;i<this->number;i++) { cout<<this->set[i]<<" "; } cout<<endl; } int getNumber()const//the function return the number of elements in array { return this->number; } void setSet(const int n,const double* s)//the setSet function { if(n>0&&s!=NULL) { this->number=n; this->set=new double[n]; for(int i=0;i<n;i++) { this->set[i]=s[i]; } sort(this->set,this->set+n); }else if(n==0||s==NULL) { this->number=0; this->set=NULL; printf("An empty set will be created.\n"); }else if(n<=0) { printf("%d must be greater than or equal to zero.\n An empty set will be created.\n",n); this->set=NULL; this->number=0; } } //tow friend functions overriding operator friend Set operator+(const Set a,const Set b); friend Set operator*(const Set a,const Set b); }; Set operator+(const Set a,const Set b) { int na=a.getNumber(); int nb=b.getNumber(); double tmp[a.getNumber()+b.getNumber()+10]; int i; for(i=0;i<na;i++) { tmp[i]=a.set[i]; } for(int j=0;j<nb;j++) { tmp[i+j]=b.set[j]; } sort(tmp,tmp+na+nb); double tmp_2[a.getNumber()+b.getNumber()+10]; i=0; for(int j=0;j<(na+nb);j++) { if(j==0) { tmp_2[i]=tmp[j]; i++; }else if(j>0&&tmp[j]!=tmp[j-1]) { tmp_2[i]=tmp[j]; i++; } } Set result(i,tmp_2); return result; } Set operator*(const Set a,const Set b) { double tmp[a.getNumber()+b.getNumber()+10]; int cnt=0; for(int i=0;i<a.getNumber();i++) { for(int j=0;j<b.getNumber();j++) { if(a.set[i]==b.set[j]) { tmp[cnt]=a.set[i]; cnt++; break; } } } Set result(cnt,tmp); return result; } #endif // SET_H_INCLUDED
main.cpp
#include <iostream> #include<stdio.h> #include"Set.h" using namespace std; int main() { //We test the class Set here. // 1. Declare two double arrays a and b that contain the following values // (in the given order). // array a = -6.2, -3.1, 0.0, 1.9, 5.2, 8.3, 9.5, 10.7, 11.0, 12.5 // array b = -8.1, -3.1, 0.0, 2.5, 5.8, 8.3, 9.5, 11.0 double a[]={-6.2,-3.1,0.0,1.9,5.2,8.3,9.5,10.7,11.0,12.5}; double b[]={-8.1,-3.1,0.0,2.5,5.8,8.3,9.5,11.0}; // 2. Calculate the size of a and b using the sizeof function. DO NOT assume // that array a has 10 elements and DO NOT assume b has 8 elements. int na=sizeof(a)/8; int nb=sizeof(b)/8; // 3. If na is the size of a and nb the size of b then create a Set object // named X using the constructor that takes NO parameters (this makes X // the empty set). Then use the setSet function for set X, passing it // na and a. This sets X to a set that contains the numbers in array a. // 4. Then print X. // 5. Create a second Set object named Y with the constructor that takes two // parameters. Pass nb abd b to the constructor. // 6. Then print Y. Set X(na,a); Set Y(nb,b); X.print(); Y.print(); // 7. Create a Set object named Z using the constructor that takes NO parameters // (this makes Z the empty set). // 8. Calculate the set union Z = X + Y and print Z. // 9. Calculate the set intersection Z = X * Y and print Z. Set Z; Z=X+Y; Z.print(); Z=X*Y; Z.print(); // 10. Attempt to create Set object U as follows. // Set U(-na,a); Set U(-na,a); // 11. Reset set Z as follows // Z.setSet(nb,NULL); Z.setSet(nb,NULL); //cout << "Hello world!" << endl; return 0; }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用
悬赏问题
- ¥15 PADS Logic 原理图
- ¥15 PADS Logic 图标
- ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
- ¥20 气象站点数据求取中~
- ¥15 如何获取APP内弹出的网址链接
- ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
- ¥50 STM32单片机传感器读取错误
- ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
- ¥15 机器人轨迹规划相关问题
- ¥15 word样式右侧翻页键消失