第13周项目1:分数类中的运算符重载(4)
2016-06-01 17:44
309 查看
Copyright(c)2016,烟台大学计算机与控制工程学院 All rights reserved. 文件名称:第13周项目1:分数类中的运算符重载(4) 作者:于子娴 完成日期:2016.6.1 版本号:v1.0 问题描述:定义分数类中和运算符重载,实现分数的输入输出,改造原程序中对 运算结果显示方式,使程序读起来自然。 输入描述: 程序输出: #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) { cinx.numecx.deno; if(x.deno==0) cerr分母为0,请重新输入n; else if(c!='') cerr格式错误!请重新输入n; else break; } return cin; } ostream &operator(ostream &out,CFraction &x) { coutx.nume''x.deno; return cout; } void CFractiondisplay() { coutnumedenoendl; } 分数简化 void CFractionsimplify() { 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(deno0) { deno=-deno; nume=-nume; } } CFraction CFractionoperator+(const CFraction &c) { CFraction t; t.nume=numec.deno+c.numedeno; t.deno=denoc.deno; t.simplify(); return t; } CFraction CFractionoperator-(const CFraction &c) { CFraction t; t.nume=numec.deno-c.numedeno; t.deno=denoc.deno; t.simplify(); return t; } CFraction CFractionoperator(const CFraction &c) { CFraction t; t.nume=numec.nume; t.deno=denoc.deno; t.simplify(); return t; } CFraction CFractionoperator(const CFraction &c) { CFraction t; t.nume=numec.deno; t.deno=denoc.nume; t.simplify(); return t; } CFraction CFractionoperator+(int a) { CFraction t; int c; c=adeno; t.deno=deno; t.nume=nume+c; t.simplify(); return t; } CFraction CFractionoperator-(int a) { CFraction t; int c; c=adeno; t.deno=deno; t.nume=nume-c; t.simplify(); return t; } CFraction CFractionoperator(int a) { CFraction t; t.deno=deno; t.nume=numea; t.simplify(); return t; } CFraction CFractionoperator(int a) { CFraction t; t.deno=denoa; t.nume=nume; t.simplify(); return t; } CFraction CFractionoperator+() { return this; } CFraction CFractionoperator-() { CFraction t; t.nume=-nume; t.deno=deno; return t; } CFraction CFractionoperator~() { CFraction t; t.nume=deno; t.deno=nume; if(t.deno0)保证负分数的负号在分子上 { t.deno=-t.deno; t.nume=-t.nume; } return t; } bool CFractionoperator(const CFraction &c) { int n1,d1; n1=numec.deno-c.numedeno; d1=denoc.deno; if(n1d10) return true; return false; } bool CFractionoperator(const CFraction &c) { int n1,d1; n1=numec.deno-c.numedeno; d1=denoc.deno; if(n1d10) return true; return false; } bool CFractionoperator==(const CFraction &c) { if(this!=c) return false; return true; } bool CFractionoperator!=(const CFraction &c) { if(this==c) return false; return true; } bool CFractionoperator=(const CFraction &c) { if(thisc) return false; return true; } bool CFractionoperator=(const CFraction &c) { if(thisc) return false; return true; } int main() { char c; int n,d; cout输入分数的样例:xyendl; cinncd; CFraction x(n,d); x.display(); cinncd; CFraction y(n,d); y.display(); CFraction s; s=x+y; coutx+y=; s.display(); coutendl; s=x-y; coutx-y=; s.display(); coutendl; s=xy; coutxy=; s.display(); coutendl; s=xy; coutxy=; s.display(); coutendl; cout请输入要进行运算的整数dendl; cind; s=x+d; coutx+d=; s.display(); coutendl; s=x-d; coutx-d=; s.display(); coutendl; s=xd; coutxd=; s.display(); coutendl; s=xd; coutxd=; s.display(); coutendl; if(xy) cout大于!endl; if(xy) cout小于! endl; if(x==y) cout等于!endl; return 0; }
运行结果:
相关文章推荐
- 第10章 C语言及程序设计提高例程-37 操作字符串数组
- Android 自定义 Application 多次调用
- 【HDU 1588】Gauss Fibonacci(矩阵快速幂+二分)
- android中判断EditText的内容是否为空
- HTTP的请求方法OPTIONS
- 生活协奏曲
- 什么是EPEL 及 Centos上安装EPEL(转)
- nginx+tomcat集群负载均衡(实现session复制)
- js中的prototype
- 从源代码看 ObjC 中消息的发送
- springmvc+ibatis 项目命名规范
- Activity生命周期管理~ 如何开始Activity
- ubuntu下100%成功安装torch,同时配置cuda和cudnn(不成功,则成仁)
- 集合四_Map_HashMap_TreeMap
- jQWidgets的TreeGrid 心得:
- mysql 入门基础(2)—外键、主键和索引
- 自定义Progress样式(原生progress方式实现)
- ccah-500 第10题 Which best describes how you determine when the last checkpoint happened
- HDU 1166 敌兵布阵 (线段树点更新/模板)
- 打飞机代码