面向对象技术c++平时作业

上传人:小** 文档编号:89257266 上传时间:2019-05-22 格式:DOC 页数:7 大小:64.50KB
返回 下载 相关 举报
面向对象技术c++平时作业_第1页
第1页 / 共7页
面向对象技术c++平时作业_第2页
第2页 / 共7页
面向对象技术c++平时作业_第3页
第3页 / 共7页
面向对象技术c++平时作业_第4页
第4页 / 共7页
面向对象技术c++平时作业_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《面向对象技术c++平时作业》由会员分享,可在线阅读,更多相关《面向对象技术c++平时作业(7页珍藏版)》请在金锄头文库上搜索。

1、平时作业共2次平时作业(1)定义、实现并测试表示由整型数元素组成的集合类型IntSet。需提供的操作至少应包括:l 构造函数l 析构函数l 拷贝构造函数l 插入元素l 删除元素l 清空集合l 集合并l 集合交l 集合差l 集合显示输出集合显示输出的格式为元素1, 元素2,空集的输出为。/* intset.h */#ifndef INTSET_H#define INTSET_Hclass IntSet int cursize,maxsize; int *x; bool member(int t) const;public:IntSet(int m = 100);/l构造函数IntSet(cons

2、t IntSet&);/l拷贝构造函数IntSet();/l析构函数void insert(int t);/l插入元素void remove(int t);/l删除元素void clear();/l清空集合void print();/l集合显示输出IntSet setunion(const IntSet&);/l集合并IntSet setdifference(const IntSet&);/l集合差IntSet setintsection(const IntSet&);/l集合交;#endif/* intset.cpp */#include stdafx.h#include #include

3、using namespace std;#include intset.hIntSet:IntSet(int m) if (m1) exit(1); cursize=0; x=new intmaxsize=m; IntSet:IntSet() delete x; IntSet:IntSet(const IntSet& m)cursize=m.cursize; x=new intmaxsize=m.maxsize; for (int i=0;icursize;i+) xi=m.xi;bool IntSet:member(int t) constint l=0;int u=cursize-1;wh

4、ile (l=u)int m=(u+l)/2;if (txm)l=m+1;elsereturn true;return false;void IntSet:insert(int t)if (member(t) return; if (cursize=maxsize) exit(1); xcursize+=t;for (int i=cursize-1;i0;i-) if (xixi-1) int temp=xi; xi=xi-1; xi-1=temp; else break;void IntSet:remove(int t)int flag = 0;int pos;for (int i = 0;

5、 i cursize; i+) if (t=xi) flag = 1; pos = i; if (flag = 0)cout该集合中不存在t这个元素,删除失败。endl;elseint *temp = x;cursize-;x = new intcursize;for (int j = 0; j pos; j+) xj = tempj; for (int i = pos; i cursize; i+) xi = tempi+1; void IntSet:clear()if (cursize=0) return; x = new intmaxsize; cursize =0;void IntSe

6、t:print()cout 0) for (int i=0;icursize;i+) cout xi; if (i!=cursize-1) cout ,; cout ;IntSet IntSet:setdifference(const IntSet& anotherset)IntSet r;for (int i=0;icursize;i+)if(!anotherset.member(xi)r.insert(xi);return r;IntSet IntSet:setunion(const IntSet& anotherset)IntSet r = anotherset;for (int i=0

7、;icursize;i+)if(!anotherset.member(xi)r.insert(xi);return r;IntSet IntSet:setintsection(const IntSet& anotherset)IntSet r;for (int i=0;icursize;i+)if(anotherset.member(xi)r.insert(xi);return r; 平时作业(2)第1题.定义HugeInt类,计算并显示出5000阶乘的值和它的位数。5000!的值是多少?测试示例主程序/*/* f5000.cpp */*/#include #include using nam

8、espace std;#include hugeint.hint main() HugeInt product =1; long N; cout N; /运行时输入5000 for (long idx=1; idx=N;idx+) product = product*idx; cout endl N ! = product endl; return 0;/* hugeint.h */#include const int MAXLEN=200000;class HugeIntpublic:HugeInt();HugeInt(const int& iOperand);friend std:ostr

9、eam& operator (std:ostream& out,HugeInt &R);HugeInt operator *(HugeInt &R);HugeInt operator *(int R);int Len()return m_len;private:int m_sign; /符号int m_len; /长度char m_numMAXLEN; /存储空间;/* hugeint.cpp */#include stdafx.h#include hugeint.h#include #include #include #include using namespace std;HugeInt:

10、HugeInt() memset(m_num,0,sizeof(char)*MAXLEN); m_sign=0; m_len=0; HugeInt:HugeInt(const int &ioperand)memset(m_num,0,sizeof(char)*MAXLEN);if(ioperand!=0)if(ioperand0)m_sign=1;elsem_sign=-1;int i=0,k=1;int abs_R=abs(ioperand);do i+; m_numi=abs_R%10; abs_R/=10; while(abs_R);m_len=i;else m_num1=0; m_le

11、n=1; m_sign=1; HugeInt HugeInt:operator *(int R) HugeInt hInt=R; return (*this)*hInt; HugeInt HugeInt:operator *(HugeInt &R)HugeInt Result=0;Result.m_sign=this-m_sign*R.m_sign;char *muti1,*muti2,*result=Result.m_num;int len1,len2;if(this-m_lenR.Len() muti1=this-m_num; muti2=R.m_num; len1=this-m_len;

12、 len2=R.m_len; else muti1=R.m_num; muti2=this-m_num; len2=this-m_len; len1=R.m_len; int i=1,j=1,k=1,carry=0;while(j=len2)i=1;k=j;while(i=len1) resultk+=muti1i+*muti2j+carry; carry=resultk/10; resultk%=10; k+; if(carry!=0) resultk+=carry; Result.m_len=k; carry=0; else Result.m_len=k-1; j+;return Result;std:ostream& operator (std:ostream &out,HugeInt &R) int

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 商业/管理/HR > 管理学资料

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号