-
Notifications
You must be signed in to change notification settings - Fork 0
/
1088.cpp
103 lines (102 loc) · 2.22 KB
/
1088.cpp
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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;//单纯用int会导致后两个测试点错误,故数据类型要改用long long
ll gcd(ll a, ll b){//求最大公约数
return b==0?a:gcd(b, a%b);
}
struct fraction{//分数
ll up, down;
bool illegal = false;
fraction(){}
fraction(ll u, ll d){
up = u;
down = d;
}
};
fraction reduce(fraction &result){
if(result.down == 0){
result.illegal = true;
return result;
}else if(result.down < 0){
result.up = -result.up;
result.down = -result.down;
}
if(result.up == 0){
result.down = 1;
}else{
int d = gcd(abs(result.up), result.down);
result.up /= d;
result.down /= d;
}
return result;
}
fraction add(const fraction &a, const fraction &b){
fraction result;
result.down = a.down * b.down;
result.up = a.up * b.down + b.up * a.down;
return reduce(result);
}
fraction subtract(const fraction &a, const fraction &b){
fraction result;
result.down = a.down * b.down;
result.up = a.up * b.down - b.up * a.down;
return reduce(result);
}
fraction multi(const fraction &a, const fraction &b){
fraction result;
result.down = a.down * b.down;
result.up = a.up * b.up;
return reduce(result);
}
fraction divide(const fraction &a, const fraction &b){
fraction result;
result.down = a.down * b.up;
result.up = a.up * b.down;
return reduce(result);
}
void printFraction(const fraction &result){
if(result.illegal){
cout<<"Inf";
return;
}
if(result.up < 0) cout<<'(';
if(result.down == 1) cout<<result.up;
else if(abs(result.up) > result.down){
cout<<result.up/result.down<<' '<<abs(result.up%result.down)<<'/'<<result.down;
}else{
cout<<result.up<<'/'<<result.down;
}
if(result.up < 0) cout<<')';
}
int main(){
char tmp;
ll aup, adown, bup, bdown;
cin>>aup>>tmp>>adown>>bup>>tmp>>bdown;
fraction a(aup, adown), b(bup, bdown);
reduce(a);
reduce(b);
printFraction(a);
cout<<" + ";
printFraction(b);
cout<<" = ";
printFraction(add(a, b));
cout<<endl;
printFraction(a);
cout<<" - ";
printFraction(b);
cout<<" = ";
printFraction(subtract(a, b));
cout<<endl;
printFraction(a);
cout<<" * ";
printFraction(b);
cout<<" = ";
printFraction(multi(a, b));
cout<<endl;
printFraction(a);
cout<<" / ";
printFraction(b);
cout<<" = ";
printFraction(divide(a, b));
return 0;
}