第八周——运算符重载——项目三(一)分数类
来源:互联网 发布:国外好玩的软件 编辑:程序博客网 时间:2024/06/01 08:41
问题及代码:
/* *Copyright (c) 2014,烟台大学计算机学院 *All rights reserved. *文件名称:lily.cpp *作者:李莉 *完成日期:2015年4月25日 *版本号:v1.0 * *问题描述:实现分数类中的运算符重载 *程序输入:无输入*程序输出:如结果图*/#include <iostream>#include <Cmath>using namespace std;class CFraction{private: int nume; // 分子 int deno; // 分母public: CFraction(int nu=0,int de=1):nume(nu),deno(de) {}; void simplify(); void display(); CFraction operator+(const CFraction &f); CFraction operator-(const CFraction &f); CFraction operator*(const CFraction &f); CFraction operator/(const CFraction &f); bool operator<(const CFraction &f); bool operator>(const CFraction &f); bool operator==(const CFraction &f); bool operator<=(const CFraction &f); bool operator>=(const CFraction &f); bool operator!=(const CFraction &f);};void CFraction::display(){ cout<<"("<<nume<<"/"<<deno<<")"<<endl;}CFraction CFraction::operator+(const CFraction &f){ int n,d; n=nume*f.deno+f.nume*deno; d=deno*f.deno; CFraction f0(n,d); f0.simplify(); return f0;}CFraction CFraction::operator-(const CFraction &f){ int n,d; n=nume*f.deno-f.nume*deno; d=deno*f.deno; CFraction f0(n,d); f0.simplify(); return f0;}CFraction CFraction::operator*(const CFraction &f){ int n,d; n=nume*f.nume; d=deno*f.deno; CFraction f0(n,d); f0.simplify(); return f0;}CFraction CFraction::operator/(const CFraction &f){ int n,d; n=nume*f.deno; d=deno*f.nume; CFraction f0(n,d); f0.simplify(); return f0;}bool CFraction::operator<(const CFraction &f){ int this_nume,f_nume,common_deno; this_nume=nume*f.deno; f_nume=f.nume*deno; common_deno=deno*f.deno; if ((this_nume-f_nume)*common_deno<0) return true; return false;}bool CFraction::operator>(const CFraction &f){ int this_nume,f_nume,common_deno; this_nume=nume*f.deno; f_nume=f.nume*deno; common_deno=deno*f.deno; if ((this_nume>f_nume&&common_deno>0)||(this_nume<f_nume&&common_deno<0)) return true; return false;;}bool CFraction::operator!=(const CFraction &f){ if(*this<f||*this>f) return true; return false;}bool CFraction::operator==(const CFraction &f){ if(*this!=f)return false; return true;}bool CFraction::operator<=(const CFraction &f){ if(*this>f) return false; return true;}bool CFraction::operator>=(const CFraction &f){ if(*this<f) return false; return true;}void CFraction::simplify(){ int m,n,r; m=fabs(deno); //此处存在bug,请参看评论1楼 n=fabs(nume); while((r=m%n)) // 求m,n的最大公约数 { m=n; n=r; } deno/=n; // 化简 nume/=n; if (deno<0) // 将分母转化为正数 { deno=-deno; nume=-nume; }}int main(){ CFraction f1(1,4),f2(1,20),f3; cout<<"分数1是:"<<endl; f1.display(); cout<<"分数2是:"<<endl; f2.display(); f3=f1+f2; cout<<"两分数相加的结果是:"<<endl; f3.display(); f3=f1*f2; cout<<"两分数相乘的结果是:"<<endl; f3.display(); f3=f1-f2; cout<<"两分数相减的结果是:"<<endl; f3.display(); f3=f1/f2; cout<<"两分数相除的结果是:"<<endl; f3.display(); if(f1>f2) cout<<"f1>f2"<<endl; if(f1==f2) cout<<"f1=f2"<<endl; if(f1<f2) cout<<"f1<f2"<<endl; if(f1!=f2) cout<<"f1≠f2"<<endl; if(f1<=f2) cout<<"f1<=f2"<<endl; if(f1>=f2) cout<<"f1>=f2"<<endl; f2.display(); return 0;}
运行结果:
心得体会:
写完之后一直改bug。改改改改。。。最值得的一点是,理解了warning的意思,就是那个处理分子分母的最大公约数的时候给的警告,就是混淆了==语=,即应该是使用==而不是=,所以在用一层圆括号,标注出来
0 0
- 第八周——运算符重载——项目三(一)分数类
- 第八周——运算符重载——项目三(2)分数类
- 第八周(项目三1)——分数类中的运算符重载.
- 第八周(项目三2)——分数类中的运算符重载。
- 15第八周项目三——分数类中的运算符重载(1)
- 15第八周项目三——分数类中的运算符重载(2)
- 第八周 课后实践:项目三——分数类中的运算符重载(1)
- 第八周项目三——分数类中的运算符重载(1)
- 第八周项目三——分数类中的运算符重载(2)
- 第八周项目三——分数类中的运算符重载(2)
- 第八周项目三——分数类中的运算符重载
- 第八周项目三:分数类中的运算符重载(一)
- 第八周上机实践项目3——分数类的运算符重载(1)
- 第八周上机实践项目3——分数类中的运算符重载(2)
- 第八周项目3(1)——分数类中的运算符重载
- 第八周项目3——分数类中的运算符重载(四则运算)
- 第八周项目三----分数类中的运算符重载
- 第八周 项目三:分数类中的运算符重载
- java利用label 标识退出多重循环
- 关于 rs.Open SQL_str,conn,1,2 中数字的问题
- Film
- linux下Redis与phpredis扩展安装
- devexpress gridcontrol gridview小结
- 第八周——运算符重载——项目三(一)分数类
- 使用Nexus搭建Maven私服
- 插花艺术构图原理
- 盗版XP成主要恶意攻击对象
- android与WEB服务器交互时,如何保证在同一个会话Session中通信
- 面试前应该掌握的问题
- 快速还原系统注册表的方法
- Oulipo
- Windows 8消费者预览版兼容的杀毒软件