C语言编程分数比较(10分)题目内容:本题要求编写程序,比较两个分数的大小。输入格式:输入在一行中按照“a1/b1 a2

1个回答

  • 得到如下代码:#include

    int gcd(int a, int b)

    {

    return b == 0 ? a : gcd (b, a%b);

    }

    int lcm(int a, int b)

    {

    return a/gcd(a, b)*b;

    }

    int main()

    {

    int a1, b1, a2, b2, lb;

    while (scanf ("%d/%d %d/%d", a1, b1, a2, b2) != EOF){

    lb = lcm(b1, b2);

    if (lb/b1*a1 > lb/b2*a2){

    printf ("%d/%d > %d/%d", a1, b1, a2, b2);

    }

    else if (lb/b1*a1 < lb/b2*a2){

    printf ("%d/%d < %d/%d", a1, b1, a2, b2);

    }

    else {

    printf ("%d/%d = %d/%d", a1, b1, a2, b2);

    }

    }

    }