分数类

    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.abool operator >(const Fen &x) const{
        return x<*this;
    } 
    bool operator <=(const Fen &x){
        return !(x<*this);
    }
    bool operator >=(const Fen &x){
        return !(*thisbool operator !=(const Fen &x){
        return x<*this || *thisbool operator ==(const Fen &x){
        return !(x<*this) && !(*thisoperator +=(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<'/'<return out;
}

你可能感兴趣的:(分数类)