-1

I want to make a 4 operation calculator... the user must enter the 3 variables float for A and B and char for +, *, ... He types A, op, and B and then presses the enter button to show the result. I want something like A op B = Result to appear in the console, all 4 on the same line.

#include <stdio.h>
int main()
{
    float A = 1, B = 1, R = 1;
    char op = '+';

    printf ("entrer A op B (sans espaces)\n");
    scanf ("%f%c%f", &A, &op, &B);


    switch (op)
    {
        case 43 :
            R = A+B;
            break;
        case 45 :
            R = A-B;
            break;
        case 42 :
            R = A*B;
            break;
        case 47 :
            if (B!=0)
            R = A/B;
            else
            printf ("on peut pas diviser sur 0");
            break;
        default :
            printf ("seulement les operations +,-,*,/ peuvent etre realiser, une par une, veuillez resseyer la saisie de votre operation");
            break;
    }
    if (B!=0 && (op =='+' || op =='-' || op =='*' || op =='/'))
    printf ("\b= %.2f", R);
    return 0;
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
Omayma Benali
  • 119
  • 1
  • 7
  • By employing a terminal `gotoxy` function, which is not in the standard C libraries. But you would have to know how long the user's entry was. You could do that by getting the input with `fgets()`. Aside: note that your `%c` will not be space-tolerant. – Weather Vane Oct 10 '19 at 17:32
  • Well this needs to be done using *platform-specific* libraries as the control to the cursor of the console is not the part of the standard... A "*horrible*" way to do it would be to use something like [`system("cls");` or `system("clear")` depending on your system](https://stackoverflow.com/questions/6486289/how-can-i-clear-console) (by clearing the screen and printing it again with the values), which looks like would work in your case. – Ruks Oct 10 '19 at 17:36
  • 1
    Another aside: why are you using the `float` and not `double`? Never use `float` unless you have a good reason to. – Weather Vane Oct 10 '19 at 17:42
  • You do not need to choose because of the ascii code in the case statements. You also can enter the operators symbols in this way: `case '+' :` – RobertS supports Monica Cellio Oct 10 '19 at 17:57
  • 1
    Try this way. Don't use `scanf`. Instead use `getchar` to read characters in a loop till you read the character `'='`. Then inside the loop parse the characters to distinguish between operand and operator based on the spaces. Then compute the result outside the loop. Well, if display format is your need you will have to work for it. – Sisir Oct 10 '19 at 18:10
  • What is the problem with your code? Can you share your experience with us? – RobertS supports Monica Cellio Oct 10 '19 at 18:11
  • 1
    Use  `case '+'` in your program. – Basile Starynkevitch Oct 10 '19 at 18:13
  • You can put the terminal in non-cannonical mode and intercept each keypress (Linux/Unix see POSIX `termios.h`, Windows (smell) but `getch()` works). To provide minimal editing you will need to catch an implement a backspace (delete) so the user can backup if they mess up. – David C. Rankin Oct 10 '19 at 18:14

2 Answers2

0

you can use in sprintf to all line.

notice in line:

    scanf ("%f%c%f", &A, &op, &B)

it get the "enter" as "\n" (ascii 10)

you need space between them:

    #include <stdio.h>

int main() 
{
    float A = 1, B = 1, R = 1;
    char op = '+';
    char ans[32];
    printf ("entrer A op B (sans espaces)\n");
    scanf("%f %c %f", &A, &op, &B);

    switch (op)
    {
        case '+' :
            R = A+B;
            break;
        case '-' :
            R = A-B;
            break;
        case '*' :
            R = A*B;
            break;
        case '/' :
            if (B!=0)
            {
                R = A/B;
            }
            else
            {
                printf ("on peut pas diviser sur 0");
            }
            break;
        default :
            printf ("seulement les operations +,-,*,/ peuvent etre realiser, une par une, veuillez resseyer la saisie de votre operation");
            break;
    }

    if (B!=0 && (op =='+' || op =='-' || op =='*' || op =='/'))
    {
        sprintf(ans,"%.2f %c %.2f \b= %.2f\n", A, op, B, R);
    }

    printf("%s", ans);

    return 1;
}

you need to check A and B is a number (check "isdigit()" command). why "B!=0"?

if you want you can get all exercise from user as one string "4 + 5" and parser it (I understand you are begginer in C. parser is very usefullת You better practice it)

gil
  • 1
  • 3
0

Well, i don´t know if i quite understood what you want, but this:

printf(" %f.2 %c %f.2 = %f.4",A,op,B,R);

should give one answer on the question you give.

printf ("\b= %.2f", R);

does not make sense in this case, because it prints only the equality sign (=) and the resulting value of R out on the monitor and in the program before is not an any outprint of the other values of A,opand B provided.