/*
*Copyright (c) 2015,烟台大学计算机学院
*All rights reserved.
*文件名称:CFraction.cpp
*作者:刘天恩
*完成时间:2015年5月13号
*版本号:v1.0
*问题描述:实现分数类中的运算符重载,在分数类中可以完成分数的加减乘除(运算后再化简)、比较(6种关系)的运算
*输入描述:无
*程序输出:输出两个分数加减乘除的结果,比较运算的结果
*/
#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();
friend istream &operator>>(istream &in,CFraction &x);
friend ostream &operator<<(ostream &out,CFraction x);
CFraction operator+(const CFraction &c);
CFraction operator-(const CFraction &c);
CFraction operator*(const CFraction &c);
CFraction operator/(const CFraction &c);
CFraction operator+();
CFraction operator-();
CFraction operator~();
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::simplify()
{
int m,n,r;
n=fabs(deno);
m=fabs(nume);
while(r=m%n) // 求m,n的最大公约数
{
m=n;
n=r;
}
deno/=n;
nume/=n;
if (deno<0)
{
deno=-deno;
nume=-nume;
}
}
istream &operator>>(istream &in,CFraction &x)
{
char ch;
while(1)
{
cin>>x.nume>>ch>>x.deno;
if (x.deno==0)
cerr<<"分母为0, 请重新输入\n";
else if(ch!='/')
cerr<<"格式错误(形如m/n)! 请重新输入\n";
else
break;
}
return cin;
}
ostream &operator<<(ostream &out,CFraction x)
{
cout<<x.nume<<'/'<<x.deno;
return cout;
}
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;
if (!c.nume) return *this;
t.nume=nume*c.deno;
t.deno=deno*c.nume;
t.simplify();
return t;
}
CFraction CFraction:: operator+()
{
return *this;
}
CFraction CFraction:: operator-()
{
CFraction x;
x.nume=-nume;
x.deno=deno;
return x;
}
CFraction CFraction:: operator~()
{
CFraction x;
x.nume=deno;
x.deno=nume;
if(x.deno<0)
{
x.deno=-x.deno;
x.nume=-x.nume;
}
return x;
}
bool CFraction::operator>(const CFraction &c)
{
int this_nume,c_nume,common_deno;
this_nume=nume*c.deno;
c_nume=c.nume*deno;
common_deno=deno*c.deno;
if ((this_nume-c_nume)*common_deno>0) return true;
return false;
}
bool CFraction::operator<(const CFraction &c)
{
int this_nume,c_nume,common_deno;
this_nume=nume*c.deno;
c_nume=c.nume*deno;
common_deno=deno*c.deno;
if ((this_nume-c_nume)*common_deno<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 || *this<c) 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;
}
int main()
{
CFraction x,y,s;
cout<<"输入x: ";
cin>>x;
cout<<"输入y: ";
cin>>y;
s=+x+y;
cout<<"+x+y="<<s<<endl;
s=x-y;
cout<<"x-y="<<s<<endl;
s=x*y;
cout<<"x*y="<<s<<endl;
s=x/y;
cout<<"x/y="<<s<<endl;
cout<<"-x="<<-x<<endl;
cout<<"+x="<<+x<<endl;
cout<<"x的倒数: "<<~x<<endl;
cout<<x;
if (x>y) cout<<"大于";
if (x<y) cout<<"小于";
if (x==y) cout<<"等于";
cout<<y<<endl;
return 0;
}
运算结果: