C结构使用结构将有理数减少到最低形式的程序

时间:2018-12-24 15:13:27

标签: c data-structures

    #include <stdio.h>
    typedef struct
    {
        int numerator;
        int denominator;
    }RATIONAL;
    void reduce(RATIONAL *r1,RATIONAL *r2)
    {
        int a,b,rem;
        if(r1->numerator > r1->denominator)
        {
            b=r1->denominator;
            a=r1->numerator;
        }
        else{
            a=r1->denominator;
            b=r1->numerator;
        }
        while(b!=0)
        {
            rem=a%b;
            a=b;
            b=rem;
        }
        r2->numerator/=a;
        r2->denominator/=a;
    }
    int main()
    {
        RATIONAL r1={24,12};
        RATIONAL red;
        reduce(&r1,&red);
        printf("%i/%i",red.numerator,red.denominator);
    }

我得到的答案很模糊,根本与所需的输出不匹配(对于参考输出= 350066/4)。我不明白为什么会这样。请告诉我这段代码有什么问题。 谢谢!

0 个答案:

没有答案