分数类中的运算符重载(续)
来源:互联网 发布:最优化问题求解 编辑:程序博客网 时间:2024/05/20 05:07
问题及代码:
/* * Copyright (c) 2014, 烟台大学计算机学院 * All rights reserved. * 文件名称:test.cpp * 作 者:李盈盈 * 完成日期:2015年 05 月 10 日 * 版 本 号: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(); friend istream &operator>>(istream &in,CFraction &x); friend ostream &operator<<(ostream &out,CFraction x); CFraction operator+(const CFraction &c); CFraction operator-(const CFraction &c); CFraction operator*(const CFraction &c); CFraction operator/(const CFraction &c); 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::simplify(){ int m,n,r; n=fabs(deno); m=fabs(nume); while((r=m%n)!=0) { m=n; n=r; } deno/=n; nume/=n; if (deno<0) { deno=-deno; nume=-nume; }}istream &operator>>(istream &in,CFraction &x){ char ch; while(1) { cin>>x.nume>>ch>>x.deno; if (x.deno==0) cerr<<"分母为0, 请重新输入\n"; else if(ch!='/') cerr<<"格式错误(形如m/n)! 请重新输入\n"; else break; } return cin;}ostream &operator<<(ostream &out,CFraction x){ cout<<x.nume<<'/'<<x.deno; return cout;}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; if (!c.nume) return *this; t.nume=nume*c.deno; t.deno=deno*c.nume; t.simplify(); return t;}CFraction CFraction:: operator+(){ return *this;}CFraction CFraction:: operator-(){ CFraction x; x.nume=-nume; x.deno=deno; return x;}CFraction CFraction:: operator~(){ CFraction x; x.nume=deno; x.deno=nume; if(x.deno<0) { x.deno=-x.deno; x.nume=-x.nume; } return x;}bool CFraction::operator>(const CFraction &c){ int this_nume,c_nume,common_deno; this_nume=nume*c.deno; c_nume=c.nume*deno; common_deno=deno*c.deno; if ((this_nume-c_nume)*common_deno>0) return true; return false;}bool CFraction::operator<(const CFraction &c){ int this_nume,c_nume,common_deno; this_nume=nume*c.deno; c_nume=c.nume*deno; common_deno=deno*c.deno; if ((this_nume-c_nume)*common_deno<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 || *this<c) 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;}int main(){ CFraction x,y,s; cout<<"输入x: "; cin>>x; cout<<"输入y: "; cin>>y; s=+x+y; cout<<"+x+y="<<s<<endl; s=x-y; cout<<"x-y="<<s<<endl; s=x*y; cout<<"x*y="<<s<<endl; s=x/y; cout<<"x/y="<<s<<endl; cout<<"-x="<<-x<<endl; cout<<"+x="<<+x<<endl; cout<<"x的倒数: "<<~x<<endl; cout<<x; if (x>y) cout<<"大于"; if (x<y) cout<<"小于"; if (x==y) cout<<"等于"; cout<<y<<endl; return 0;}
运行结果:
0 0
- 分数类中的运算符重载(续)
- 分数类中的运算符重载(续)
- 分数类中的运算符重载(续)
- 分数类中的运算符重载
- 分数类中的运算符重载
- 分数类中的运算符重载
- 分数类中的运算符重载
- 分数类中的运算符重载
- 分数类中的运算符重载
- 分数类中的运算符重载
- 分数类中的运算符重载
- 分数类中的运算符重载
- 分数类中的运算符重载
- 分数类中的运算符重载
- 分数类中的运算符重载
- 分数类中的运算符重载
- 分数类中的运算符重载
- 分数类中的运算符重载
- nyoj题目277:车牌号
- 网页上不带下载链接但可播放的歌曲可以如何下载呢?
- eclipse3.X集成maven3.0.4插件详细图解(安装、配置)
- From Apprentice To Artisan 01
- Two ways to create a new thread of execution
- 分数类中的运算符重载(续)
- Android之Android apk动态加载机制的研究
- 设计4个线程,其中两个线程每次对j增加1,另外两个线程对j每次减少1。写出程序。
- eclipse集成maven3后,创建java项目详细图解
- SQLite Version3.3.6源代码文件结构
- java 遍历arrayList的四种方法
- ORMLite完全解析(一)通过实例理解使用流程
- HZNU-1028: 多项式合并(map实现)
- 畅游C++ Boost Serialization 序列化