定义分数类中<<和>>运算符重载,实现分数的输入输出,改造原程序中对运算结果显示方式,使程序读起来更自然。
来源:互联网 发布:c语言中使用thumb指令 编辑:程序博客网 时间:2024/04/28 03:25
- /*
- *Copyright (c) 2016,烟台大学计算机学院
- *All rights reserved.
- *文件名称:main.cpp
- *作 者:李德彪
- *完成时间:2016年6月15日
- *版 本 号:v1.0
- *
- *问题描述:定义分数类中<<和>>运算符重载,实现分数的输入输出,改造原程序中对运算结果显示方式,使程序读起来更自然。
- *输入描述:无。
- *程序输出:分数的各种形式。
- */
- #include<iostream>
- using namespace std;
- class CFraction
- {
- private:
- int nume;//fenzi
- int deno;//fenmu
- public:
- CFraction(int a=0,int b=0);
- void show();
- friend istream &operator>>(istream &in,CFraction &x);
- friend ostream &operator<<(ostream &out,CFraction x);
- CFraction operator+(double c);
- CFraction operator-(double c);
- CFraction operator*(double c);
- CFraction operator/(double c);
- bool operator>(CFraction &c);
- bool operator<(CFraction &c);
- bool operator==(CFraction &c);
- bool operator>=(CFraction &c);
- bool operator<=(CFraction &c);
- bool operator!=(CFraction &c);
- CFraction operator+();
- CFraction operator-();
- CFraction operator~();
- };
- istream &operator>>(istream &in,CFraction &x)
- {
- char c;
- for(;;)
- {
- cin>>x.nume>>c>>x.deno;
- if (x.deno==0)
- cerr<<"分母为0, 请重新输入\n";
- else if(c!='/')
- cerr<<"格式错误(形如m/n)! 请重新输入\n";
- else
- break;
- }
- return cin;
- }
- ostream &operator<<(ostream &out,CFraction x)
- {
- cout<<x.nume<<'/'<<x.deno;
- return cout;
- }
- CFraction CFraction::operator~()
- {
- int a=nume,b=deno,t;
- t=a;
- a=b;
- b=t;
- CFraction c(a,b);
- return c;
- }
- CFraction CFraction::operator+()
- {
- return *this;
- }
- CFraction CFraction::operator-()
- {
- int mu,zi;
- mu=deno;
- zi=-nume;
- CFraction t(zi,mu);
- return t;
- }
- bool CFraction::operator>(CFraction &c)
- {
- int mu,zi1,zi2;
- mu=deno*c.deno;
- zi1=nume*c.deno;
- zi2=c.nume*deno;
- if(zi1>zi2)
- return true;
- else
- return false;
- }
- bool CFraction::operator<(CFraction &c)
- {
- int mu,zi1,zi2;
- mu=deno*c.deno;
- zi1=nume*c.deno;
- zi2=c.nume*deno;
- if(zi1< zi2)
- return true;
- else
- return false;
- }
- bool CFraction::operator==(CFraction &c)
- {
- int mu,zi1,zi2;
- mu=deno*c.deno;
- zi1=nume*c.deno;
- zi2=c.nume*deno;
- if(zi1==zi2)
- return true;
- else
- return false;
- }
- bool CFraction::operator<=(CFraction &c)
- {
- int mu,zi1,zi2;
- mu=deno*c.deno;
- zi1=nume*c.deno;
- zi2=c.nume*deno;
- if(zi1<=zi2)
- return true;
- else
- return false;
- }
- bool CFraction::operator>=(CFraction &c)
- {
- int mu,zi1,zi2;
- mu=deno*c.deno;
- zi1=nume*c.deno;
- zi2=c.nume*deno;
- if(zi1>=zi2)
- return true;
- else
- return false;
- }
- bool CFraction::operator!=(CFraction &c)
- {
- int mu,zi1,zi2;
- mu=deno*c.deno;
- zi1=nume*c.deno;
- zi2=c.nume*deno;
- if(zi1!=zi2)
- return true;
- else
- return false;
- }
- CFraction::CFraction(int a,int b)
- {
- nume=a;
- deno=b;
- }
- CFraction CFraction::operator+(double c)
- {
- int zi;
- zi=nume+c*deno;
- CFraction t(zi,deno);
- return t;
- }
- CFraction CFraction::operator-(double c)
- {
- int zi;
- zi=nume-c*deno;
- CFraction t(zi,deno);
- return t;
- }
- CFraction CFraction::operator*(double c)
- {
- int zi;
- zi=nume*c;
- CFraction t(zi,deno);
- return t;
- }
- CFraction CFraction::operator/(double c)
- {
- int mu;
- mu=deno*c;
- CFraction t(nume,mu);
- return t;
- }
- void CFraction::show()
- {int t,m,r,n;
- m=deno;
- n=nume;
- if(deno<nume)
- {
- t=m;
- m=n;
- n=t;
- }
- while(r=m%n)
- {
- m=n;
- n=r;
- }
- deno=deno/n;
- nume=nume/n;
- if(deno==1)
- cout<<nume<<endl;
- else
- cout<<nume<<"/"<<deno<<endl;
- }
- int main()
- {
- CFraction c1(1,2),c2(2,3),c3;
- c3=c1+2;
- c3.show();
- c3=c1-2;
- c3.show();
- c3=c1*2;
- c3.show();
- c3=c1/2;
- c3.show();
- if(c1>c2)
- cout<<"c1>c2"<<endl;
- if(c1<c2)
- cout<<"c1<c2"<<endl;
- if(c1==c2)
- cout<<"c1==c2"<<endl;
- if(c1>=c2)
- cout<<"c1>=c2"<<endl;
- if(c1<=c2)
- cout<<"c1<=c2"<<endl;
- if(c1!=c2)
- cout<<"c1!=c2"<<endl;
- cout<<"-c1="<<-c1<<endl;
- cout<<"+c1="<<+c1<<endl;
- cout<<"c1的倒数: "<<~c1<<endl;
- return 0;
- }
0 0
- 定义分数类中<<和>>运算符重载,实现分数的输入输出,改造原程序中对运算结果显示方式,使程序读起来更自然。
- 定义分数类中<<和>>运算符重载,实现分数的输入输出,改造原程序中对运算结果显示方式,使程序读起来更自然。
- 定义分数类中<<和>>运算符重载,实现分数的输入输出,改造原程序中对运算结果显示方式,使程序读起来更自然。
- 第九周实验报告任务3 定义分数类中<<和>>运算符重载,实现分数的输入输出,改造原程序中对运算结果显示方式,使程序读起来更自然。
- 《第九周任务三》定义分数类中《和》运算符重载,实现分数的输入输出,改造源程序中对运算结果显示方式,使程序读起来更自然
- 《第九周任务三》定义分数类中《和》运算符重载,实现分数的输入输出,改造源程序中对运算结果显示方式,使程序读起来更自然
- 第九周实验报告任务 1 定义Time类中的<<和>>运算符重载,实现时间的输入输出,改造原程序中对运算结果显示方式,使程序读起来更自然。
- 第九周实验报告 任务2 定义Complex类中的<<和>>运算符的重载,实现输入和输出,改造原程序中对运算结果显示方式,使程序读起来更自然。
- 《第九周任务一》定义Complex类中的《《和》》运算符的重载,实现输入和输出,改造原程序中对运算结果显示方式,使程序读起来更自然
- 《第九周任务二》定义Time类中的<<和>>运算符的重载,实现时间输入输出,改造源程序中对运算结果的显示方式,使程序读起来更自然
- 第九周任务三--定义分数类中<<和>>运算符重载,实现分数的输入输出,使程序读起来更自然。
- 第九周【项目1】在第8周项1的基础上(1)再定义一目运算符 -,-c相当于0-c(2)定义Complex类中的<<和>>运算符的重载,实现输入和输出,改造原程序中对运算结果显示方式,使程序读起来更自然
- 第九周C++任务三。定义分数类中<<和>>运算符重载,实现分数的输入输出
- 第九周任务三之定义分数类中<<和>>运算符重载,实现分数的输入输出
- C++第九周【任务三】定义分数类中<<和>>运算符重载,实现分数的输入输出
- 《C++第九周实验报告3-1》----接第8周任务3,定义分数类中<<和>>运算符重载,实现分数的输入输出
- 第九周实验任务二--定义Complex类中的<<和>>运算符的重载,实现输入和输出,使程序读起来更自然
- 第九周 任务2 定义TIME类中的《和》运算重载,实现分数的输入输出。
- session
- eclipse中文变乱码原因及解决办法
- swift单例的创建与使用
- linux kernel的中断子系统之(九):tasklet
- Android 下陀螺仪、加速度和磁场传感器数据定义
- 定义分数类中<<和>>运算符重载,实现分数的输入输出,改造原程序中对运算结果显示方式,使程序读起来更自然。
- ViewPager
- 回调方法介绍之中国好室友篇(Java示例)
- 14.3.2.3 Consistent Nonlocking Reads 一致性非锁定读
- 如何修改NSNotificationCenter addObsever方法的实现
- PyCharm中使用virtualenv进行django开发
- 数据结构与算法:排序
- appium自动测试化相关
- mybatis和hibernate创建会话的简单区别