第十三周项目1.2—分数中的运算符重载
2016-05-25 20:21
267 查看
/* *Copyright(c) 2016.烟台大学计算机与控制工程学院 *ALL rights reserved. *文件名称:test.cpp *作者:杨驰 *完成日期:2016年5月25 *问题描述:实现分数类中的对象和整型数的四则运算。分数类中的对象可以和整型数进行四则 运算,且运算符合交换。例如:CFraction a(1,3),b; int i=2;可以完成b=a+i;。 同样,可以完成i+a,45+a,a*27,5/a等各种运算。 */ #include <iostream> #include <Cmath> using namespace std; class CFraction { private: int nume; // 分子 int deno; // 分母 public: CFraction(int nu=1,int de=1):nume(nu),deno(de){} void simplify(); void display(); CFraction operator+(const CFraction &c);//两个分数相加,结果要化简 CFraction operator-(const CFraction &c);//两个分数相减,结果要化简 CFraction operator*(const CFraction &c);//两个分数相乘,结果要化简 CFraction operator/(const CFraction &c);//两个分数相除,结果要化简 CFraction operator+(int a); CFraction operator-(int a); CFraction operator*(int a); CFraction operator/(int a); bool operator>(const CFraction &c); bool operator<(const CFraction &c); bool operator==(const CFraction &c); bool operator!=(const CFraction &c); bool operator>=(const CFraction &c); bool operator<=(const CFraction &c); }; void CFraction::display() { cout<<nume<<"/"<<deno<<endl; } //分数简化 void CFraction::simplify() { int m,n,t; n=fabs(deno); m=fabs(nume); while(t=m%n)// 求m,n的最大公约数 { m=n; n=t; } deno/=n; nume/=n; if(deno<0) { deno=-deno; nume=-nume; } } CFraction CFraction::operator+(const CFraction &c) { CFraction t; t.nume=nume*c.deno+c.nume*deno; t.deno=deno*c.deno; t.simplify(); return t; } CFraction CFraction::operator-(const CFraction &c) { CFraction t; t.nume=nume*c.deno-c.nume*deno; t.deno=deno*c.deno; t.simplify(); return t; } CFraction CFraction::operator*(const CFraction &c) { CFraction t; t.nume=nume*c.nume; t.deno=deno*c.deno; t.simplify(); return t; } CFraction CFraction::operator/(const CFraction &c) { CFraction t; t.nume=nume*c.deno; t.deno=deno*c.nume; t.simplify(); return t; } CFraction CFraction::operator+(int a) { CFraction t; int c; c=a*deno; t.deno=deno; t.nume=nume+c; t.simplify(); return t; } CFraction CFraction::operator-(int a) { CFraction t; int c; c=a*deno; t.deno=deno; t.nume=nume-c; t.simplify(); return t; } CFraction CFraction::operator*(int a) { CFraction t; t.deno=deno; t.nume=nume*a; t.simplify(); return t; } CFraction CFraction::operator/(int a) { CFraction t; t.deno=deno*a; t.nume=nume; t.simplify(); return t; } bool CFraction::operator>(const CFraction &c) { int n1,d1; n1=nume*c.deno-c.nume*deno; d1=deno*c.deno; if(n1*d1>0) return true; return false; } bool CFraction::operator<(const CFraction &c) { int n1,d1; n1=nume*c.deno-c.nume*deno; d1=deno*c.deno; if(n1*d1<0) return true; return false; } bool CFraction::operator==(const CFraction &c) { if(*this!=c) return false; return true; } bool CFraction::operator!=(const CFraction &c) { if(*this==c) return false; return true; } bool CFraction::operator>=(const CFraction &c) { if(*this<c) return false; return true; } bool CFraction::operator<=(const CFraction &c) { if(*this>c) return false; return true; } int main() { char c; int n,d; cout<<"输入分数的样例:x/y"<<endl; cin>>n>>c>>d; CFraction x(n,d); x.display(); cin>>n>>c>>d; CFraction y(n,d); y.display(); CFraction s; s=x+y; cout<<"x+y="; s.display(); cout<<endl; s=x-y; cout<<"x-y="; s.display(); cout<<endl; s=x*y; cout<<"x*y="; s.display(); cout<<endl; s=x/y; cout<<"x/y="; s.display(); cout<<endl; cout<<"请输入要进行运算的整数d"<<endl; cin>>d; s=x+d; cout<<"x+d="; s.display(); cout<<endl; s=x-d; cout<<"x-d="; s.display(); cout<<endl; s=x*d; cout<<"x*d="; s.display(); cout<<endl; s=x/d; cout<<"x/d="; s.display(); cout<<endl; if(x>y) cout<<"大于!"<<endl; if(x<y) cout<<"小于! "<<endl; if(x==y) cout<<"等于!"<<endl; return 0; }
相关文章推荐
- Android中Parcelable接口用法
- js点击button按钮跳转到另一个新页面
- 自我管理需要哪些工具
- JSON解析数据 vs 从bomb服务器数据库查询数据
- redis之(三)redis的数据类型
- 详解iOS开发中处理屏幕旋转的几种方法
- php内存机制
- maven缺少依赖包,强制更新命令
- linux下查看所有用户及所有用户组的信息
- 176. [USACO Feb07] 奶牛聚会
- 超像素图像分割简述
- 系统IO
- poj3301 三分
- CodeForces 621 B Wet Shark and Bishops
- lucene的介绍与优化
- iOS开发之高级视图—— UITableView操作——新增
- 游戏中的数据挖掘阅读文献小结
- Python模块探秘之smtplib,实现纯文本邮件的发送
- Go学习笔记一:go基础
- 如何使用ZBLibrary-Android快速开发框架