版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
平时作业共2次
平时作业(1)
定义、实现并测试表达由整型数元素构成的集合类型IntSet。
需提供的操作至少应包括:
•构造函数
•析构函数
•拷贝构造函数
•插入元素
•删除元素
•清空集合
•集合并
•集合交
•集合差
•集合显示输出
集合显示输山的格式为(元素I,元素2,.},空集的输山为(屋
/*intset.h
ttifndefINTSET_H
^defineINTSET_H
classIntSet{
intcursize,maxsize:int*x;boolmember(intl)const;
public:
IntSet(intm=100);/>构造函数
TntSet(constIntSet&);//拷贝构造函数
'intSet。;//析构函数
voidinsert(intt)://插入元素
voidremove(intt);//删除元素
voidclear();//清空集合
voidprint();//集合显示输出
IntSetsotunion(constIntSetft)集合并
IntSetsetdifference(constlntSet&);//集合差
IntSetsetintsection(constIntSet&)://集合交
};#endif
/*intsct.cpp*/
^include"stdafx.h"
#include<iostream>
include<cstdlib>
usingnamespacestd;
#include"intset.h"
IntSet::IntSet(intm){if(n<l)exit(1):cursize=O:x=newiit[maxsize=m];}
IntSet::^IntSet()(deletex;}
IntSet::IntSct(constlntSet&in)
(
cursize=m.cursize:x=newint[maxsize=in.maxsize];for(irti=0;i<cursize;i++){
x[i]=m.x[i];}
)
boolIntSet::member(intt)const
(
int1=0:intu=cursize-l;
while(K=u)
(
intm=(ui1)/2;
if(L<x[m])
u=m-l;
elseif(t>x[m])
l=m+l;
else
returntrue:
)
returnfalse;
)
voidIntSet::insert(intt)
(
if(member(t)){return;}if(cursize>=maxsize){exit(1):}x[cursize++]=t:
for(inti=cursize-l:i>0;i一){if(x[i]<x[i-l]){intlemp=x[i];x[i]=x[i-l];
x[i-l]=tcmp;}clse(break;})
)
voidIntSet::remove(intt)
(
intflag=0;intpos:
for(inti=0:i<cursize:i++)(if(t==x[i]){flaw=1:pos=i:}}
if(flag=0)
(
cout<<"该集合中不存在"<<l<<"这个元素,删除失败。"<<endl;
}else
int*tcmp=x;
cursize—;
x=newint[cursize];
for(intj=0;j<pos;j++){x[j]=}
for(inti=pos;i<cursize;i++){x[i]=temp[i+l];}}
1
voidIntSet::clcar()
(
if(cursize<=0){return;)x=newint(maxsize];cursize=0;
)
voidIntSet::print0
{
cout«~r;if(cursize>0)(for(inti=0;i<cursizc;i++){cout«x[i];if(i!=cursizc-l)
{cout<〈',';}}}cout«);
)
IntSetIntSet::sctdiffercnce\constIntSetfi.anothorsct)
(
IntSetr;
for(inti-0;i<cursizc;i++)
if(Janotherset.member(x[i]))
r.insert(x[i]);
returnr;
1
IntSetIntSet::setunion(constInlSet&anothersei)
(
IntSetr=anotherset;
for(inti=0;i<cursize;i++)
if(!anothorsct.member(x[i]))
r.insert(x[i]);
returnr;
)
IntSetIntSet::setintsection(constIntSet&anotherset)
{
IntSetr;
for(inti=0:i<cursize:i++)
if(af)otl)ers('I.member(x[i]))
r.insert(x[i]);
returnr:
}
平时作业(2)
第1题.定义Hugelnt类,计算并显示出5000阶乘的值和它的,,立数。5000!的值是多少?
测试示例主程序
/*f5000.cpp*/
/★★★*★★★★★★*************★****★**★*★*★★***★******★*★*******/
#include<iostream>
#include<iomanip>
usingnamespacestd;
#include"hugeint.h"
intmain()
{Hugelntproduct=1;
longN;
cout«nentern:u;
cin»N;〃运行时输入5000
for(longidx=l;idx<=N;idx++)product=product*idx;
cout«endl«N«"!=u«product«endl;
return0;
)
/*hugeint.h*/
^include<iostream>
constintMAXLEN=OO;
classHugelnt
{
public:
Hugelnt();
Hugelnt(constint&iOperand);
friendstd::oslieani&operator«(std::ostream&out,Hugelnt&R);
Hugelntoperator*(Hugelnt&R);
Hugelntoperator*(intR):
intLen(){returnmlen;}
private:
intm.sign;〃符号
intm_len;〃长度
charm_num[MAXLEN]:〃存储空间
};
/*hugeint.cpp*/
include*stdafx.h*
ffinclude"hugeint.h"
ftinclude<iostream>
^include<memory>
ftinclude<cmath>
^include<cstring>
usingnamespacestd:
HugeInt::HugeInt()(memset(m_num,0,sizeof(char)*MAXLEN);m_sign=0:m_len=0:}
HugeInt::Hugelnt(constintoperand)
(
memset(mnum,0,sizeof(char)*MAXLEN);
if(ioperand!=0)
(
if(ioperrind>0)
m_sign=l;
else
msign=-l;
inti=0,k=l;
intabs_R=abs(iopei'cnd):
do{i++;mnum[i]=tbsR%10;absR/=10;)while(abs_R);
m_len=i;
)
else{mnum[l]=0:m_len=l;m_sign=l;}
)
HugeIntHugeInt::operator*(intR)(llugelnthInt=R;return(*this)*hlnt;}
Hugelntllugelnt::operator*(l:ugelnt&R)
(
llugelntResult=0:
Result.m_sign=this->m_sign*R.m_sign:
char*muti1,*muti2,*rcsu!t=Rcsult.mnum;
intlenl,len2;
if(this->mlen>R.Len()):mutil=this->mnum;muti2=R.mrum;lenl=this->nilen;
lcn2=R.m_lcn:}
else{mutil=R.m_num;muti2=this->m_num;len2=this->m_l€n;lenl=R.m_len;}
inti=l,j=l,k=l,carry=0:
whilc(j<=lcn2)
(
i=l;k=j;
while(i<=lenl){resu11[k]+=muti1[i++]*muti2[j]+carry;carry=resu11[k]/10;
result[k]%=10;k++;}
if(carry!=0){resull[k]+=carry;Result,mlen=k;carry=0;}else
{Result.m_len=k-l;}
j++;
}
returnResult;
)
std::ostream&operator«(std::ostream&out,Hugelnt&R){inti:if(R.m_sign==-l){
oul<<"”;}for(i=R.mlen;i!=0;i--){oui〈<R.mnumCi]+0;)out«std::endl;return
out;}
第2题.改善第一次作业中的IntSet,分别使用运算符+、*、-和<<表达集合并、集合交、
集合差和集合输出。(必须上机验证)
/*intset.h*/
ftifndefINTSET_H
^defineINTSET_H
classIntSet{
intcursizc,maxsizc:int*x;boolmember(intt)const;
public:
IntSet(intm=100);//构造函数
IntSet(constIntSet&);.■/拷贝构造函数
'intSet();//折构函数
voidinsert(intt);//插入元素
friendostrcan)&opcrator«(ostrcam&,constlntSet&);
IntSetoperator-(constIntSet&);//集合差
IntSeloperator+(constIntSci&);//集合并
IntSetoperator*(constIntSet&);//集合交
};
ttendif
/,*intset.epp
"include*stdafx.h*
^include<iostream>
■include<cstdlib>
usingnamespacestd:
^include"intset.h"
IntSet::IntSet(intm){if(n<l)exit(l):cursize=0;x=newiit[maxsize=m];}
IntSet::'IntSet()(deletex;}
InlSet::IntSet(constIntSet&in)
(
cursize=m.cursize:x=newint[maxsize=m.maxsize];for(irti=0;i<cursize;i++){
x[i]=m.x[i];)
)
boolIntSet::member(intt)const
int1=0;intu=cursize-l;
while(K=u)
(
intm=(u+l)/2;
if(t<x[m])
u=m-I;
elseif(t>x[m])
l=m+l;
else
returntrue;
returnfalse;
)
voidIntSet::insert(intt)
if(member(:)){return;}if(cursizc>=maxsize){exit⑴;}x[cursize++]=t;
for(inti=cursize-l;i>0;i-){if(x[i]<x[i-l]){inttemp=x[i];x[i]=x[i-l];
x[i-l]=temp;}else(break;})
)
ostrean)&operator«(ostrean)&os,constIntSet
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 足癣治疗过程中的注意事项
- 褥疮预防知识普及
- 角膜塑形镜消毒步骤详解
- 肿瘤患者的介入治疗护理
- 血透室护理PDCA循环的实践案例分享
- 胸腔闭式引流患者的活动指导
- 胰腺脂肪浸润与癌变精准判别
- 骨科牵引患者的心理疏导技巧
- 肾活检术术后舒适护理
- 营养护理与膳食管理
- 2026重庆联合产权交易所集团股份有限公司招聘13人考试备考题库及答案解析
- 2026年广东深圳市高三二模高考语文试卷试题(含答案)
- CNCA-C09-02:2025 强制性产品认证实施规则 移动电源、锂离子电池和电池组(试行)
- 2026年天津市河东区中考一模道德与法治试卷和答案
- 水利三类人员安全员b证考试题
- 2025济源市中考历史试卷
- 职业教育励志微课
- 银川市、石嘴山市、吴忠市三市2026年高三年级学科教学质量检测 英语+答案
- 2025-2026学年统编版(新教材)小学道德与法治三年级下册《弘扬家庭美德》教学课件
- 2026年细胞因子释放综合征分级管理临床指南
- 2026年广州民航职业技术学院单招职业适应性测试题库含答案详解(完整版)
评论
0/150
提交评论