-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path1022.c
60 lines (50 loc) · 1.15 KB
/
1022.c
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
/* URI Online Judge | 1022
TDA Racional
Recebendo 5% de WA '-'
*/
#include <stdio.h>
int calculaMDC(int x, int y){
int resto;
resto = x % y;
while(resto){
x = y;
y = resto;
resto = x % y;
}
return y;
}
int main(void){
int n, N1, N2, D1, D2, num, den, mdc;
char op;
scanf("%d", &n);
while(n--){
scanf("%d / %d %c %d / %d", &N1, &D1, &op, &N2, &D2);
switch(op){
case '+':
num = N1*D2 + N2*D1;
den = D1*D2;
break;
case '-':
num = N1*D2 - N2*D1;
den = D1*D2;
break;
case '*':
num = N1*D2;
den = D1*D2;
break;
case '/':
num = N1*D2;
den = N2*D1;
break;
default:
break;
}
printf("%d/%d = ", num, den);
mdc = calculaMDC(num, den);
if(mdc < 0)
printf("%d/%d\n", -(num / mdc), -(den / mdc));
else
printf("%d/%d\n", (num / mdc), (den / mdc));
}
return 0;
}