-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1022.cpp
More file actions
66 lines (52 loc) · 1.67 KB
/
1022.cpp
File metadata and controls
66 lines (52 loc) · 1.67 KB
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
#include <iostream>
using namespace std;
int achaDivisorComum(int result1, int result2){
int maior, divisorComum;
if(result1 > result2) maior = result1;
else maior = result2;
for(int i=1; i<maior; i++){
if(result1 % i == 0 && result2 % i == 0) divisorComum = i;
}
if(result1 == result2) divisorComum = result1;
return divisorComum ? divisorComum : 0;
}
int main(){
int num_testes;
int N1, N2, D1, D2;
string operacao, barra;
int resultado1 = 0;
int resultado2 = 0;
int resultadoSimplificado1 = 0;
int resultadoSimplificado2 = 0;
cin >> num_testes;
while(num_testes > 0){
cin >> N1 >> barra >> D1 >> operacao >> N2 >> barra >> D2;
if(operacao == "+"){
resultado1 = (N1 * D2 + N2 * D1);
resultado2 = (D1 * D2);
}
if(operacao == "-"){
resultado1 = (N1 * D2 - N2 * D1);
resultado2 = (D1 * D2);
}
if(operacao == "*"){
resultado1 = (N1 * N2);
resultado2 = (D1 * D2);
}
if(operacao == "/"){
resultado1 = (N1 * D2);
resultado2 = (N2 * D1);
}
int guardaValor = achaDivisorComum(resultado1, resultado2);
if(guardaValor > 0){
resultadoSimplificado1 = resultado1 / guardaValor;
resultadoSimplificado2 = resultado2 / guardaValor;
} else {
resultadoSimplificado1 = resultado1;
resultadoSimplificado2 = resultado2;
}
cout << resultado1 << "/" << resultado2 << " = " << resultadoSimplificado1 << "/" << resultadoSimplificado2 << endl;
num_testes--;
}
return 0;
}