Advertisement
Guest User

LISP-like expression list execution

a guest
Jul 27th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.90 KB | None | 0 0
  1. /******************************************************************************************
  2. *******************************************************************************************
  3. ** Write a program that takes an operation followed by two operands and outputs the result. For
  4. ** example:
  5. ** + 100 3.14
  6. ** * 4 5
  7. **
  8. */
  9.  
  10. #include "../../std_lib_facilities.h"
  11.  
  12. int main()
  13. {
  14.     cout << "Pass an operator followed by its operands and execute the expression ( (+ 1 1) => 2 ):\n";
  15.     char ops = ' ';
  16.     int n1 = 0, n2 = 0;
  17.     double res;
  18.     cin >> ops;
  19.     cin >> n1 >> n2;
  20.  
  21.     cout << "The result of (" << ops << " " << n1 << " " << n2 << ") is: \n";
  22.  
  23.     if (ops == '+') {
  24.         res = n1 + n2;
  25.     } else if (ops == '-') {
  26.         res = n1 - n2;
  27.     } else if (ops == '*') {
  28.         res = n1 * n2;
  29.     } else if (ops == '/') {
  30.         res = n1 * n2;
  31.     } else if (ops == '%') {
  32.         res = n1 % n2;
  33.     }
  34.  
  35.     cout << res << "\n";
  36.  
  37.     return 0;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement