#include <iostream>
#include <cmath>
#include <cstring>
#include <iomanip>
using namespace std;

int gcd(int x,int y)
{
    x=abs(x);
    y=abs(y);
    if(x<y)
    {
        int t=x;
        x=y;
        y=t;
    }
    if(x%y==0)
        return y;
    else
        return gcd(x%y,y);
}

class Rational
{
private:
    int z;    //分子
    int m;    //分母
public:
    Rational(int a=0, int b=1)//构造有理数分数,分子默认为0,分母默认为1
    {
        z=a;
        m=b;
    }
    friend Rational& yuefen(Rational& r)//约分函数对分数化简
    {
        int c;
        c=gcd(r.z,r.m);
        r.z=r.z/c;
        r.m=r.m/c;
        if(r.z>0&&r.m<0)
        {
            r.z=0-r.z;
            r.m=0-r.m;
        }
        if(r.z<0&&r.m<0)
        {
            r.z=abs(r.z);
            r.m=abs(r.m);
        }
        return r;
    }
    friend Rational operator+(const Rational &r1, const Rational &r2)
    {
        Rational t;
        t.m=r1.m*r2.m;
        t.z=r1.z*r2.m+r2.z*r1.m;
        return t;
    }
    friend Rational operator-(const Rational &r1, const Rational &r2)
    {
        Rational t;
        t.m=r1.m*r2.m;
        t.z=r1.z*r2.m-r2.z*r1.m;
        return t;
    }
    friend Rational operator*(const Rational &r1, const Rational &r2)
    {
        Rational t;
        t.m=r1.m*r2.m;
        t.z=r1.z*r2.z;
        return t;
    }
    friend Rational operator/(const Rational &r1, const Rational &r2)
    {
        Rational t;
        t.m=r1.m*r2.z;
        t.z=r1.z*r2.m;
        return t;
    }
    Rational & operator+=(const Rational &r)
    {
        Rational t;
        t.m=this->m*r.m;
        t.z=this->z*r.m+r.z*this->m;
        this->z=t.z;
        this->m=t.m;
        return *this;
    }
    Rational & operator-=(const Rational &r)
    {
        Rational t;
        t.m=this->m*r.m;
        t.z=this->z*r.m-r.z*this->m;
        this->m=t.m;
        this->z=t.z;
        return *this;
    }
    Rational & operator*=(const Rational &r)
    {
        this->m=this->m*r.m;
        this->z=this->z*r.z;
        return *this;
    }
    Rational & operator/=(const Rational &r)
    {
        Rational t;
        t.m=this->m*r.z;
        t.z=this->z*r.m;
        this->m=t.m;
        this->z=t.z;
        return *this;
    }
    friend bool operator==(const Rational &s1, const Rational &s2)//判断两个有理数是否相等
    {
        int m1,m2,z1,z2,t1,t2;
        t1=gcd(s1.z,s1.m);
        t2=gcd(s2.z,s2.m);
        m1=s1.m/t1;
        m2=s2.m/t2;
        z1=s1.z/t1;
        z2=s1.z/t2;
        if(m1==m2&&z1==z2)
            return 1;
        else
            return 0;
    }
    friend bool operator!=(const Rational &s1, const Rational &s2)//判断两个有理数是否不等
    {
        int m1,m2,z1,z2,t1,t2;
        t1=gcd(s1.z,s1.m);
        t2=gcd(s2.z,s2.m);
        m1=s1.m/t1;
        m2=s2.m/t2;
        z1=s1.z/t1;
        z2=s1.z/t2;
        if(m1==m2&&z1==z2)
            return 0;
        else
            return 1;
    }
    friend ostream & operator<<(ostream &t1, const Rational &t2)
    {
        t1<<t2.z<<"/"<<t2.m;
        return t1;
    }
    friend istream & operator>>(istream &t1, Rational &t2)
    {
        t1>>t2.z>>t2.m;
        return t1;
    }
};

int main()
{
    Rational r1,r2,r3;
    while(cin>>r1>>r2)
    {
        cout << "r1 = " << yuefen(r1) << "\n" << "r2 = " << yuefen(r2) << endl;
        r3 = r1 + r2;
        cout << "r1+r2 = " << yuefen(r3) << endl;
        r3 = r1 - r2;
        cout << "r1-r2 = " << yuefen(r3) << endl;
        r3 = r1 * r2;
        cout << "r1*r2 = " << yuefen(r3) << endl;
        r3 = r1 / r2;
        cout << "r1/r2 = " << yuefen(r3) << endl;
        cout << (r1 == r2) << " " << (r1 != r2) << endl;
        cout << yuefen(r1 += r2) << endl;
        cout << yuefen(r1 -= r2) << endl;
        cout << yuefen(r1 *= r2) << endl;
        cout << yuefen(r1 /= r2) << endl;
    }
    return 0;
    
}

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。