您的位置:首页 > 其它

第十三周项目1.4—分数类中的运算符重载

2016-05-25 21:04 302 查看
/*
*Copyright(c) 2016.烟台大学计算机与控制工程学院
*ALL rights  reserved.
*文件名称:test.cpp
*作者:杨驰
*完成日期:2016年5月25
*问题描述:定义分数类中<<和>>运算符重载,实现分数的输入输出,改造原程序中对
运算结果显示方式,使程序读起来自然。
*/
#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: