【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");
}这段代码,得注意等号,不是赋值号,不要问我是怎么知道的!!!血的教训!
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语言实现比较两个分数的大小
- C语言实现比较两个字符串的大小
- c语言:引用指针变量比较两个整数的大小
- 比较两个分数大小
- 求两个分数的加减乘除,并比较大小
- 比较两个分数的大小
- 比较两个分数的大小
- c语言:引用指针变量比较两个整数的大小
- 使用C语言编写程序,求两个整数的较大值(随机输入两个数,比较大小)
- 比较两个分数的大小
- 比较两个分数的大小
- C语言小试牛刀两个初级程序练习------1到100的加法运算,两个数大小的比较
- 分数比较:比较两个分数的大小
- C语言中比较两个字符串的大小
- 两个double的数字比较大小
- 两个浮点数比较大小方法
- 用javascript比较两个日期的大小
- (C#)两个DataTime类型数据比较大小
- 请定义一个宏,比较两个数a、b的大小,不能使用大于、小于、if语句。
- Javascript中比较两个时间的大小