2
This problem seems very simple, however my code does not commit in any way by error with some random input.
The program that manages the submission of the question gives me some tips:
Check the following points:
- Remember to set the output to 3 decimal places;
(that I’ve done)- Partial results can be real numbers and not only integers;
(that I’ve done)- Check the cases where the operation cannot be performed.
(added the split exception by zero and add the exception in the choice of the operation character)
Description of the issue:
Code
See also on Ideone
#include <stdio.h>
#include <stdlib.h>
double op(double num1, double num2, char c)//operacoes
{
if(c == '/')//divisao
{
if(num2 == 0)
{
printf("operacao nao pode ser realizada");
exit(1);
}
else
num1 = (num1)/num2;
}
else if(c == '*')//multiplicacao
num1 = (num1)*num2;
else if(c == '-')//subtracao
num1= num1 - num2;
else if(c == '+')//soma
num1 = num1 + num2;
else if(c != '&')//se não for o caractere de finalização
{
printf("operacao nao pode ser realizada");//print o erro
exit(1);
}
printf("%.3f\n", num1);//printa o resultado da operação de: num1 e num2
return num1;//retorna o resultado da operaçãos
}
void captar_e_op(double num1)//captar e operar
{
///VAR
double num2;//equivalente ao num2
char c;
///ENTRADA
scanf("%lf", &num2);
scanf(" %c", &c);
while(c != '&')//se o char não for '&', continue o loop
{
num1 = op(num1,num2,c);//num1 recebe(=) o resultado da operação de: "antigo num1" e num2
///ENTRADA
scanf("%lf", &num2);
scanf(" %c", &c);
}
}
int main()
{
///VARAVEIS
char c;
double num1, num2;
///ENTRADA
scanf("%lf", &num1);
scanf("%lf", &num2);
scanf(" %c", &c);
num1 = op(num1, num2, c);
captar_e_op(num1);
return 0;
}
Actually I do not know how to identify this error. It is related to some entry in which the result does not return the expected. So far all my entries have returned as expected.
– pcccj