2015年大一下第6周项目3-分数类中的运算符重载(2)分数与整数的加减乘除

/*
*Copyright (c) 2014,烟台大学计算机学院
*All rights reserved.
*文件名称:Annpion.cpp
*作者:王耀鹏
*完成日期:2015年4月29日
*版本号:v1.0
*
*问题描述:在(1)的基础上,实现分数类中的对象和整型数的四则运算。分数类中的对象可以和整型数进行四则运算,且运算符合交换律。
*输入描述:无。
*输出描述:分数之间和分数与整数之间的加减乘除及比较。
*/
#include<iostream>
#include<cstdlib>
using namespace std;
int gcd(int m, int n);
class CFraction
{
private:
    int nume;  // 分子
    int deno;  // 分母
public:
    CFraction(int nu=0,int de=1);   //构造函数,初始化用
    CFraction operator+(CFraction &c);
    CFraction operator-(CFraction &c);
    CFraction operator*(CFraction &c);
    CFraction operator/(CFraction &c);
    CFraction operator+();
    CFraction operator-();
    bool operator>(CFraction &c);
    bool operator<(CFraction &c);
    bool operator==(CFraction &c);
    bool operator>=(CFraction &c);
    bool operator<=(CFraction &c);
    bool operator!=(CFraction &c);
    friend CFraction operator+(CFraction &c,int i);
    friend CFraction operator+(int i,CFraction &c);
    friend CFraction operator-(CFraction &c,int i);
    friend CFraction operator-(int i,CFraction &c);
    friend CFraction operator*(CFraction &c,int i);
    friend CFraction operator*(int i,CFraction &c);
    friend CFraction operator/(CFraction &c,int i);
    friend CFraction operator/(int i,CFraction &c);
    friend bool operator>(CFraction &c,int i);
    friend bool operator>(int i,CFraction &c);
    friend bool operator<(CFraction &c,int i);
    friend bool operator<(int i,CFraction &c);
    friend bool operator==(CFraction &c,int i);
    friend bool operator==(int i,CFraction &c);
    friend bool operator>=(CFraction &c,int i);
    friend bool operator>=(int i,CFraction &c);
    friend bool operator<=(CFraction &c,int i);
    friend bool operator<=(int i,CFraction &c);
    friend bool operator!=(CFraction &c,int i);
    friend bool operator!=(int i,CFraction &c);
    void display();
    void simplify();
};

CFraction::CFraction(int nu,int de) //构造函数,初始化用
{
    if (de!=0)
    {
        nume=nu;
        deno=de;
    }
    else
    {
        cerr<<"初始化中发生错误,程序退出\n";
        exit(0);
    }
}


