第十三周项目1.4—分数类中的运算符重载
2016-06-06 21:30
260 查看
问题及代码:
/*
*Copyright(c) 2016.烟台大学计算机与控制工程学院
*ALL rights reserved.
*文件名称:main.cpp
*作者:郝昱猛
*完成日期:2016年6月6日
*问题描述:定义分数类中<<和>>运算符重载,实现分数的输入输出,改造原程序中对
运算结果显示方式,使程序读起来自然。
*/
#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)
{
cin>>x.nume>>c>>x.deno;
if(x.deno==0)
cerr<<"分母为0,请重新输入\n";
else if(c!='/')
cerr<<"格式错误!请重新输入\n";
else
break;
}
return cin;
}
ostream &operator<<(ostream &out,CFraction &x)
{
cout<<x.nume<<'/'<<x.deno;
return cout;
}
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;
t.nume=nume*a;
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+()
{
return *this;
}
CFraction CFraction::operator-()
{
CFraction t;
t.nume=-nume;
t.deno=deno;
return t;
}
CFraction CFraction::operator~()
{
CFraction t;
t.nume=deno;
t.deno=nume;
if(t.deno<0)//保证负分数的负号在分子上
{
t.deno=-t.deno;
t.nume=-t.nume;
}
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;
if(x==y)
cout<<"等于!"<<endl;
return 0;
}
运行结果:
/*
*Copyright(c) 2016.烟台大学计算机与控制工程学院
*ALL rights reserved.
*文件名称:main.cpp
*作者:郝昱猛
*完成日期:2016年6月6日
*问题描述:定义分数类中<<和>>运算符重载,实现分数的输入输出,改造原程序中对
运算结果显示方式,使程序读起来自然。
*/
#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)
{
cin>>x.nume>>c>>x.deno;
if(x.deno==0)
cerr<<"分母为0,请重新输入\n";
else if(c!='/')
cerr<<"格式错误!请重新输入\n";
else
break;
}
return cin;
}
ostream &operator<<(ostream &out,CFraction &x)
{
cout<<x.nume<<'/'<<x.deno;
return cout;
}
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;
t.nume=nume*a;
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+()
{
return *this;
}
CFraction CFraction::operator-()
{
CFraction t;
t.nume=-nume;
t.deno=deno;
return t;
}
CFraction CFraction::operator~()
{
CFraction t;
t.nume=deno;
t.deno=nume;
if(t.deno<0)//保证负分数的负号在分子上
{
t.deno=-t.deno;
t.nume=-t.nume;
}
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;
if(x==y)
cout<<"等于!"<<endl;
return 0;
}
运行结果:
相关文章推荐
- 论文测试
- 手机开发实战76——端口介绍2
- 每天一道算法题——亮着电灯的盏数
- CJSON源码研究笔记
- Spring下载 ---- Spring_0
- c++作业7
- 6/6 Sprint2 看板和燃尽图
- 值类型与引用类型的传值
- redis学习笔记——RDB和AOF持久化二
- 手机开发实战75——端口介绍1
- 堆与并差集
- 环形缓存(C#版)
- redis学习笔记——RDB和AOF持久化一
- 第二阶段个人总结10
- redis学习笔记——Redis过期键的删除策略
- android showAsDropDown的用法属性介绍
- Ubuntu下安装Redis
- opencv cvLoad 运行出错 cv::exception 出错
- smarty 变量调节器
- 封装练习