Marathon Problem: Calculator (Level 2)

2

This problem seems very simple, however my code does not submit in any way because of an error with some random input.

The program that manages the submission of the question gives me some tips:

  

Check the following points:

     
  • Remember to put the output to 3 decimal places; (this I've already done)
  •   
  • Partial results can be real numbers and not just integers; (this I've already done)
  •    )
  •   

Description of the question:

Code

Seealso 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;
}
    
asked by anonymous 25.10.2014 / 20:32

3 answers

2

You are using exit(1) in function op() when you should use return

    
27.10.2014 / 14:03
1

Try adding a NewLine in both error printf.

        printf("operacao nao pode ser realizada\n");
        //                                     ^^

In some systems, the absence of NewLine causes the line not to be printed or recognized by the parent program.

    
26.10.2014 / 09:47
1

Check the scanfs on main. If the input does not start correctly, your program does not give an error message.

  if (scanf("%lf", &num1) != 1) { fprintf(stderr, "operacao nao pode ser realizada\n"); exit(EXIT_FAILURE); }
  if (scanf("%lf", &num2) != 1) { fprintf(stderr, "operacao nao pode ser realizada\n"); exit(EXIT_FAILURE); }
  if (scanf(" %c", &c) != 1) { fprintf(stderr, "operacao nao pode ser realizada\n"); exit(EXIT_FAILURE); }
    
26.10.2014 / 10:04