分数类

    int gcd(int x,int y){
        return (x%y==0?y:gcd(y,x%y));
    }   
    int lcm(int x,int y){
        return x/gcd(x,y)*y;
    }
struct Fen{
    int a,b; //分子 分母
    Fen(){
        a=0;b=1;
    }
    Fen(int num){
        *this=num;
    } 
    Fen(pair<int,int> x){
        *this=x;
    }
    Fen(pair<int,int> x){
        *this=x;
    }
    Fen (const Fen &x){
        a=x.a;
        b=x.b;
        check();
    }
    double value(){
        return (double)a/b;
    }
    int check(){//约分
        int p=gcd(a,b);
        a/=p;
        b/=p;
        return 0;
    }
    Fen operator = (int num){
        a=num;
        b=1;
        return *this;
    }
    Fen operator = (pair<int,int> x){
        a=x.first;
        b=x.second;
        check(); 
        return *this;
    } 
    Fen operator = (pair<int,int> x){
        a=x.first;
        b=x.second;
        check(); 
        return *this;
    } 
    Fen operator = (const Fen &x){
        a=x.a;
        b=x.b;
        check();
        return *this;
    }
    Fen operator +(const Fen &x) const{
        Fen y=*this,z=x; 
        int p=lcm(y.b,z.b);
        y.a=y.a*(p/y.b);
        y.b=p;
        z.a=z.a*(p/z.b);
        z.b=p;
        Fen kl=make_pair(y.a+z.a,p);
        return kl;
    } 
    Fen operator -(const Fen &x) const{
        Fen y=*this,z=x; 
        int p=lcm(y.b,z.b);
        y.a=y.a*(p/y.b);
        y.b=p;
        z.a=z.a*(p/z.b);
        z.b=p;
        Fen kl=make_pair(y.a-z.a,p);
        return kl;
    }
    Fen operator *(const Fen &x) const{
        Fen z=make_pair(a*x.a,b*x.b);
        z.check();
        return z;
    }
    Fen operator /(const Fen &x) const{
        Fen z=make_pair(x.b,x.a);
        z*=*this;
        return z; 
    }
    bool operator <(const Fen &x) const{
        Fen y=*this;
        Fen z=x;
        int p=lcm(y.b,z.b);
        y.a=y.a*(p/y.b);
        y.b=p;
        z.a=z.a*(p/z.b);
        z.b=p;
        return y.a<z.a;
    }
    bool operator >(const Fen &x) const{
        return x<*this;
    } 
    bool operator <=(const Fen &x){
        return !(x<*this);
    }
    bool operator >=(const Fen &x){
        return !(*this<x);
    }
    bool operator !=(const Fen &x){
        return x<*this || *this<x;
    }
    bool operator ==(const Fen &x){
        return !(x<*this) && !(*this<x);
    }
    Fen operator +=(const Fen &x){
        *this=*this+x;
        return *this; 
    }
    Fen operator -=(const Fen &x){
        *this=*this-x;
        return *this; 
    }
    Fen operator *=(const Fen &x){
        *this=*this*x;
        return *this; 
    }
    Fen operator /=(const Fen &x){
        *this=*this/x;
        return *this; 
    }
    bool is_right(){//确认分母不为0
        if (b==0) return false;
        else return true;
    }
    Fen clear(){//分数清零
        a=0;
        b=1;
        return *this;
    }
    Fen be_right(){
        if (!is_right()) clear();
        return *this;
    }
};
istream& operator >> (istream &in,Fen &x)//重载IO运算
{
    int l,p;
    in>>l>>p;
    pair<int,int> pl=make_pair(l,p);
    x=pl;
    x.be_right();
    return in;
}
ostream& operator << (ostream &out,const Fen &x){
    out<<x.a<<'/'<<x.b;
    return out;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值