I’m having trouble with my C algorithm

Asked

Viewed 153 times

-3

I created a calculator:

    include stdio.h

    include stdlib.h

    include locale.h

    int main(void){

        system("color 0a");
        float num1, num2;
        char op;
        setlocale(LC_ALL, "portuguese");
        while(1){
        printf("\t\t\tCALCULADORA DO BARALHO\n");
        printf("Faça seus Cálculos: ");
        scanf("%f%c%f", &num1,&op,&num2);
        switch (op){
            case '+':
            printf ("\nO Resultado é= %.1f\n", num1+num2);
            break;
            case '-':
            printf ("\nO Resultado é= %.1f\n", num1-num2);
            break;
            case '*':
            printf ("\nO Resultado é= %.1f\n", num1*num2);
            break;
            case '/':
            printf ("\nO Resultado é= %.1f\n", num1/num2);
            break;
        default:
            system("cls");
            printf("\n(There something wrong DUDE!!! Try again)\n\n");
            break;
        }
        if(num1!=0 && num1!=1 && num1!=2 && num1!=3 && num1!=4 && num1!=5 && num1!=6 && num1!=7 && num1!=8 && num1!=9){
            system("cls");
            printf("(There something wrong DUDE!!! Try again)\n\n");
            system("color 0c");
            system("pause");
            break;  
        }
        else{
        }
        if(num2!=0 && num2!=1 && num2!=2 && num2!=3 && num2!=4 && num2!=5 && num2!=6 && num2!=7 && num2!=8 && num2!=9){
            system("cls");
            printf("(There something wrong DUDE!!! Try again)\n\n");
            system("color 0c");
            system("pause");
            break;
        }
        else{
        }
        printf("\t\t\t\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n!!!!LUCAS PRODUÇÕES E DESENVOLVIMENTO!!!!\n;-;");

        system ("pause");

        system ("cls");
    }

//use ctrl+c para fechar o programa.
    return 0;
}

However it presents the following problems:

  1. Once one of the error conditions is reached the established loop by while(1) terminates and the program closes. I want it to go back to the beginning.
  2. After the first calculation works, if the second one goes forward to one of the error conditions the program suffers a bug and not to do nothing else.
  • If you want to move to another iteration, change the break; for continue;. If you want to check if an integer is not in the range [0.9] if (num1 < 0 && num1 > 9) {/* Seu código aqui.*/}

  • I want to check if the number is float, in case n wants the program to signal an error and go back to the beginning, to the loop.

  • In C you can’t do that. The C language does not pass type information in Runtime.

  • 1

    I just want q n bug when I type a character, just send an error msg and go back to the program.

  • For this you have to check the result of scanf. In case of success scanf returns the number of completed arguments, in case of failure it returns 0 or an error code (negative number). Documentation of scanf

1 answer

0

I don’t know why you limited operations between 0 and 9, so I limited my implementation as well.

The checks must be done before performing the calculations and if an entry is found invalidates the program breaks the current loop using the continue, the break is used to close the loop.

I avoided using the function system() for portability, if you want to stop the program use a getchar() or run the program from your system terminal or integrated terminal in your IDE/Editor.

#include <stdio.h>

int main() {
  printf("[CALCULADORA]\n");

  float n1 = 0, n2 = 0;
  char op = '+';

  while(1) {
    printf("DIGITE A OPERAÇÃO(ex: 2 + 2, pressione Ctrl + C para sair): ");

    scanf("%f %c %f*c", &n1, &op, &n2);

    if(n1 < 0 || n1 > 9 || n2 < 0 || n2 > 9) {
      printf("As entradas estão limitada entre 0 a 9. Tente Novamente...\n");
      continue;
    }

    switch(op) {
      case '+':
        printf("[RESULTADO]: %.2f\n", n1 + n2);
      break;
      case '-':
        printf("[RESULTADO]: %.2f\n", n1 - n2);
      break;
      case '*':
        printf("[RESULTADO]: %.2f\n", n1 * n2);
      break;
      case '/':
        if(n2 != 0) {
          printf("[RESULTADO]: %.2f\n", n1 / n2);
        }else {
          printf("O número %.2f não pode ser dividido por zero! Tente Novamente...\n", n1);
        }
      break;
      default:
        printf("Operação Inválida! Tente Novamente...\n");
    }
  }

  return 0;
}

Browser other questions tagged

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