#include<stdio.h>
int gcd(int a, int b){
return b==0? a: gcd(b,a%b);
}
int main(){
int a, b, c, d; //第一,二个分子分母
int x, y, m; //所求分子分母
char ch;
while(scanf("%d/%d %d/%d %c",&a,&b,&c,&d,&ch)!=EOF){
if(ch=='*')
{
x = a * c;
y = b * d;
m = gcd(x, y);
printf("%d/%d\n", x/m, y/m);
}
else if(ch=='/')
{
x = a * d;
y = c * b;
m = gcd(x, y);
printf("%d/%d\n", x/m, y/m);
}
else if(ch=='+')
{
x = a * d + b * c;
y = b * d;
m = gcd(x, y);
printf("%d/%d\n", x/m, y/m);
}
else if(ch=='-') //减法的时候,负数要注意
{
x = a * d - b * c;
y = b * d;
m = gcd(x < 0? -x: x, y);
printf("%d/%d\n", x/m, y/m);
}
}
return 0;
}
1012分数运算
最新推荐文章于 2023-06-24 22:57:45 发布