您好,欢迎访问三七文档
//编程:建立一个分数类。分数类的数据成员包括分子和分母,操作包括显示、输入、约分、通分、比较、加、减、乘、除、求相反数。#includeiostream#includecmathusingnamespacestd;classfraction{intabove;//分子intbelow;//分母voidreduction();//约分voidmakeCommond(fraction&);//通分public:fraction(inta=0,intb=1){//构造函数above=a;below=b;}fractionadd(fraction);//两分数相加fractionsub(fraction);//本分数减去实参分数fractionmul(fraction);//两分数相乘fractiondiv(fraction);//本分数除以实参分数fractionreciprocal();//求倒数boolequal(fraction);//等于运算boolgreaterThan(fraction);//大于运算boollessThan(fraction);//小于运算voiddisplay();//显示分数voidinput();//输入分数};voidfraction::reduction(){//约分先求最大公约数inta,b,temp;if(below0){above=-above;below=-below;}a=abs(above);b=abs(below);while(a%b){//欧几里德法求最大公约数temp=a;a=b;b=temp%b;}above/=b;below/=b;}voidfraction::makeCommond(fraction&b){inttemp;reduction();b.reduction();above*=b.below;b.above*=below;temp=below*b.below;below=b.below=temp;}fractionfraction::add(fractionb){fractiontemp;makeCommond(b);//通分temp.above=above+b.above;temp.below=below;temp.reduction();//约分returntemp;}fractionfraction::sub(fractionb){fractiontemp;makeCommond(b);//通分temp.above=above-b.above;temp.below=below;temp.reduction();//约分returntemp;}fractionfraction::mul(fractionb){fractiontemp;temp.above=above*b.above;temp.below=below*b.below;temp.reduction();//约分returntemp;}fractionfraction::div(fractionb){fractiontemp;if(b.above==0){cout零不能作除数!endl;exit(1);}temp.above=above*b.below;temp.below=below*b.above;temp.reduction();//约分returntemp;}fractionfraction::reciprocal(){fractiontemp;temp.above=below;temp.below=above;temp.reduction();//约分returntemp;}boolfraction::equal(fractionb){makeCommond(b);//通分return(above==b.above);}boolfraction::greaterThan(fractionb){makeCommond(b);//通分return(aboveb.above);}boolfraction::lessThan(fractionb){makeCommond(b);//通分return(aboveb.above);}voidfraction::display(){reduction();//约分cout为:above/belowendl;}voidfraction::input(){while(1){cout请顺序输入分子和分母(整数):endl;cinabovebelow;if(below==0)cout分母不可为零!endl;else{reduction();return;}}}intmain(){fractionf1(-3,-5),f2(-3,5),f3(3,-7),f4,f5(8);coutf1;f1.display();coutf2;f2.display();coutf3;f3.display();coutf4;f4.display();coutf5;f5.display();if(f1.greaterThan(f2))coutf1f2endl;if(f2.lessThan(f3))coutf2f3endl;if(f1.equal(f1))coutf1==f1endl;f4=f1.add(f3);coutf4=f1+f3;f4.display();f4=f1.sub(f2);coutf4=f1-f2;f4.display();f4=f1.mul(f3);coutf4=f1*f3;f4.display();f4=f2.div(f3);coutf4=f1/f3;f4.display();f4=f2.reciprocal();coutf4=1/f2;f4.display();f4.input();coutf4;f4.display();return0;}4.3构造一个日期时间类(Timedate),数据成员包括年、月、日和时、分、秒,函数成员包括设置日期时间和输出时间,其中年、月请用枚举类型,并完成测试。(包括用成员函数和用普通函数)解:本题要求仅是定义类的练习,并非实用的提供日期时间的程序。实用的日期时间程序见附录二的日期时间函数。#includeiostream#includeiomanipusingnamespacestd;enumYR{Y2000,Y2001,Y2002,Y2003,Y2004,Y2005};//enumMT{Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec};classTimedate{private:YRyear;MTmonth;intdate;inthh;intmm;intss;public:Timedate(){year=Y2000;month=Jan;date=1;hh=0;mm=0;ss=0;}Timedate(YRa,MTb,intc){year=a;month=b;date=c;hh=12;mm=30;ss=0;}voidgetdate(YR&,MT&,int&);//使用引用一次取得3个数值voidgettime(int&,int&,int&);voidputdate(YR,MT,int);voidputtime(int,int,int);voidlist();};voidTimedate::getdate(YR&y,MT&m,int&d){y=year;m=month;d=date;}voidTimedate::gettime(int&a,int&b,int&c){a=hh;b=mm;c=ss;}voidTimedate::putdate(YRa,MTb,intc){year=a;month=b;date=c;}voidTimedate::puttime(inta,intb,intc){hh=a;mm=b;ss=c;}voidTimedate::list(){//成员函数,直接访问私有的数据成员coutyear/month/date:;switch(year){caseY2000:cout2000;break;caseY2001:cout2001;break;caseY2002:cout2002;break;caseY2003:cout2003;break;caseY2004:cout2004;break;caseY2005:cout2005;break;}switch(month){caseJan:cout'/'Jan;break;caseFeb:cout'/'Feb;break;caseMar:cout'/'Mar;break;caseApr:cout'/'Apr;break;caseMay:cout'/'May;break;caseJun:cout'/'Jun;break;caseJul:cout'/'Jul;break;caseAug:cout'/'Aug;break;caseSep:cout'/'Sep;break;caseOct:cout'/'Oct;break;caseNov:cout'/'Nov;break;caseDec:cout'/'Dec;break;}cout'/'dateendl;couthour:minite:second:;couthh':'mm':'ssendl;}intmain(intargc,char*argv[]){TimedateA(Y2004,Mar,3),B;A.list();B.list();B.putdate(Y2005,Oct,18);B.puttime(17,30,00);B.list();return0;}4.4设计并测试一个矩形类(Rectangle),属性为矩形的左下与右上角的坐标,矩形水平放置。操作为计算矩形周长与面积。测试包括用成员函数和普通函数。解:这里的矩形的4边分别与x轴y轴平行,为最简单的情况。注意参数有缺省值的函数的声明和定义格式。#includeiostream#includecmathusingnamespacestd;classRectangle{doubleleft,top;doubleright,bottom;public:Rectangle(doublel=0,doublet=0,doubler=0,doubleb=0);~Rectangle(){};//析构函数,在此函数体为空voidAssign(doublel,doublet,doubler,doubleb);doublegetLeft(){returnleft;}//以下四个函数皆为内联成员函数doublegetRight(){returnright;}doublegetTop(){returntop;}doublegetBottom(){returnbottom;}voidShow();doubleArea();doublePerimeter();};//构造函数,带缺省参数,缺省值为全0,在声明中指定Rectangle::Rectangle(doublel,doublet,doubler,doubleb){left=l;top=t;right=r;bottom=b;}voidRectangle::Assign(doublel,doublet,doubler,doubleb){//赋值left=l;top=t;right=r;bottom=b;}voidRectangle::Show(){//成员函数直接使用私有的数据成员coutleft-toppointis(left,top)'\n';co
本文标题:类的作业及答案
链接地址:https://www.777doc.com/doc-4782812 .html