分数计算器

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

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

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

资源描述

分数计算器0511590120.cpp/*Fractionsclass-headerfile--分数类头文件*//*#ifndef_TGR_FRACTIONS_H#define_TGR_FRACTIONS_H*/#includeiostream.h#includeconio.h#includestdlib.h#includemath.h//#includebool.h#includestring.hclassfraction//分数类定义{public:fraction();//constructorsfraction(intinitn,intinitd);//accessorsintnumerator()const;//取分子,const成员函数,不能修改数据成员intdenominator()const;//取分母doubledecimal()const;//将分数转换为对应的小数fractionabsval()const;//分数的绝对值//modifiersvoidsetnum(intnewnum);//设置分子voidsetden(intnewden);//设置分母voidsimplify();//约分voidrecip();//分子分母转置constfraction&operator+=(intrhs);private:intn;intd;};//free(nonmember)functions//miscellanyvoidsimple();//简单计算函数voidturn();//转换函数voidtest();//测试函数voidcomplex();//混合运算函数voidexam();//随机测试题函数intGCF(intx,inty);//求x,y的最大公因子//additionoperatorsfractionoperator+(constfraction&lhs,constfraction&rhs);fractionoperator+(constfraction&lhs,intrhs);//subtractionoperatorsfractionoperator-(constfraction&lhs,constfraction&rhs);fractionoperator-(constfraction&lhs,intrhs);//multiplicationoperatorsfractionoperator*(constfraction&lhs,constfraction&rhs);fractionoperator*(constfraction&lhs,intrhs);//divisionoperatorsfractionoperator/(constfraction&lhs,constfraction&rhs);fractionoperator/(constfraction&lhs,intrhs);//poweroperatorfractionoperator^(constfraction&base,intexp);//^(幂)运算符重载//comparisonoperatorsbooloperator==(constfraction&lhs,constfraction&rhs);booloperator!=(constfraction&lhs,constfraction&rhs);booloperator=(constfraction&lhs,constfraction&rhs);booloperator=(constfraction&lhs,constfraction&rhs);booloperator(constfraction&lhs,constfraction&rhs);booloperator(constfraction&lhs,constfraction&rhs);booloperator==(constfraction&lhs,intrhs);booloperator!=(constfraction&lhs,intrhs);booloperator=(constfraction&lhs,intrhs);booloperator=(constfraction&lhs,intrhs);booloperator(constfraction&lhs,intrhs);booloperator(constfraction&lhs,intrhs);//I/Ooperators/functionsostream&operator(ostream&os,constfraction&f);//输出运算符重载istream&operator(istream&is,fraction&f);//输入运算符重载voidPrintAsMixed(ostream&os,constfraction&f);//输出带分数/*#endif*//*fraction.cppFractionsclass-implementationfile--分数类执行文件开始*///#includefraction.hfraction::fraction():n(0),d(1){};//缺省构造函数(=0/1)fraction::fraction(intinitn,intinitd):n(initn),d(initd){};intfraction::numerator()const//取分子{returnn;}intfraction::denominator()const//取分母{returnd;}doublefraction::decimal()const//分数转换小数{returndouble(n)/double(d);}fractionfraction::absval()const//取分数绝对值{fractionreturnValue(abs(n),abs(d));//通过构造函数取分母分子绝对值returnreturnValue;}voidfraction::setnum(intnewnum)//设置分子的值{n=newnum;}voidfraction::setden(intnewden)//设置分母的值{if(newden!=0)//分母非零时设置d=newden;else//若分母为零,显示错误信息并退出{cout错误:除数为零!!!\n按任意键终止!;getch();abort();//退出}}voidfraction::simplify()//分数约分{intr=GCF(n,d);n/=r;d/=r;}voidfraction::recip()//分子分母交换{inttemp=n;n=d;d=temp;}constfraction&fraction::operator+=(intrhs)//重载+={fractionnewrhs(rhs,1);//将作为参数的整数RHS转换为分数类的对象return(*this+newrhs);//}intGCF(intx,inty)//辗转相除求x,y的最大公约数{intr=x%y;if(r==0)returny;elsereturnGCF(y,r);}fractionoperator+(constfraction&lhs,constfraction&rhs)//重载+,完成两分数相加情况{fractionsum;//定义分数类对象,通过两参数对象对其赋值intnewnum=(lhs.numerator()*rhs.denominator())+(rhs.numerator()*lhs.denominator());//得到相加后的分子intnewden=(lhs.denominator()*rhs.denominator());//得到相加后的分母sum.setnum(newnum);//设置分子sum.setden(newden);//设置分母sum.simplify();//分数约分returnsum;}fractionoperator+(constfraction&lhs,intrhs)//重载+分数与整数相加{fractionnewrhs(rhs,1);//将RHS转换为分数类对象returnlhs+newrhs;}fractionoperator-(constfraction&lhs,constfraction&rhs)//重载-完成两分数相减{fractionnewrhs=rhs;newrhs.setnum(-rhs.numerator());//设置分母为其相反数returnlhs+newrhs;//利用+的重载}fractionoperator-(constfraction&lhs,intrhs)//重载-完成分数与整数的相减{fractionnewrhs(rhs,-1);returnlhs+newrhs;//利用+的重载}fractionoperator*(constfraction&lhs,constfraction&rhs)//重载*完成两分数相乘{fractionproduct;intnewnum=lhs.numerator()*rhs.numerator();//分子相乘intnewden=lhs.denominator()*rhs.denominator();//分母相乘product.setnum(newnum);//设置分子product.setden(newden);//设置分母product.simplify();//分数约分returnproduct;}fractionoperator*(constfraction&lhs,intrhs)//重载*完成分数与整数的相乘{fractionprod(lhs.numerator()*rhs,lhs.denominator());//通过构造函数完成赋值prod.simplify();//分数约分returnprod;}fractionoperator/(constfraction&lhs,constfraction&rhs)//重载/完成两分数相除{if(rhs==0)//若分母为零,显示错误信息并退出{coutError:Divisionbyzeroattempted!\nPressanykeytoterminate;getch();abort();//退出}fractionnewrhs=rhs;newrhs.recip();//将作分母的分数倒置returnlhs*newrhs;//利用分数乘法}fractionoperator/(constfraction&lhs,intrhs)//重载/完成分数与整数的相除{if(rhs==0)//若分母为零,显示错误信息并退出{cout错误:除数为零!!!\n按任意键终止!;getch();abort();}fractionquot(lhs.numerator(),lhs.denominator()*rhs);//通过构造函数完成赋值quot.simplify();//分数约分returnquot;}fractionoperator^(constfraction&base,intexp){fractionreturnValue;returnValue.setnum(pow(base.numerator(),exp));//通过函数完成returnValue.setden(pow(base.denominator(),exp));//分子分母幂乘returnreturnValue;}booloperator==(constfraction&lhs,constfraction&rhs)//重载==判断两分数是否相等{returnlhs.decimal()==rhs.decimal();}booloperator!=(constfraction&lhs,constfractio

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

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

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

×
保存成功