2015.5.9分数运算符的重载

#include<iostream>
using namespace std;
class CFraction
{
private:
    int nume;
    int deno;
public:
    CFraction(int a=0,int b=0);

    CFraction operator+(const CFraction &a);
    CFraction operator-(const CFraction &a);
    CFraction operator*(const CFraction &a);
    CFraction operator/(const CFraction &a);

    friend CFraction operator+(const CFraction &b,int a);
    friend CFraction operator-(const CFraction &b,int a);
    friend CFraction operator*(const CFraction &b,int a);
    friend CFraction operator/(const CFraction &b,int a);

    friend CFraction operator+(int a,const CFraction &b);
    friend CFraction operator-(int a,const CFraction &b);
    friend CFraction operator*(int a,const CFraction &b);
    friend CFraction operator/(int a,const CFraction &b);

    friend int multiple(const CFraction &a,const CFraction &b);
    void sort_CFraction();
    void show_CFraction();

};
CFraction::CFraction(int a,int b)    //
{
    nume=a;
    deno=b;
}

CFraction CFraction::operator+(const CFraction &a)
{
    CFraction b;
    int c;
    c=multiple(*this,a);
    b.nume=nume*(c/deno)+a.nume*(c/a.deno);
    b.deno=c;
    b.sort_CFraction();
    return b;
}
CFraction CFraction::operator-(const CFraction &a)
{
    CFraction b;
    int c=multiple(*this,a);
    b.nume=nume*(c/deno)-a.nume*(c/a.deno);
    b.deno=c;
    b.sort_CFraction();
    return b;
}
CFraction CFraction::operator*(const CFraction &a)
{
    CFraction b;
    b.nume=nume*a.nume;
    b.deno=deno*a.deno;
    b.sort_CFraction();
    return b;
}
CFraction CFraction::operator/(const CFraction &a)
{
    CFraction b;
    b.nume=nume*a.deno;
    b.deno=deno*a.nume;
    b.sort_CFraction();
    return b;
}



CFraction operator+(const CFraction &b,int a)
{
    CFraction c;
    c.nume=b.nume+a*b.deno;
    c.deno=b.deno;
    c.sort_CFraction();
    return c;
}
CFraction operator-(const CFraction &b,int a)
{
    CFraction c;
    c.nume=b.nume-a*b.deno;
    c.deno=b.deno;
    c.sort_CFraction();
    return  c;
}
CFraction operator*(const CFraction &b,int a)
{
    CFraction c;
    c.nume=b.nume*a;
    c.deno=b.deno;
    c.sort_CFraction();
    return c;
}
CFraction operator/(const CFraction &b,int a)
{
    CFraction c;
    c.nume=b.nume;
    c.deno=b.deno*a;
    c.sort_CFraction();
    return c;
}





CFraction operator+(int a,const CFraction &b)
{
    CFraction c;
    c.nume=b.nume+a*b.deno;
    c.deno=b.deno;
    c.sort_CFraction();
    return c;
}
CFraction operator-(int a,const CFraction &b)
{
    CFraction c;
    c.nume=a*b.deno-b.nume;
    c.deno=b.deno;
    c.sort_CFraction();
    return c;
}
CFraction operator*(int a,const CFraction &b)
{
    CFraction c;
    c.nume=b.nume*a;
    c.deno=b.deno;
    c.sort_CFraction();
    return c;
}
CFraction operator/(int a,const CFraction &b)
{
    CFraction c;
    c.nume=b.deno*a;
    c.deno=b.nume;
    c.sort_CFraction();
    return c;
}



int multiple(const CFraction &a ,const CFraction &b)
{
    int c,d,temp,e;
    c=a.deno;
    d=b.deno;
    while(d!=0)
    {
        temp=c%d;
        c=d;
        d=temp;
    }
    e=a.deno*b.deno/c;
    return e;
}
void CFraction::sort_CFraction()
{
    int a,b,temp;
    a=nume;
    b=deno;
    while(b!=0)
    {
        temp=a%b;
        a=b;
        b=temp;
    }
    nume/=a;
    deno/=a;
    if(nume>0&&deno<0)
    {
        nume=-nume;
        deno=-deno;
    }
}
void CFraction::show_CFraction()
{
    CFraction::sort_CFraction();
    if(deno==1||nume==0)
        cout<<nume<<endl;
    else
        cout<<nume<<'/'<<deno<<endl;
}
int main()
{
    CFraction  a(-3,-4),b(4,5),d;
    int c=6;
    a.show_CFraction();
    d=a+b;
    d.show_CFraction();
    d=a-b;
    d.show_CFraction();
    d=a*b;
    d.show_CFraction();
    d=a/b;
    d.show_CFraction();

    d=a+c;
    d.show_CFraction();
    d=a-c;
    d.show_CFraction();
    d=a*c;
    d.show_CFraction();
    d=a/c;
    d.show_CFraction();

    d=c+a;
    d.show_CFraction();
    d=c-a;
    d.show_CFraction();
    d=c*a;
    d.show_CFraction();
    d=c/a;
    d.show_CFraction();
    return 0;

}
<img src="https://img-blog.csdn.net/20150509112051709?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2xpdXpoaWxpbw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值