第9周项目3 分数类中的运算符重载(续)

/*
*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;
}


运行结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值