Advertisement
pdaogu

Ex6.2

Oct 14th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.87 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int main () {
  4.     float a, b;
  5.     char op;
  6.     printf("%*s%s%-*s\n\n", 10, ".:: ", "Simple Calculator", 10, " ::.");
  7.     printf("> Enter a simple expression (e.g: 1 + 2): ");
  8.     if (!scanf("%f %c %f", &a, &op, &b)) {
  9.         printf("You entered an invalid expression.");
  10.         return 0;
  11.     }
  12.     while (getchar() != '\n');
  13.     printf("\n> ");
  14.     switch (op) {
  15.         case '+':
  16.             printf("%.2f %c %.2f = %.2f\n", a, op, b, a + b);
  17.             break;
  18.         case '-':
  19.             printf("%.2f %c %.2f = %.2f\n", a, op, b, a - b);
  20.             break;
  21.         case '/':
  22.             printf("%.2f %c %.2f = %.2f\n", a, op, b, a / b);
  23.             break;
  24.         case '*':
  25.             printf("%.2f %c %.2f = %.2f\n", a, op, b, a * b);
  26.             break;
  27.         case '%':
  28.             printf("%d %c %d = %d\n", (int) a, op, (int) b, (int)a % (int)b);
  29.             break;
  30.         default:
  31.             printf("The operator you entered is invalid or not supported (support: + - * / %).\n");
  32.     }
  33.     return 0;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement