【c语言】比较两个分数的大小

#include<stdio.h>
int divisor(int a,int b){
    int temp=1;
    
    if(a<b){
        temp=a;
        a=b;
        b=temp;
    }
    while(1){
        temp=a%b;
        if(temp!=0){
        a=b;
        b=temp;
        }else{
            return b;
        }
    }
}

int multiple(int a,int b){
    int m;
    m=divisor(a,b);
    return a*b/m;

}
int compare(int a,int b,int c,int d){
    int t,m,n;
    t=multiple(b,d);
    m=t/b;
    n=t/d;
    return (a*m-c*n);
}
void main(){
    int a,b,c,d,m;
    while(1){
    printf("请输入第一个数的分子和分母:");
    scanf("%d,%d",&a,&b);
    printf(" ");
    printf("请输入第二个数的分子和分母:");
    scanf("%d,%d",&c,&d);
    printf(" ");
    m=compare(a,b,c,d);
    if(m>0){
        printf("a/b大于c/d! ");
    }
    if(m==0){
        printf("a/b等于c/d! ");
    }
    if(m<0){
        printf("a/b小于c/d! ");
    }

    }
}

备注: if(m==0){
        printf("a/b等于c/d! ");
    }这段代码,得注意等号,不是赋值号,不要问我是怎么知道的!!!血的教训!

原文地址:https://www.cnblogs.com/zhizhuniuniu/p/4174679.html