欢迎来到天天文库
浏览记录
ID:20959538
大小:61.50 KB
页数:8页
时间:2018-10-18
《类作业及答案》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、//编程:建立一个分数类。分数类的数据成员包括分子和分母,操作包括显示、输入、约分、通分、比较、加、减、乘、除、求相反数。#include#includeusingnamespacestd;classfraction{intabove;//分子intbelow;//分母voidreduction();//约分voidmakeCommond(fraction&);//通分public:fraction(inta=0,intb=1){//构造函数above=a;below=b;}fractionadd(fraction);//两分数相加fract
2、ionsub(fraction);//本分数减去实参分数fractionmul(fraction);//两分数相乘fractiondiv(fraction);//本分数除以实参分数fractionreciprocal();//求倒数boolequal(fraction);//等于运算boolgreaterThan(fraction);//大于运算boollessThan(fraction);//小于运算voiddisplay();//显示分数voidinput();//输入分数};voidfraction::reduction(){//约分先求最大公约数inta,b,temp
3、;if(below<0){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::ad
4、d(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
5、){fractiontemp;temp.above=above*b.above;temp.below=below*b.below;temp.reduction();//约分returntemp;}fractionfraction::div(fractionb){fractiontemp;if(b.above==0){cout<<"零不能作除数!"<6、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(above>b.above);}boolfraction::lessThan(fractionb){makeCommond7、(b);//通分return(above>above>>below;if(below==0)cout<<"分母不可为零!"<
6、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(above>b.above);}boolfraction::lessThan(fractionb){makeCommond
7、(b);//通分return(above>above>>below;if(below==0)cout<<"分母不可为零!"<
此文档下载收益归作者所有