第九周项目三 分数类中的运算符重载(续)
来源:互联网 发布:pdf文档剪切软件 编辑:程序博客网 时间:2024/05/22 19:27
/**Copyright (c)2014,烟台大学计算机与控制工程学院*All rights reserved.*文件名称:test.cpp*作 者:徐洪祥*完成日期:2015年5月20日*版 本 号: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; m=fabs(deno); n=fabs(nume); while(r=m%n) { 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) cout<<"分母不能为0, 请重新输入\n"; else if(ch!='/') cout<<"格式错误! 请重新输入\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+deno*c.nume; t.deno=deno*c.deno; t.simplify(); return t;}CFraction CFraction::operator-(const CFraction &c){ CFraction t; t.nume=nume*c.deno-deno*c.nume; 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+(){ 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=deno*c.nume; common_deno=deno*c.deno; if((this_nume-c_nume)*common_deno>0) return true; else return false;}bool CFraction::operator<(const CFraction &c){ int this_nume,c_nume,common_deno; this_nume=nume*c.deno; c_nume=deno*c.nume; common_deno=deno*c.deno; if((this_nume-c_nume)*common_deno<0) return true; else return false;}bool CFraction::operator==(const CFraction &c){ if(*this-c!=0) return false; else return true;}bool CFraction::operator>=(const CFraction &c){ if(*this<c) return false; else return true;}bool CFraction::operator<=(const CFraction &c){ if(*this>c) return false; else return true;}bool CFraction::operator!=(const CFraction &c){ if(*this>c||*this<c) return true; else return false;}int main(){ CFraction x,y,z; cout<<"x:"; cin>>x; cout<<"y:"; cin>>y; z=+x+y; cout<<"x+y="<<z<<endl; z=+x-y; cout<<"x-y="<<z<<endl; z=x*y; cout<<"x*y="<<z<<endl; z=x/y; cout<<"x/y="<<z<<endl; cout<<"-x="<<-x<<endl; cout<<"+x="<<+x<<endl; cout<<"x的倒数: "<<~x<<endl; if(x>y) cout<<"x>y"<<endl; if(x<y) cout<<"x<y"<<endl; if(x==y) cout<<"x=y"<<endl; cout<<endl; return 0;}
0 0
- 2015年第九周项目三:分数类中的运算符重载(续)(1)
- 2015年第九周项目三:分数类中的运算符重载(续)(2)
- 第九周 项目三:分数类中的运算符重载(续)
- 第九周 项目三-分数类中的运算符重载(续)
- 15第九周项目三——分数类中的运算符的重载(续)
- 第九周项目三--分数类中的运算符重载(续)
- 第九周 课后实践:项目三——分数类中的运算符重载(续)
- 第九周项目三 分数类中的运算符重载(续)
- 第九周项目三——分数类中的运算符重载(续)
- 第九周项目三:分数类中的运算符重载(续)
- 第九周项目三 分数类中的运算符重载(续)
- 第九周 项目三 分数类中的运算符重载(续)
- 第九周项目三 分数类中的运算符重载(续)
- 第九周 项目三--分数类中的运算符重载(续)
- 第九周项目三:分数类中的运算符重载(续)
- 第九周(项目三)——分数类中的运算符重载。
- 第九周项目三——分数类中的运算符重载(利用输入输出运算符重载)
- 第九周--项目3-分数类中的运算符重载
- WLAN标准协议
- linux ping 不通原因总结
- 大数据性能调优之HBase的RowKey设计
- 【笔记】JavaScript编码规范- 存取器
- 第二学期第11周项目1
- 第九周项目三 分数类中的运算符重载(续)
- 获取手机Imei码,手机号,IMSI标识
- ITOO-权限系统(一)
- 使用Java进行SNMP编程-SNMP4J-SNMPv3-代码实例
- 5.20 干掉班长
- 第十一周 项目0-是春哥啊
- 2015-十一周-类的派生继承-项目三
- 第十一周项目一存储班长信息的学生类(2)
- 优化UITableViewCell高度计算的那些事