PAT-A-1088-Rational Arithmetic

or 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:

1
2/3 -4/2

Sample Output 1:

1
2
3
4
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:

1
5/3 0/6

Sample Output 2:

1
2
3
4
1 2/3 + 0 = 1 2/3
1 2/3 - 0 = 1 2/3
1 2/3 * 0 = 0
1 2/3 / 0 = Inf

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <iostream>
using namespace std;

typedef long long ll;

ll gcd(ll a,ll b){
return !b? a:gcd(b,a%b);
}

struct Fraction{
ll up,down;//分子和分母
};
//化简
void reduction(Fraction& f1){
//正负的化简
if(f1.down<0){
f1.up=-f1.up;
f1.down=-f1.down;
}
//为0的化简
if(f1.up==0)
f1.down=1;
else{
//约去公约数
ll d = gcd(abs(f1.up),abs(f1.down));
f1.up/=d;
f1.down/=d;
}
}

void showFraction(Fraction& f1){
if (f1.up < 0)
printf("(");
if (f1.down == 1)
printf("%lld", f1.up);
else if (abs(f1.up) > f1.down)//假分式
printf("%lld %lld/%lld", f1.up / f1.down, abs(f1.up) % f1.down, f1.down);
else
printf("%lld/%lld", f1.up, f1.down);
if (f1.up < 0)
printf(")");
}
//加
void add(Fraction& f1,Fraction& f2){
reduction(f1);
reduction(f2);
Fraction res;
showFraction(f1);
printf(" + ");
showFraction(f2);
printf(" = ");
res.up=f1.up*f2.down+f1.down*f2.up;
res.down=f1.down*f2.down;
reduction(res);
showFraction(res);
printf("\n");
}
//减
void difference(Fraction& f1,Fraction& f2){
reduction(f1);
reduction(f2);
Fraction res;
showFraction(f1);
printf(" - ");
showFraction(f2);
printf(" = ");
res.up=f1.up*f2.down-f1.down*f2.up;
res.down=f1.down*f2.down;
reduction(res);
showFraction(res);
printf("\n");

}
//乘
void product(Fraction& f1,Fraction& f2){
reduction(f1);
reduction(f2);
Fraction res;
showFraction(f1);
printf(" * ");
showFraction(f2);
printf(" = ");
res.up=f1.up*f2.up;
res.down=f1.down*f2.down;
reduction(res);
showFraction(res);
printf("\n");
}
//除
void quotient(Fraction& f1,Fraction& f2){
reduction(f1);
reduction(f2);
Fraction res;
showFraction(f1);
printf(" / ");
showFraction(f2);
printf(" = ");
res.up=f1.up*f2.down;
res.down=f1.down*f2.up;
reduction(res);
if(f2.up==0)
printf("Inf");
else
showFraction(res);
printf("\n");
}



int main(){
Fraction f1,f2;
scanf("%lld/%lld%lld/%lld",&f1.up,&f1.down,&f2.up,&f2.down);
add(f1,f2);
difference(f1,f2);
product(f1,f2);
quotient(f1,f2);
return 0;
}

----\(˙<>˙)/----赞赏一下吧~