您的位置:首页 > 编程语言 > C语言/C++

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

2014-12-19 20:25 465 查看
#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");
}这段代码,得注意等号,不是赋值号,不要问我是怎么知道的!!!血的教训!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: