第十三周项目1.4—分数类中的运算符重载
2016-05-25 21:04
302 查看
/* *Copyright(c) 2016.烟台大学计算机与控制工程学院 *ALL rights reserved. *文件名称:test.cpp *作者:杨驰 *完成日期:2016年5月25 *问题描述:定义分数类中<<和>>运算符重载,实现分数的输入输出,改造原程序中对 运算结果显示方式,使程序读起来自然。 */ #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~();//取倒数一目运算 friend istream &operator>>(istream &in,CFraction &x); friend ostream &operator<<(ostream &out,CFraction &x); 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); }; istream &operator>>(istream &in,CFraction &x) { char c; while(1) { cin>>x.nume>>c>>x.deno; if(x.deno==0) cerr<<"分母为0,请重新输入\n"; else if(c!='/') cerr<<"格式错误!请重新输入\n"; else break; } return cin; } ostream &operator<<(ostream &out,CFraction &x) { cout<<x.nume<<'/'<<x.deno; return cout; } 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; }
相关文章推荐
- 打飞机
- mybatis 动态sql详解
- Spring 整合 Redis
- 数据结构实验之栈:行编辑器
- Android--Bundle类
- 1341 - Aladdin and the Flying Carpet ---light oj (唯一分解定理+素数筛选)
- 聚类算法实践(一)——层次聚类、K-means聚类
- C语言程序设计感想
- 51 NOD 1189 阶乘分数(素因子分解+推公式+求逆元)
- opencv入门建议和资源推荐
- C++第六次作业
- bzoj1901 Zju2112 Dynamic Rankings
- 菜鸟学Java之错将for循环大写
- 第12周项目1-实现复数类中的运算符重载(2)使用友缘函数
- 2016.5.25(1)0/1背包问题
- Android Home按键的监听与屏蔽方式
- Bean property 'productService' is not writable or has an invalid setter method. Does the parameter t
- Spring Boot 快速入门
- 汇编学习(五)8086汇编拾遗 (4)
- 第一节课作业:(第一篇博客)