C++大学教程实验指导课后实验答案(7-10)

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

//整数集//chart7#includeiostreamusingstd::cout;usingstd::endl;#includeintegerset.hintmain(){IntegerSeta(101),b(101),c(101),d(101);coutEntersetA:\n;a.inputSet();cout\nEntersetB:\n;b.inputSet();c=b.unionOfIntegerSets(a);d=b.intersectionOfIntegerSets(a);cout\nUnionofAandBis:\n;c.setPrint();coutIntersectionofAandBis:\n;d.setPrint();if(a.isEqualTo(b))coutSetAisequaltosetB\n;elsecoutSetAisnotequaltosetB\n;cout\nInserting77intosetA...\n;a.insertElement(77);coutSetAisnow:\n;a.setPrint();cout\nDeleting77fromsetA...\n;a.deleteElement(77);coutSetAisnow:\n;a.setPrint();coutendl;return0;}#includeiostreamusingstd::cout;usingstd::cin;#includeiomanip#includecassertusingstd::setw;/*whiteincludederectiveforintrgerSet.h*/#includeintegerset.hIntegerSet::IntegerSet(ints){size=s;set=newint[size];assert(set!=0);emptySet();/*whitecalltoemptyset*/}IntegerSet::IntegerSet(constIntegerSet&init){size=init.size;/*whritastatementtoallocatesufficientmemory*/set=newint[size];assert(set!=0);emptySet();/*whritestamenttocopyelementsofinit*/for(inti=0;isize;i++)set[i]=init.set[i];}voidIntegerSet::emptySet(){for(inti=0;isize;i++)set[i]=0;}voidIntegerSet::inputSet(void){intnumber;do{coutEnteranelement(-1toend):;cinnumber;if(validEntry(number))set[number]=1;//数和位置是相同的elseif(number!=-1)coutInvalidElement\n;}while(number!=-1);coutEntrycomplete\n;}voidIntegerSet::setPrint(void)const{intx=1;boolempty=true;cout'{';for(intu=0;usize;++u)if(set[u]){coutsetw(4)u(x%10==0?\n:);empty=false;++x;}if(empty)coutsetw(4)---;coutsetw(4)}\n;}IntegerSetIntegerSet::unionOfIntegerSets(constIntegerSet&r){IntegerSettemp(sizer.size?size:r.size);temp.emptySet();intiterations=(sizer.size?size:r.size);for(inti=0;iiterations;i++)if(set[i]==1||r.set[i]==1)temp.set[i]=1;returntemp;}IntegerSetIntegerSet::intersectionOfIntegerSets(constIntegerSet&r){IntegerSettemp(sizer.size?size:r.size);temp.emptySet();intiterations=(sizer.size?size:r.size);for(inti=0;iiterations;i++)if(set[i]==1&&r.set[i]==1)temp.set[i]=1;returntemp;}voidIntegerSet::insertElement(intk){if(validEntry(k))set[k]=1;elsecoutInvalidinsertattempted!\n;}voidIntegerSet::deleteElement(intm){set[m]=0;}/*writedefinitionforisEqualTo*/boolIntegerSet::isEqualTo(constIntegerSet&r)const{if(size==r.size){for(inti=0;isize;i++){if(set[i]!=r.set[i]){returnfalse;break;}}returntrue;}elsereturnfalse;}#ifndefINTSET_H#defineINTSET_HclassIntegerSet{public:IntegerSet(int);IntegerSet(constIntegerSet&);IntegerSetunionOfIntegerSets(constIntegerSet&);IntegerSetintersectionOfIntegerSets(constIntegerSet&);voidemptySet(void);voidinputSet(void);voidinsertElement(int);voiddeleteElement(int);voidIntegerSet::setPrint(void)const;/*writeaprototypeforsetprint*/boolisEqualTo(constIntegerSet&)const;private:int*set;//dynamicallyallocatedsetintsize;boolvalidEntry(intx)const{returnx=0&&xsize;}};#endif//计算机#includeiostream#includeSimpleCalculator.husingstd::cout;usingstd::endl;intmain(){doublea=10.0;doubleb=20.0;SimpleCalculatorsc;coutThevalueofais:a\n;coutThevalueofbis:b\n\n;doubleaddition=sc.add(a,b);coutAddingaandbyieldsaddition\n;doublesubtraction=sc.subtract(a,b);coutSubtractingbfromasubtraction\n;doublemultiplication=sc.multiply(a,b);coutMultiplyingaandbyieldsmultiplication\n;doubledivision=sc.divide(a,b);coutDividingabybyieldsdivisionendl;return0;}#includeSimpleCalculator.hdoubleSimpleCalculator::add(doublea,doubleb)const{returna+b;}doubleSimpleCalculator::subtract(doublea,doubleb)const{returna-b;}doubleSimpleCalculator::multiply(doublea,doubleb)const{returna*b;}doubleSimpleCalculator::divide(doublea,doubleb)const{returna/b;}#includeSimpleCalculator.hdoubleSimpleCalculator::add(doublea,doubleb)const{returna+b;}doubleSimpleCalculator::subtract(doublea,doubleb)const{returna-b;}doubleSimpleCalculator::multiply(doublea,doubleb)const{returna*b;}doubleSimpleCalculator::divide(doublea,doubleb)const{returna/b;}//学生#ifndefSTUDENT_H#defineSTUDENT_HclassStudent{public:Student(constchar*);~Student();voiddisplayGrades()const;Student&addGrade(int);staticintgetNumStudents();private:int*grades;char*name;intnumGrades;intidNum;staticintnumStudents;};#endif#includeiostreamusingstd::cout;usingstd::endl;#includeiomanipusingstd::setw;#includecstring#includestudent.hintStudent::numStudents=0;//初始化Student::Student(constchar*n){grades=newint[100];grades[0]=0;name=newchar[strlen(n)+1];strcpy(name,n);numGrades=0;numStudents++;coutAstudenthasbeenadded\n;}Student::~Student(){coutnamehasbeendeleted\n;deletegrades;deletename;numStudents--;}voidStudent::displayGrades()const{coutHerearethegradesfornameendl;for(inti=0;inumGrades;i++)coutsetw(5)grades[i];coutendlendl;}Student&Student::addGrade(intgrade){numGrades++;int*temp=newint[numGrades+1];for(inti=0;inumGrades;i++)temp[i]=grades[i];temp[numGrades]=grade;grades[numGrades-1]=temp[numGrades];return*this;}intStudent::getNumStudents(){returnnumStudents;}#includeiostreamusingstd::cout;usingstd::endl;#inc

1 / 23
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功