Marathon Problem: Calculator (Level 2)

Asked

Viewed 133 times

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:

Descrição da questão

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.

3 answers

2


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

  • 1

    The exit() this path for errors. Normally the function returns the expected value.

  • That was exactly what it was. When entering an invalid command, or invalid operation the program did not end as I had thought;

  • return num1; instead of exit(1);

1

Try adding a Newline in the two error printf.

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

In some systems, the absence of Newline makes the line not printed or recognized by the parent program.

  • True. I changed and submitted again. But the system still continues to state that the program is refutable and gives the following tips:

  • Check the following points: * Remember to set the output to 3 decimal places; * Partial results can be real numbers and not only integers; * Check the cases where the operation cannot be performed.

1

Check the scanfs in main. If the input does not start correctly, your program does not give 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); }
  • 1

    Alias you must at all times check the scanf().

  • 1

    You can join the scanfs: if (scanf("%lf%lf %c", &num1, &num2, &c) != 3) { /* erro */ }

  • Thank you for your reply! I have never used the scanf in an IF... WHILE... condition but how does the scanf work in such cases? what values the scanf can return?

  • 1

    The scanf() returns the number of assignments you executed or EOF in case of error. Basically if the returned number is not equal to the number of variables to assign value, something went wrong within the scanf().

Browser other questions tagged

You are not signed in. Login or sign up in order to post.