Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2016
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.86 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int calculate(int x, int y, char op) {
  6.     int result;
  7.     switch (op) {
  8.         case '+':
  9.             result = x + y;
  10.             break;
  11.         case '-':
  12.             result = x - y;
  13.             break;
  14.         case '*':
  15.             result = x * y;
  16.             break;
  17.         case '/':
  18.             result = x / y;
  19.             break;
  20.         case '%':
  21.             result = x % y;
  22.             break;
  23.         default:
  24.             cout << "calculate(): Unhandled case \n"; return 0;
  25.     }
  26.     return result;
  27. }
  28.  
  29.  
  30. int main() {
  31.     cout << "Enter An Interger: ";
  32.     int x;
  33.     cin >> x;
  34.     cout << "Enter Another Interger: ";
  35.     int y;
  36.     cin >> y;
  37.     cout << "Enter an operator (+,-,*,/,%): ";
  38.     char op;
  39.     cin >> op;
  40.    
  41.     cout << calculate(x, y, op) << endl;
  42.    
  43.     return 0;
  44.    
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement