码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2014-12-19 20:37:11      阅读:265      评论:0      收藏:0      [点我收藏+]

标签:

#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("\n");
    printf("请输入第二个数的分子和分母:");
    scanf("%d,%d",&c,&d);
    printf("\n");
    m=compare(a,b,c,d);
    if(m>0){
        printf("a/b大于c/d!\n");
    }
    if(m==0){
        printf("a/b等于c/d!\n");
    }
    if(m<0){
        printf("a/b小于c/d!\n");
    }

    }
}

 

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

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

标签:

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!