编写C语言程序,计算由用户输入的两个分数,求它们的加,减,乘,除.
答案:3 悬赏:30 手机版
解决时间 2021-11-24 23:22
- 提问者网友:沉默的哀伤
- 2021-11-24 17:46
编写C语言程序,计算由用户输入的两个分数,求它们的加,减,乘,除.
最佳答案
- 五星知识达人网友:酒醒三更
- 2021-11-24 18:19
思路:直接输入两个依次计算加,减,乘,除,需要注意除法的除数不能为0。
参考代码:
#include
int main()
{
int a,b;
scanf("%d%d",&a,&b);
printf("a+b=%d
",a+b);
printf("a-b=%d
",a-b);
printf("a*b=%d
",a*b);
if(b!=0)
printf("a/b=%.2f
",1.0*a/b);
else
printf("除数为0!
");
return 0;
}
参考代码:
#include
int main()
{
int a,b;
scanf("%d%d",&a,&b);
printf("a+b=%d
",a+b);
printf("a-b=%d
",a-b);
printf("a*b=%d
",a*b);
if(b!=0)
printf("a/b=%.2f
",1.0*a/b);
else
printf("除数为0!
");
return 0;
}
全部回答
- 1楼网友:一袍清酒付
- 2021-11-24 20:21
#include
void main()
{
int a,b;
printf("input a,b");
scanf("%d%d",&a,&b);
printf("%2d%2d%2d%2f",a+b,a-b,a*b,a*1.0/b);
}
void main()
{
int a,b;
printf("input a,b");
scanf("%d%d",&a,&b);
printf("%2d%2d%2d%2f",a+b,a-b,a*b,a*1.0/b);
}
- 2楼网友:刀戟声无边
- 2021-11-24 19:14
这题,问过答过。
int Gcd(int a, int b){} 是求最大公约数函数。用于加,减,乘,除结果的约分。
输入格式 是 4/3,9/8 用逗号分隔 2 个分数。
#include
#include
int Gcd(int a, int b)
{
if(b == 0)
return a;
return Gcd(b, a % b);
}
main(){
int a,b,c,d;
int e,f,g;
printf("input: 4/3,9/8\n");
scanf("%d/%d,%d/%d",&a,&b,&c,&d);
// printf("%d %d %d %d\n",a,b,c,d);
e = a*d+c*b;
f = b*d;
g = Gcd(f,e);
f = f/g; e = e/g;
printf("sum: %d/%d\n",e,f);
e = a*d-c*b;
f = b*d;
g = Gcd(f,e);
f = f/g; e = e/g;
printf("dif: %d/%d\n",e,f);
e = a*c;
f = b*d;
g = Gcd(f,e);
f = f/g; e = e/g;
printf("mul: %d/%d\n",e,f);
e = a*d;f=b*c;
g = Gcd(f,e);
f = f/g; e = e/g;
printf("quo: %d/%d\n",e,f);
return 0;
}
运行结果:
4/3,9/8
sum: 59/24
dif: 5/24
mul: 3/2
quo: 32/27
int Gcd(int a, int b){} 是求最大公约数函数。用于加,减,乘,除结果的约分。
输入格式 是 4/3,9/8 用逗号分隔 2 个分数。
#include
#include
int Gcd(int a, int b)
{
if(b == 0)
return a;
return Gcd(b, a % b);
}
main(){
int a,b,c,d;
int e,f,g;
printf("input: 4/3,9/8\n");
scanf("%d/%d,%d/%d",&a,&b,&c,&d);
// printf("%d %d %d %d\n",a,b,c,d);
e = a*d+c*b;
f = b*d;
g = Gcd(f,e);
f = f/g; e = e/g;
printf("sum: %d/%d\n",e,f);
e = a*d-c*b;
f = b*d;
g = Gcd(f,e);
f = f/g; e = e/g;
printf("dif: %d/%d\n",e,f);
e = a*c;
f = b*d;
g = Gcd(f,e);
f = f/g; e = e/g;
printf("mul: %d/%d\n",e,f);
e = a*d;f=b*c;
g = Gcd(f,e);
f = f/g; e = e/g;
printf("quo: %d/%d\n",e,f);
return 0;
}
运行结果:
4/3,9/8
sum: 59/24
dif: 5/24
mul: 3/2
quo: 32/27
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