Advertisement
KuoHsiangYu

testArithmetic_2

Nov 18th, 2020
911
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.96 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. void showResult(int);
  5.  
  6. void showResult(int n3) {
  7.     printf("answer = ");
  8.     printf("%d\n", n3);
  9. }
  10.  
  11. int main(int argc, char *argv[]) {
  12.     int n1 = 0;
  13.     int n2 = 0;
  14.     char selectSymbol = ' ';
  15.     char clearChar = ' ';
  16.  
  17.     printf("Enter two number: ");
  18.     scanf("%d %d", &n1, &n2);
  19.  
  20.     /* clear buffer */
  21.     while ((clearChar = getchar()) != EOF && clearChar != '\n');
  22.  
  23.     printf("Enter operator: ");
  24.     scanf("%c", &selectSymbol);
  25.  
  26.     int ans = 0;
  27.  
  28.     switch (selectSymbol) {
  29.         case '+':
  30.             ans = n1 + n2;
  31.             break;
  32.         case '-':
  33.             ans = n1 - n2;
  34.             break;
  35.         case '*':
  36.             ans = n1 * n2;
  37.             break;
  38.         case '/':
  39.             ans = n1 / n2;
  40.             break;
  41.         default:
  42.             /* do nothing */
  43.             break;
  44.     }
  45.  
  46.     showResult(ans);
  47.  
  48.     printf("\n");
  49.     system("pause");
  50.     return 0;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement