/*
*Copyright (c)2015,烟台大学计算机与控制工程学院
*All rights reserved.
*文件名称:score.cpp
*作 者:惠睿
*完成日期:2015年5月18日
*版 本 号:v1.0
*
*问题描述:在第八周项目的基础上,加入要求的运算符重载定义。
*程序输入:无输入。
*程序输出:输出调用函数后的值。
*/
#include <iostream>
#include <cmath>
using namespace std;
class CFraction
{
private:
int nume; // 分子
int deno; // 分母
public:
CFraction(int nu=0,int de=1); //构造函数,初始化用
void set(int nu=0,int de=1); //置值,改变值时用
void input(); //按照"nu/de"的格式,如"5/2"的形式输入
void simplify(); //化简(使分子分母没有公因子)
void display();
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 + (const CFraction &c2);
CFraction operator - (const CFraction &c2);
CFraction operator * (const CFraction &c2);
CFraction operator / (const CFraction &c2);
CFraction operator+();//分数取正
CFraction operator-();//分数求反
friend istream& operator>>(istream&input,CFraction &c);
friend ostream& operator<<(ostream&output,CFraction &c);
};
int gys(int a,int b)
{
return (a%b!=0?(gys(b,a%b)):b);
}
int gbs(int u,int v)
{
int h;
h=gys(u,v);
return (u*v/h);
}
CFraction::CFraction(int nu,int de)
{
if(de!=0)
{
nume=nu;
deno=de;
}
}
void CFraction::set(int nu,int de)//置值,改变值时用
{
if(de!=0)
{
nume=nu;
deno=de;
}
}
bool CFraction::operator > (CFraction &c)
{
int r;
if (deno!=c.deno) //将分数化成相同分母便于比较
{
r=gbs(deno,c.deno);
if(nume*r/deno>c.nume*r/c.deno)
return true;
else
return false;
}
else
{
if(nume>c.nume)
return true;
else
return false;
}
}
bool CFraction::operator < (CFraction &c)
{
int r;
if (deno!=c.deno) //将分数化成相同分母便于比较
{
r=gbs(deno,c.deno);
if(nume*r/deno<c.nume*r/c.deno)
return true;
else
return false;
}
else
{
if(nume<c.nume)
return true;
else
return false;
}
}
bool CFraction::operator >= (CFraction &c)
{
return !(*this < c);
}
bool CFraction::operator <= (CFraction &c)
{
return !(*this > c);
}
bool CFraction::operator == (CFraction &c)
{
return !((*this > c)||(*this < c));
}
bool CFraction::operator != (CFraction &c)
{
return ((*this > c)||(*this < c));
}
CFraction CFraction::operator + (const CFraction &c2)
{
CFraction c;
int r;
if (deno!=c.deno) //取分母的最大公倍数
{
r=gbs(deno,c2.deno);
c.nume=(nume*r/deno)+(c2.nume*r/c2.deno);
c.deno=r;
}
else
{
c.nume=nume+c2.nume;
c.deno=deno;
}
return c;
}
CFraction CFraction::operator - (const CFraction &c2)
{
CFraction c;
int r;
if (deno!=c.deno) //取分母的最大公倍数
{
r=gbs(deno,c2.deno);
c.nume=(nume*r/deno)-(c2.nume*r/c2.deno);
c.deno=r;
}
else
{
c.nume=nume-c2.nume;
c.deno=deno;
}
return c;
}
CFraction CFraction::operator * (const CFraction &c2)
{
CFraction c;
c.nume=nume*c2.nume;
c.deno=deno*c2.deno;
return c;
}
CFraction CFraction::operator / (const CFraction &c2)
{
CFraction c;
c.nume=nume*c2.deno;
c.deno=deno*c2.nume;
return c;
}
void CFraction::simplify()//化简(使分子分母没有公因子)
{
int r;
r=gys(nume,deno);
nume/=r;
deno/=r;
}
//流插入
istream& operator>>(istream&input,CFraction &c)
{
int a,b;
char sign;
do
{
cout<<"please input a fraction(a/b):";
input>>a>>sign>>b;
}
while(!(sign=='/'));
c.nume=a;
c.deno=b;
return input;
}
//流提取
ostream& operator<<(ostream&output,CFraction &c)
{
c.simplify();
if(c.deno!=1)
output<<c.nume<<"/"<<c.deno;
else
output<<c.nume;
return output;
}
CFraction CFraction::operator+()//分数取正
{
return CFraction(fabs(nume),fabs(deno));
}
CFraction CFraction::operator-()//分数求反
{
return CFraction(-fabs(nume),fabs(deno));
}
int main()
{
CFraction x,y,s;
cin>>x>>y;
cout<<"两个分数是:"<<x<<'\t'<<y<<endl;
cout<<"下面比较两个分数的大小:"<<endl;
if (x>y)
cout<<x<<">"<<y<<endl;
if (x<y)
cout<<x<<"<"<<y<<endl;
if (x==y)
cout<<x<<"="<<y<<endl;
if (x!=y)
cout<<x<<"≠"<<y<<endl;
if (x>=y)
cout<<x<<"≥"<<y<<endl;
if (x<=y)
cout<<x<<"≤"<<y<<endl;
cout<<"下面对两个分数进行计算:"<<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;
s=x/y;
cout<<"x/y="<<s<<endl;
cout<<"下面对两个分数进行取正和求反:"<<endl;
s=(+x);
cout<<"+x="<<s<<endl;
s=(-y);
cout<<"-y="<<s<<endl;
return 0;
}
运行结果: