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;
}