第九周项目3——分数类中的运算符重载(续)
来源:互联网 发布:淘宝香水店铺推荐 编辑:程序博客网 时间:2024/05/19 09:03
问题及代码:
/**Copyright (c)2015,烟台大学计算机与控制工程学院*All rights reserved.*文件名称:cfraction.cpp*作 者:赵敏*完成日期:2015年5月14日*版 本 号:v1.0**问题描述:1)定义分数的一目运算+和-,分别代表分数取正和求反,将“按位取反运算符”~重载为分数的求倒数运算。(2)定义分数类中<<和>>运算符重载,实现分数的输入输出,改造原程序中对运算结果显示方式,使程序读起来更自然。*/#include <iostream>#include <Cmath>using namespace std;class CFraction{private: int nume; // 分子 int deno; // 分母public: CFraction(int nu=0,int de=1); //构造函数,初始化用 void simplify(); //构造函数及运算符重载的函数声明 int gcd(int,int); CFraction operator+(const CFraction &c); CFraction operator-(const CFraction &c); CFraction operator*(const CFraction &c); CFraction 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); bool operator<=(const CFraction &c); friend istream &operator>>(istream &input,CFraction &x); friend ostream &operator<<(ostream &output,CFraction x); CFraction operator+(); //取正 CFraction operator-(); //取反 CFraction operator~(); //取倒数};//重载函数的实现及用于测试的main()函数CFraction::CFraction(int nu,int de){ if(de!=0) { nume=nu; deno=de; }}void CFraction::simplify(){ int a=gcd(nume,deno); nume=nume/a; deno=deno/a;}int CFraction::gcd(int m, int n){ int r; do { r=m%n; m=n; n=r; }while(r!=0); return m;}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; if (!c.nume) return *this; t.nume=nume*c.deno; t.deno=deno*c.nume; t.simplify(); return t;}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)||(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)||(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 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;}istream &operator>>(istream &input,CFraction &x){ char c; do{ cout<<"input cfraction: "; input>>x.nume>>c>>x.deno; }while(!(c=='/')); return input;}ostream &operator<<(ostream &output,CFraction x){ output<<x.nume<<"/"<<x.deno; return output;}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;}int main(){ CFraction x,y,s; cin>>x>>y; cout<<"分数x=1/3 y=-5/10"<<endl; s=x+y; cout<<"x+y="; cout<<s<<endl; s=x-y; cout<<"x-y="; cout<<s<<endl;; s=x*y; cout<<"x*y="; cout<<s<<endl; s=x/y; cout<<"x/y="; cout<<s<<endl; cout<<x<<endl; if (x>y) cout<<"大于"<<endl; if (x<y) cout<<"小于"<<endl; if (x==y) cout<<"等于"<<endl; cout<<y; cout<<endl;
return 0;}
运行结果:
知识点总结:
重载单目运算符
0 0
- 第九周上机实践项目3——分数类中的运算符重载(续)
- 第九周项目3——分数类中的运算符重载(续)
- 第九周项目3——分数类中的运算符重载(续)
- 第九周项目3——分数中的运算重载符(续)
- 15第九周项目三——分数类中的运算符的重载(续)
- 第九周 课后实践:项目三——分数类中的运算符重载(续)
- 第九周项目三——分数类中的运算符重载(续)
- 第九周--项目3-分数类中的运算符重载
- 第九周项目3(1)-分数类中的运算符重载(续)
- 第九周项目3(2)-分数类中的运算符重载(续)
- 第九周 项目3 分数类中的运算符重载(续)
- 第九周 【项目3-分数类中的运算符重载(续)】
- 第九周【项目3-分数类中的运算符重载(续)】
- 第九周 项目3-分数类中的运算符重载(续)
- 第九周项目3-分数类中的运算符重载(续)
- 第九周项目3--分数类中的运算符重载(续)
- 第九周项目3-分数类中的运算符重载(续)
- 第九周上机项目3 分数类中的运算符重载(续)
- 五角星加强版 c++代码
- Linux Debian grub
- CentOS6.3搭建Nginx代理访问MongoDB GridFS图片资源(已经验证-不错)
- 黑马程序员------------函数
- 获取手机网络模式
- 第九周项目3——分数类中的运算符重载(续)
- java+jsp网页制作总结(3)
- 二维平面曼哈顿距离最小生成树模版
- 利用XIB创建自动以控件的步骤
- 几个重要的ndarray对象属性
- linux下源码安装mysql
- aidl ( 二) 多进程调试
- 黑马程序员------------字符串
- 程序猿必备软件之sublimeText