A1088/B1034 Rational Arithmetic

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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

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
#include<bits/stdc++.h> 
using namespace std;
typedef long long ll;
struct fract{
ll a,b;

};
int n;
ll gcd(ll a,ll b){
if(a%b==0) return b;
gcd(b,a%b);
}

fract redu(fract m){
fract res;
if(m.b<0){
m.b=-m.b;
m.a=-m.a;
}
if(m.b==0) {
fract tmp;
tmp.b=0;
tmp.a=1;
return tmp;
}
ll g=gcd(abs(m.a),abs(m.b));
res.a=m.a/g;
res.b=m.b/g;
return res;
}
fract add(fract m,fract n){
fract res;
res.b=m.b*n.b;
res.a=m.a*n.b+n.a*m.b;
return redu(res);
}
fract min(fract m,fract n){
fract res;
res.b=m.b*n.b;
res.a=m.a*n.b-n.a*m.b;
return redu(res);
}
fract chen(fract m,fract n){
fract res;
res.b=m.b*n.b;
res.a=m.a*n.a;
return redu(res);
}
fract chu(fract m,fract n){
fract res;
res.a=m.a*n.b;
res.b=m.b*n.a;

return redu(res);
}
void show(fract ff){
ff=redu(ff);
if(ff.a<0) cout<<"(";
if(ff.b==1) {
printf("%lld",ff.a);
}

else if(abs(ff.a)>ff.b){
printf("%lld %lld/%lld",ff.a/ff.b,abs(ff.a)%ff.b,ff.b);
}
else printf("%lld/%lld",ff.a,ff.b);
if(ff.a<0) cout<<")";
}
int main(){
fract sum1,sum2,sum3,sum4,f,g;
scanf("%lld/%lld %lld/%lld",&f.a,&f.b,&g.a,&g.b);
sum1=add(f,g);
sum2=min(f,g);
sum3=chen(f,g);

show(f);
cout<<" + ";
show(g);
cout<<" = ";
show(sum1);
cout<<endl;

show(f);
cout<<" - ";
show(g);
cout<<" = ";
show(sum2);
cout<<endl;

show(f);
cout<<" * ";
show(g); cout<<" = ";
show(sum3);
cout<<endl;


show(f);
cout<<" / ";
show(g); cout<<" = ";
if(g.a==0) {
cout<<"Inf"<<endl;
}
else show(chu(f,g));
}