1
I went to solve exercise 1022 of Urionlinejudge and received a Compilation error, but the code works perfectly in Code::Blocks. Please inform me of the error. Link to the problem.
#include <stdio.h>
void simplificacao(int *a, int *b){
for(int j=*b;j>0;j--){
if(*a%j==0 && *b%j==0){
*a/=j;
*b/=j;
}
}
}
int main(){
int x, N1, D1, N2, D2, i, N3, D3;
char t1, op, t2;
scanf("%d", &x);
for(i=0;i<x;i++){
scanf("%d %c %d %c %d %c %d", &N1, &t1, &D1, &op, &N2, &t2, &D2);
if(op == '+'){
N3 = (N1*D2 + N2*D1);
D3 = (D1*D2);
printf("%d/%d = ", N3, D3);
simplificacao(&N3, &D3);
printf("%d/%d\n", N3, D3);
}
else if(op == '-'){
N3 = (N1*D2 - N2*D1);
D3 = (D1*D2);
printf("%d/%d = ", N3, D3);
simplificacao(&N3, &D3);
printf("%d/%d\n", N3, D3);
}
else if(op == '*'){
N3 = (N1*N2);
D3 = (D1*D2);
printf("%d/%d = ", N3, D3);
simplificacao(&N3, &D3);
printf("%d/%d\n", N3, D3);
}
else if(op == '/'){
N3 = (N1*D2);
D3 = (N2*D1);
printf("%d/%d = ", N3, D3);
simplificacao(&N3, &D3);
printf("%d/%d\n", N3, D3);
}
}
return 0;
}
If you’re using the compiler
gcc
with Codeblocks, try the parametersgcc -std=c89 -pedantic ...
to have the same behavior as online.– pmg