-
Notifications
You must be signed in to change notification settings - Fork 0
/
1022.cpp
65 lines (56 loc) · 1.09 KB
/
1022.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
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int mdc (int a, int b) {
if (a == b)
return a;
else if (a > b)
return mdc(b, a-b);
else
return mdc(a, b-a);
}
void print (int a, int b) {
cout << a << '/' << b << " = ";
int m = mdc(fabs(a),fabs(b));
a /= m;
b /= m;
cout << a << '/' << b << endl;
}
void sum (int a, int b, int c, int d) {
int n1 = a*d + b*c;
int n2 = b*d;
print(n1, n2);
}
void sub (int a, int b, int c, int d) {
sum(a,b,-c,d);
}
void mult (int a, int b, int c, int d) {
int n1 = a*c;
int n2 = b*d;
print(n1, n2);
}
void div (int a, int b, int c, int d) {
mult(a,b,d,c);
}
int main () {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
int a, b, c, d;
char op;
char aux;
// a / b op c / d
cin >> a >> aux >> b >> op;
cin >> c >> aux >> d;
if (op == '+')
sum(a,b,c,d);
else if (op == '-')
sub(a,b,c,d);
else if (op == '*')
mult(a,b,c,d);
else if (op == '/')
div(a,b,c,d);
}
return 0;
}