1088 Rational Arithmetic (20分)
For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate their sum, difference, product and quotient.
Input Specification:
Each input file contains one test case, which gives in one line the two rational numbers in the format a1/b1 a2/b2
. The numerators and the denominators are all in the range of long int. If there is a negative sign, it must appear only in front of the numerator. The denominators are guaranteed to be non-zero numbers.
Output Specification:
For each test case, print in 4 lines the sum, difference, product and quotient of the two rational numbers, respectively. The format of each line is number1 operator number2 = result
. Notice that all the rational numbers must be in their simplest form k a/b
, where k
is the integer part, and a/b
is the simplest fraction part. If the number is negative, it must be included in a pair of parentheses. If the denominator in the division is zero, output Inf
as the result. It is guaranteed that all the output integers are in the range of long int.
Sample Input 1:
2/3 -4/2
Sample Output 1:
2/3 + (-2) = (-1 1/3)
2/3 - (-2) = 2 2/3
2/3 * (-2) = (-1 1/3)
2/3 / (-2) = (-1/3)
Sample Input 2:
5/3 0/6
Sample Output 2:
1 2/3 + 0 = 1 2/3
1 2/3 - 0 = 1 2/3
1 2/3 * 0 = 0
1 2/3 / 0 = Inf
题目意思:求分数的加减乘除
输出规则:负数需要带括号输出。假分数要以标准格式输出。
解题思路:每个分数的输出格式都是同意的规则,将这个输出搞定就行了。
注意:数据类型要是long long类型,否则测试点2会报错。
#include<bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b){
if(a==0) return b;
gcd(b%a,a);
}
void com(long long x, long long y){
long long g, c;
bool f = false;
g = gcd(abs(x),abs(y));
x/=g; y/=g;
c = x/y;
if(c<0) x *= -1;
x %= y;
if(c!=0){
if(c<0){
printf("(%lld", c);
f = true;
}
else printf("%lld", c);
}
if(x<0){
printf("(%lld", x);
f = true;
}
else{
if(c==0) printf("%lld", x);
else if(c!=0&&x!=0) printf(" %lld", x);
}
if(x!=0&&y!=1){
printf("/%lld", y);
}
if(f) printf(")");
}
void solve1(long long a1, long long b1, long long a2, long long b2){
com(a1,b1); printf(" + ");
com(a2,b2); printf(" = ");
long long a, b;
b = b1*b2;
a = a1*b2 + a2*b1;
com(a,b);
}
void solve2(long long a1, long long b1, long long a2, long long b2){
com(a1,b1); printf(" - ");
com(a2,b2); printf(" = ");
long long a, b;
b = b1*b2;
a = a1*b2 - a2*b1;
com(a,b);
}
void solve3(long long a1, long long b1, long long a2, long long b2){
com(a1,b1); printf(" * ");
com(a2,b2); printf(" = ");
if(a1==0||a2==0){
printf("0");
return ;
}
long long a, b;
a = a1*a2; b = b1*b2;
if(b<0){
b*=-1;
a*=-1;
}
com(a,b);
}
void solve4(long long a1, long long b1, long long a2, long long b2){
com(a1,b1); printf(" / ");
com(a2,b2); printf(" = ");
if(a2==0){
printf("Inf");
return ;
}
long long a, b;
a = a1*b2; b = b1*a2;
if(b<0){
b*=-1;
a*=-1;
}
com(a,b);
}
int main()
{
long long a1, b1, a2, b2;
scanf("%lld/%lld %lld/%lld", &a1, &b1, &a2, &b2);
solve1(a1,b1,a2,b2); printf("\n");
solve2(a1,b1,a2,b2); printf("\n");
solve3(a1,b1,a2,b2); printf("\n");
solve4(a1,b1,a2,b2); printf("\n");
return 0;
}