
码图:165 (多态)Set类(C++)
发布日期:2021-05-08 03:17:45
浏览次数:18
分类:精选文章
本文共 3026 字,大约阅读时间需要 10 分钟。
题目描述:
引入头文件CSet.h,它的内容如下:
#includeusing namespace std;class Set{ private: int n; int * pS; //集合元素public: Set(){ n = 0;pS =NULL;} Set(Set &s){ n = s.n; if (n !=0) { pS= new int[n+1]; for (int i =1;i<=n;i++) //集合的下标从1开始,集合中不能有重复元素 pS[i] = s.pS[i]; } } ~Set(){ if (pS) { delete []pS; pS = NULL; n =0; } } void ShowElement()const{ //输出集合的元素 int temp = 0; for(int i=1;i pS[j]) { temp = pS[i]; pS[i] = pS[j]; pS[j] = temp; } } } cout<<"{"; for(int i =1;i
完成Set类,实现运算符的重载。
重载操作符+=,向集合中增减元素e,例如:Set s;s +=1;s.ShowElement();//{1}
重载操作符-=,删除集合中元素e,例如:
Set s;s +=1,s+=2;s.ShowElement();//{1,2}s -=1;s.showElement();//{2}
重载操作符<=,判断当前集合是否包于另一个集合,例如:
Set s1,s2,s3;s1 +=1; s2+=1;s2+=3; s3+=2;s1 <=s2;//trues3 <=s2//false;
重载操作符==,判断集合是否相等,例如:
Set s1 s2;s1 == s2;//trues1+=1;s2+=2;s1 ==s2 ;//false;
重载操作符|,集合并,例如:
Set s1 s2;s1+=1;s2+=2;s1|s2 ;//{1,2}
重载操作符&,集合交,例如:
Set s1 s2;s1+=1;s2+=2;s2+=1;s1&s2 ;//{1}
重载操作符-,集合差,例如:
Set s1 s2;s1+=1;s1+=3;s2+=2;s2+=1;s1-s2 ;//{3}
解答:
#include#include"CSet.h"bool Set::operator <=(const Set& s)const{ bool is_in = false; int i = 1, j; for (i = 1; i <= n; i++) { is_in = false; for (j = 1; j <= s.n; j++) if (s.pS[j] == pS[i]) { is_in = true; break; } if (!is_in) return false; } return true;}bool Set::operator ==(const Set& s)const{ bool outcome = false; if (s.n != n) return outcome; outcome = ((*this) <= s); return outcome;}Set& Set::operator +=(int e){ int i; bool is_in = false; for (i = 1; i <= n; i++) { if (e == pS[i]) { is_in = true; break; } } if (!is_in) { int* temp = pS; n++; pS = new int[n + 1]; for (i = 1; i < n; i++) pS[i] = temp[i]; pS[n] = e; } return *this;}Set& Set::operator -=(int e){ int i, pos = 0; for (i = 1; i <= n; i++) { if (pS[i] == e) { pos = i; break; } } if (pos == 0) return *this; for (i = pos; i < n; i++) pS[i] = pS[i + 1]; n--; return *this;}Set Set::operator |(const Set& s)const{ int cnt = 0; int* temp = new int[this->n + s.n + 1]; for (int i = 1; i <= n; i++) temp[++cnt] = pS[i]; int i, j; bool is_in = false; for (i = 1; i <= s.n; i++) { is_in = false; for (j = 1; j <= n; j++) if (s.pS[i] == pS[j]) { is_in = true; break; } if (!is_in) temp[++cnt] = s.pS[i]; } Set uni; uni.n = cnt; uni.pS = temp; return uni;}Set Set::operator &(const Set& s)const{ int* temp = new int[min(n, s.n) + 1]; int cnt = 0; int i, j; bool is_in = false; for (i = 1; i <= s.n; i++) { is_in = false; for (j = 1; j <= n; j++) if (s.pS[i] == pS[j]) { is_in = true; break; } if (is_in) temp[++cnt] = s.pS[i]; } Set con; con.n = cnt; con.pS = temp; return con;}Set Set::operator -(const Set& s)const{ int* temp = new int[n + 1]; int i, j, cnt = 0; bool is_in = false; for (i = 1; i <= n; i++) { is_in = false; for (j = 1; j <= s.n; j++) if (s.pS[j] == pS[i]) { is_in = true; break; } if (!is_in) temp[++cnt] = pS[i]; } Set dif; dif.n = cnt; dif.pS = temp; return dif;}
发表评论
最新留言
路过,博主的博客真漂亮。。
[***.116.15.85]2025年04月08日 22时47分28秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
LeetCode - 5. 最长回文子串——字符串、动态规划
2021-05-07
全局锁和表锁 :给表加个字段怎么有这么多阻碍?
2021-05-07
二分查找与插入排序的结合使用
2021-05-07
892 三维形体的表面积(分析)
2021-05-07
16 最接近的三数之和(排序、双指针)
2021-05-07
279 完全平方数(bfs)
2021-05-07
875 爱吃香蕉的珂珂(二分查找)
2021-05-07
桌面图标的自动排列图标
2021-05-07
第十一届蓝桥杯python组第二场省赛-数字三角形
2021-05-07
Jquery使用需要下载的文件
2021-05-07
BST中某一层的所有节点(宽度优先搜索)
2021-05-07
广度优先搜索
2021-05-07
Dijkstra算法的总结
2021-05-07
SpringCloud和SprinBoot之间的关系
2021-05-07
C语言的运算符和表达式
2021-05-07
Vue实现选项卡功能
2021-05-07
uni-app请求头中携带token
2021-05-07
vue中接收后台的图片验证码并显示
2021-05-07
Vue入门学习笔记(1)
2021-05-07