-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path1022.c
60 lines (47 loc) · 917 Bytes
/
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
//1022 - TDA Racional
/* 28/02/2015 - 1022 _ TDA Racional
https://www.urionlinejudge.com.br/judge/pt/problems/view/1022
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int mdc(int a, int b)
{
if(a<0)
a = -a;
if(b<0)
b = -b;
if(a%b == 0)
return b;
else
return mdc(b,a%b);
}
int main (){
int N, n1, d1, n2, d2, num, den, div;
char oper;
scanf("%d", &N);
do{
scanf("%d / %d %c %d / %d", &n1, &d1, &oper, &n2, &d2);
if(oper == '+'){
num = (n1*d2 + n2*d1);
den = (d1*d2);
}
else if(oper == '-'){
num = (n1*d2 - n2*d1);
den = (d1*d2);
}
else if(oper == '*'){
num = (n1*n2);
den = (d1*d2);
}
else if(oper == '/'){
num = (n1*d2);
den = (n2*d1);
}
div = mdc(num,den);
printf("%d/%d = %d/%d\n", num, den, num/div, den/div);
N--;
} while (N>0);
return 0;
}