【題解】Luogu P1482 【Cantor表(升级版)】

方法楼下的说的很清楚啦qwq

面向对象的写法
写一个封装分数功能的类
以后直接调用即可
一次编写,到处运(tiao)行(shi)
如果c++提供这个类就好了
咱就不必如此模拟

#include<cstdio>

using namespace std;

class Fraction{
private:
    /*
     * @var:den -> 分母
     * @var:num -> 分子 
     */

    int den;
    int num;

    /*
     * @args:int a,int b
     * @des:求a,b的最大公约数 
     */
    int gcd(int a,int b){
        return b == 0 ? a : gcd(b,a % b);
    } 

    /*
     * @des:将分数化简为最简分数 
     */
    void simplify(){
        int MOD = gcd(this -> getNum(),this -> getDen());//求出最大公约数 

        if((double)this -> getNum() / this -> getDen() == 1){//如果分子等于分母,则为1 
            this -> setNum(1);
            this -> setDen(1);
            return;
        }else if(MOD == 1){//如果最简,直接返回 
            this -> setNum(this -> num);
            this -> setDen(this -> den);
            return;
        }else if(MOD != 1){//如果为非最简,则除以gcd 
            this -> setNum(this -> num / MOD);
            this -> setDen(this -> den / MOD);
        }
    }

public:
    /* 
     * @des: 无参构造方法
     */
    Fraction(){
        this -> num = 0;
        this -> den = 1;
    }

    /* 
     * @var: int num,int den 
     * @des: 有参构造方法 
     */
    Fraction(int num,int den){
        if(den != 0){
            this -> num = num;
            this -> den = den;
            this -> simplify();
        }else{
            printf("Unaccept Fraction,zero den\n");
        }
    }

    /* 
     * @des: getter
     */
    int getNum(){
        return this -> num;
    }

    /* 
     * @des: getter
     */
    int getDen(){
        return this -> den;
    }

    /* 
     * @des: setter
     */
    void setNum(int num){
        this -> num = num;
    }

    /* 
     * @des: setter
     */
    void setDen(int den){
        this -> den = den;
    }

    /* 
     * @des: 显示分数 
     */
    void show(){
        if(this -> getDen() < 0){
            this -> setDen(-this -> getDen());
            this -> setNum(-this -> getNum());
        }

        if(this -> den == 1){
            printf("%d",this -> num);
        }else{
            printf("%d/%d",this -> num,this -> den);
        }
    }

    /* 
     * @des: 加法 
     */
    friend Fraction operator + (Fraction &a,Fraction &b){
        return Fraction(a.getNum() * b.getDen() + a.getDen() * b.getNum(),a.getDen() * b.getDen());
    }

    /* 
     * @des: 减法 
     */
    friend Fraction operator - (Fraction &a,Fraction &b){
        return Fraction(a.getNum() * b.getDen() - a.getDen() * b.getNum(),a.getDen() * b.getDen());
    }

    /* 
     * @des: 乘法 
     */
    friend Fraction operator * (Fraction &a,Fraction &b){
        if(a.getNum() == 0 || b.getNum() == 0){
            return Fraction();
        }

        return Fraction(a.getNum() * b.getNum(),a.getDen() * b.getDen());
    }

    /* 
     * @des: 除法 
     */
    friend Fraction operator / (Fraction &a,Fraction &b){
        if(a.getNum() == 0){
            return Fraction();
        }

        if(b.getNum() == 0){
            return Fraction(1,0);
        }

        return Fraction(a.getNum() * b.getDen(),a.getDen() * b.getDen());
    }
};

int main(){
    int aa,bb,cc,d;
    scanf("%d/%d\n%d/%d",&aa,&bb,&cc,&d);

    Fraction a(aa,bb);
    Fraction b(cc,d);

    Fraction c = a * b;

    printf("%d %d",c.getDen(),c.getNum());

    return 0;
}