第十三周上机实践项目1(3):分数中的运算符重载
2016-06-09 09:27
399 查看
/* *Copyright(c) 2016.烟台大学计算机与控制工程学院 *ALL rights reserved. *文件名称:app.cpp *作者:赵子琳 *完成日期:2016年6月9日 *问题描述:定义分数的一目运算+和-,分别代表分数取正和求反,将“按位取反运算符”~重载为分数的求倒数运算。 */ #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); CFraction operator+();//取正一目运算 CFraction operator-();//取反一目运算 CFraction operator~();//取倒数一目运算 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; } CFraction CFraction::operator+() { return *this; } CFraction CFraction::operator-() { CFraction t; t.nume=-nume; t.deno=deno; return t; } CFraction CFraction::operator~() { CFraction t; t.nume=deno; t.deno=nume; if(t.deno<0)//保证负分数的负号在分子上 { t.deno=-t.deno; t.nume=-t.nume; } 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; }
相关文章推荐
- 第14周阅读程序(1)
- 生成MyEclipse8.5注册码的Java源码 生成MyEclipse8.5注册码的java源码 MyEclipseKeyGen MyEclipse8.5注册机 MyEclipse8.5注册码生成
- iOS中的UISlider滑块组件用法总结
- 数组类
- clang terminal编译OC代码
- leetcode 4. Median of Two Sorted Arrays
- java正则表达式
- 第14周阅读程序1(2) .
- 实现图片的自动滚动
- 第13周 项目二(3)
- 第14周项目1—排序函数模板
- JBOSS常用端口说明
- 项目1-程序填空
- 排序函数模板2之double 型
- 第15周-项目2-洗牌
- 十一周项目-1-点-圆-圆柱类族的设计(2)
- 二进制文件的读写(视频)
- AtomicInteger类的理解与使用
- 第15周项目1--程序填空
- LaTeX如何设置段落层次结构