// 求m,n的最大公约数
int gcd(int m, int n) //这个函数可以定义为类的成员函数,也可以为一般函数
{
    int r;
    if (m==0)
    {
        return n;
    }
    while(r=m%n)  // 求m,n的最大公约数
    {
        m=n;
        n=r;
    }
    return n;
}
CFraction CFraction::operator+(CFraction &c)
{
    CFraction a((nume*c.deno+c.nume*deno),(deno*c.deno));
    a.simplify();
    return a;
}
CFraction CFraction::operator-(CFraction &c)
{
    CFraction a((nume*c.deno-c.nume*deno),(deno*c.deno));
    a.simplify();
    return a;
}
CFraction CFraction::operator*(CFraction &c)
{
        CFraction a(nume*c.nume,(deno*c.deno));
    a.simplify();
    return a;
}
CFraction CFraction::operator/(CFraction &c)
{
    CFraction a(c.deno,c.nume);
    ((*this)*a).simplify();
    return (*this)*a;
}
CFraction CFraction::operator+()
{
    return *this;
}
CFraction CFraction::operator-()
{
    nume=-nume;
    deno=-deno;
    return *this;
}
bool CFraction::operator>(CFraction &c)
{
            int a=nume*c.deno,b=c.nume*deno;
        return a>b;
}
bool CFraction::operator<(CFraction &c)
{
        int a=nume*c.deno,b=c.nume*deno;
        return a<b;
}
bool CFraction::operator==(CFraction &c)
{
            int a=nume*c.deno,b=c.nume*deno;
    return !((a>b)||(a<b));
}
bool CFraction::operator>=(CFraction &c)
{
    int a=nume*c.deno,b=c.nume*deno;
    return !(a<b);
}
bool CFraction::operator<=(CFraction &c)
{
        int a=nume*c.deno,b=c.nume*deno;
        return !(a>b);
}
bool CFraction::operator!=(CFraction &c)
{
    int a=nume*c.deno,b=c.nume*deno;
    return !(a==b);
}
CFraction operator+(CFraction &c,int i)
{
    CFraction a(c.nume+i*c.deno,c.deno);
    a.simplify();
    return a;
}
CFraction operator+(int i,CFraction &c)
{
    return c+i;
}
CFraction operator-(CFraction &c,int i)
{
    CFraction a(c.nume-i*c.deno,c.deno);
    a.simplify();
    return a;
}
CFraction operator-(int i,CFraction &c)
{
    CFraction a(i*c.deno-c.nume,c.deno);
    a.simplify();
    return a;
}
CFraction operator*(CFraction &c,int i)
{
    CFraction a(i*c.deno,c.deno);
    return c*a;
}
CFraction operator*(int i,CFraction &c)
{
    CFraction a(i*c.deno,c.deno);
    return c*a;
}
CFraction operator/(CFraction &c,int i)
{
    CFraction a(1,i);
    return c*a;
}
CFraction operator/(int i,CFraction &c)
{
    CFraction a(i*c.deno,c.nume);
    a.simplify();
    return a;
}
bool operator>(CFraction &c,int i)
{
    CFraction a(i*c.deno,c.deno);
    return c>a;
}
bool operator>(int i,CFraction &c)
{
    CFraction a(i*c.deno,c.deno);
    return a>c;
}
bool operator<(CFraction &c,int i)
{
    CFraction a(i*c.deno,c.deno);
    return c<a;

}
bool operator<(int i,CFraction &c)
{
    CFraction a(i*c.deno,c.deno);
    return a<c;
}
bool operator==(CFraction &c,int i)
{
    return !(c>i||c<i);
}
bool operator==(int i,CFraction &c)
{
    return !(i>c||i<c);
}
bool operator>=(CFraction &c,int i)
{
    return !(c<i);
}
bool operator>=(int i,CFraction &c)
{
    return !(i<c);
}
bool operator<=(CFraction &c,int i)
{
    return !(c>i);
}
bool operator<=(int i,CFraction &c)
{
    return !(i>c);
}
bool operator!=(CFraction &c,int i)
{
    return !(c==i);
}
bool operator!=(int i,CFraction &c)
{
    return !(i==c);
}
void CFraction::display()
{
    cout<<nume<<"/"<<deno<<endl;
}
void CFraction::simplify()
{
    int d=gcd(nume,deno);

    nume/=d;
    deno/=d;
        if(deno<0)
    {
        nume=-nume;
        deno=-deno;
    }
}

int main()
{
    CFraction x(1,3),y(-5,10),s;
    cout<<"分数x=1/3      y=-5/10"<<endl;
    s=+x+y;
    cout<<"+x+y=";
    s.display();
    s=x-y;
    cout<<"x-y=";
    s.display();
    s=x*y;
    cout<<"x*y=";
    s.display();
    s=x/y;
    cout<<"x/y=";
    s.display();
    s=-x+y;
    cout<<"-x+y=";
    s.display();

    x.display();
    if (x>y) cout<<"大于"<<endl;
    if (x<y) cout<<"小于"<<endl;
    if (x==y) cout<<"等于"<<endl;
    y.display();
    cout<<endl;

    CFraction c1(5,3),c2;
    //以下建议在调试环境中完成测试,
    c2=c1+5;
    cout<<"c1+5=";
    c2.display();
    c2=5+c1;
    cout<<"5+c1=";
    c2.display();

    c2=c1-5;
    cout<<"c1-5=";
    c2.display();
    c2=5-c1;
    cout<<"5-c1=";
    c2.display();

    c2=c1*5;
    cout<<"c1*5=";
    c2.display();
    c2=5*c1;
    cout<<"5*c1=";
    c2.display();

    c2=c1/5;
    cout<<"c1/5=";
    c2.display();
    c2=5/c1;
    cout<<"5/c1=";
    c2.display();

    bool b;

    b=(c1>2);
    {
        if(b)
            cout<<"c1>2"<<endl;
        else cout<<"c1≤2"<<endl;
    }
    b=(2>c1);
    {
        if(b)
            cout<<"2>c1"<<endl;
        else cout<<"2≤c1"<<endl;
    }


    return 0;
}




运行结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值