版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、.平时作业共 2 次平时作业 (1)定 、 并 表示由整型数元素 成的集合 型IntSet。需提供的操作至少 包括:构造函数析构函数拷 构造函数插入元素 除元素清空集合集合并集合交集合差集合 示 出集合 示 出的格式 元素 1,元素 2, , 空集的 出 。/*intset.h*/#ifndefINTSET_H#defineINTSET_HclassIntSetintcursize,maxsize;int*x;bool member( int t) const ;public :IntSet(intm = 100);/构造函数IntSet(const IntSet&); /拷贝构造函数IntS
2、et();/析构函数void insert(intt);/插入元素void remove( intt);/删除元素void clear();/清空集合void print();/集合显示输出IntSetsetunion( const IntSet &); /集合并IntSetsetdifference(constIntSet&); /集合差IntSetsetintsection(constIntSet&); /集合交; #endif./*intset.cpp*/#includestdafx.h#include#includeusingnamespace std;#includeintset.h
3、IntSet :IntSet(intm) if( m1) exit(1); cursize=0; x=new int maxsize= m; IntSet :IntSet() deletex; IntSet :IntSet(constIntSet & m)cursize= m.cursize; x=new int maxsize= m.maxsize;for( inti=0;icursize;i+) xi=m.xi;boolIntSet :member( intt ) constintl=0;intu=cursize-1;while (l=u)intm=(u+l)/2;if( t xm)l=m
4、+1;elsereturntrue ;returnfalse ;voidIntSet :insert(intt )if(member( t ) return ;if(cursize=maxsize) exit(1); xcursize+=t ;for ( inti=cursize-1;i0;i-) if(xixi-1) inttemp=xi; xi=xi-1;xi-1=temp; else break ;voidIntSet :remove( intt )intflag = 0;intpos;for ( inti = 0; i cursize; i+) if( t =xi) flag = 1;
5、 pos = i; if(flag = 0)cout 该集合中不存在 t 这个元素,删除失败。 endl; *temp = x;cursize-;x =new int cursize;for(intj = 0; j pos; j+) xj = tempj; for(inti = pos; i cursize; i+) xi = tempi+1; voidIntSet :clear()if(cursize=0) return ; x =new int maxsize; cursize =0;voidIntSet :print()cout 0) for( inti=0;icursi
6、ze;i+) cout xi;if(i!=cursize-1) cout ,; cout ;IntSetIntSet :setdifference(constIntSet & anotherset )IntSetr;for ( inti=0;icursize;i+)if (! anotherset .member(xi)r.insert(xi);returnr;IntSetIntSet :setunion(constIntSet & anotherset)IntSetr =anotherset;for ( inti=0;icursize;i+)if (! anotherset .member(
7、xi)r.insert(xi);returnr;IntSetIntSet :setintsection(constIntSet & anotherset )IntSetr;for ( inti=0;icursize;i+)if ( anotherset.member(xi)r.insert(xi);returnr;.平时作业 (2)第 1 题 . 定义 HugeInt类,计算并显示出5000 阶乘的值和它的位数。5000!的值是多少?测试示例主程序/*/*f5000.cpp*/*/#include #include using namespace std;#include hugeint.hi
8、nt main() HugeInt product =1; long N;cout N;/运行时输入5000for (long idx=1; idx=N;idx+) product = product*idx;cout endl N ! = product endl;return 0;/* hugeint.h */#includeconstintMAXLEN=200000;classHugeIntpublic :HugeInt();HugeInt( constint & iOperand);friendstd:ostream & operator (std:ostream & out, Hug
9、eInt &R);HugeInt operator *(HugeInt &R);HugeInt operator *(intR);intLen()returnm_len;private:intm_sign;/ 符号intm_len;/ 长度char m_numMAXLEN; / 存储空间;/* hugeint.cpp */#includestdafx.h#includehugeint.h#include#include#include#includeusingnamespace std;.HugeInt :HugeInt() memset(m_num,0,sizeof ( char )*MAX
10、LEN); m_sign=0; m_len=0; HugeInt :HugeInt(constint&ioperand )memset(m_num,0,sizeof ( char )*MAXLEN);if ( ioperand !=0)if ( ioperand 0)m_sign=1;elsem_sign=-1;inti=0,k=1;intabs_R=abs( ioperand );do i+; m_numi=abs_R%10; abs_R/=10; while (abs_R);m_len=i;else m_num1=0; m_len=1; m_sign=1; HugeInt HugeInt
11、: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;intlen1,len2;if ( this -m_len R.Len() muti1=this -m_num; muti2= R.m_num; len1= this -m_len;len2= R.m_len
12、; else muti1=R.m_num; muti2= this -m_num; len2= this -m_len; len1=R.m_len; inti=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+;returnResult;std:o
13、stream & operator(std:ostream&out , HugeInt&R)inti;if ( R.m_sign=-1)out - ;for (i= R.m_len;i!=0;i-) out R.m_numi+0;out std:endl;returnout ; .第 2 题 . 改进第一次作业中的 IntSet ,分别使用运算符 +、 * 、 - 和表示集合并、集合交、集合差和集合输出。 ( 必须上机验证 )/*intset.h*/#ifndefINTSET_H#defineINTSET_HclassIntSetintcursize,maxsize;int*x;boolmem
14、ber( int t) const ;public:IntSet(intm = 100);/构造函数IntSet(const IntSet&); /拷贝构造函数IntSet();/析构函数voidinsert(intt);/插入元素friendostream & operator( ostream &,constIntSet &);IntSetoperator-(constIntSet&); /集合差IntSetoperator+(constIntSet&); /集合并IntSetoperator*(constIntSet&); /集合交;#endif/*intset.cpp*/#includ
15、estdafx.h#include#includeusingnamespace std;#includeintset.hIntSet :IntSet(intm) if( m1) exit(1); cursize=0; x=new int maxsize= m; IntSet :IntSet() deletex; IntSet :IntSet(constIntSet & m)cursize= m.cursize; x=new int maxsize= m.maxsize;for( inti=0;icursize;i+) xi=m.xi;boolIntSet :member( intt ) con
16、stintl=0;intu=cursize-1;while (l=u)intm=(u+l)/2;if( t xm)l=m+1;elsereturntrue ;returnfalse ;.voidIntSet :insert(intt )if(member( t ) return ;if(cursize=maxsize) exit(1); xcursize+=t ;for ( inti=cursize-1;i0;i-) if(xixi-1) inttemp=xi; xi=xi-1;xi-1=temp; else break ;ostream & operator(ostream & os,constIntSet & is )cout 0)for ( inti=0;iis .cursize;i+)os is .xi;if(i!=is .cursize-1)os ,;cout ;returnos;IntSetIntSet :operator-(constIntSet & anotherset )IntSet r;for ( int i=0;icursize;i+)if (! anotherset .member(xi) r.insert(xi);return r;IntSetIntSet
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年2月闽江师范高等专科学校招聘编外心理咨询师兼心理教师1人(福建)考试备考题库及答案解析
- 成都市不动产登记中心2026年公开招聘编外聘用人员的(5人)考试参考试题及答案解析
- 2026贵州铜仁市德江县考调城区小学紧缺学科专任教师26人考试备考试题及答案解析
- 2026北京中关村发展集团股份有限公司中层管理人员岗位诚聘英才3人考试备考试题及答案解析
- 2026江西赣州市第三人民医院招募第一批青年见习46人考试备考试题及答案解析
- 2026四川成都市金牛区中医医院第一批次编外人员招聘17人考试备考题库及答案解析
- 2026西南证券股份有限公司招聘31人考试备考试题及答案解析
- 吉安市欣荣文化影视有限公司2026年招聘劳务派遣人员的考试参考试题及答案解析
- 2026甘肃兰州城市学院高层次人才引进60人(第一批)考试参考题库及答案解析
- 2026湖北咸宁市嘉鱼经济开发区国有公司招聘9人考试参考试题及答案解析
- 西门子PLC培训教学课件
- 2026年初中地理教研组工作计划
- 初中历史项目式学习与批判性思维发展课题报告教学研究课题报告
- 抖音本地生活服务商家直播数据复盘分析操作指南内附9个表格
- 绿色制造全套课件
- 2025年湖北省初中学业水平考试英语真题
- 第01讲 平面向量的概念及线性运算(六大题型)(练习)(原卷版)
- 再审被申请人意见书
- 创新医疗供应链管理模式提升医疗服务水平
- 防性侵课件主题班会
- 2025-2030中国保健醋行业市场运发展分析及竞争形势与投资战略研究报告
评论
0/150
提交评论