第十三周上机实践项目1(2):分数类中的运算符重载
来源:互联网 发布:摩托车违章查询软件 编辑:程序博客网 时间:2024/05/16 15:44
/* *Copyright(c) 2016.烟台大学计算机与控制工程学院 *ALL rights reserved. *文件名称:app.cpp *作者:赵子琳 *完成日期:2016年6月9日 *问题描述:在(1)的基础上,实现分数类中的对象和整型数的四则运算。 *分数类中的对象可以和整型数进行四则运算,且运算符合交换律。 *例如:CFraction a(1,3),b; int i=2; 可以完成b=a+i;。同样,可以完成i+a, 45+a, a*27, 5/a等各种运算。 */#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(); 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); 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::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*a; t.nume=nume; t,simplify(); return t;}CFraction CFraction::operator/(int a){ CFraction t; t.deno=deno*a; t.nume=nume; t.simplify(); 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; else cout<<"等于!"<<endl; return 0;}
0 0
- 第十三周上机实践项目1(2):分数类中的运算符重载
- 第十三周上机实践项目1(1):分数类中的运算符重载
- 第十三周上机实践项目1(4):分数类中的运算符重载
- 第十三周上机实践项目 项目1--分数类中的运算符重载
- 第十三周上机实践——项目1-分数类中的运算符重载
- 第十三周上机实践项目1(3):分数中的运算符重载
- 第十三周上机时间项目——项目1-分数类中的运算符重载
- 第八周上机实践项目3——分数类中的运算符重载(2)
- 第十三周实践项目1-分数类中的运算符重载
- 第十三周项目 1分数类中的运算符重载
- 第十三周【项目 1分数类中的运算符重载】
- 第十三周项目-项目1-(2)-分数类中的运算符重载
- 第九周上机实践项目3——分数类中的运算符重载(续)
- 第八周上机实践项目3-分数类中的运算符重载(分数与分数、分数与整数)
- 第十三周实践项目1————分数类运算符重载(3)
- 第十三周实践项目1————分数类运算符重载(4)
- 第八周上机实践项目3——分数类的运算符重载(1)
- 第九周上机实践项目3-分数类中的运算符重载(续 重载输入输出流)
- 第十五周--程序填空
- GDB 调试 Nginx 磨刀不误砍柴工
- 13周 项目二--动物这样叫(1)
- 阅读程序(9)
- 第15周-项目2-洗牌
- 第十三周上机实践项目1(2):分数类中的运算符重载
- 第八周项目三用对象数组操作长方柱类
- IMF Vmvare 中linux 根分区磁盘扩容 gparted 增加虚拟机磁盘空间
- 第十三周上机实践项目 项目2--动物这样叫(2)
- 十一周阅读程序5-b
- 第十二周上机时间项目——项目2-Time类中的运算符重载
- jquery请求格式和返回类型 汇总
- 欢迎使用CSDN-markdown编辑器
- C#中 多态的作用