Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.65 KB | None | 0 0
  1. #include <iostream>
  2. #include "RpnCalculator.h"
  3.  
  4. using namespace std;
  5.  
  6. void input();
  7.  
  8. int main(void) {
  9.  
  10.     cout << endl << "Reverse Polish Notation Calculator." << endl << endl <<
  11.         "Please enter some values to compute followed by the return key or press Q to quit." << endl;
  12.     input();
  13.     cout << "Goodbye.";
  14.     return 0;
  15. }
  16.  
  17.  
  18. void input() {
  19.     RpnCalculator *c = new RpnCalculator();
  20.     int i_value = 0;
  21.     char input = '\0';
  22.     bool play = true;
  23.  
  24.     cout << "(Acceptable operators: + - * /)" << endl;
  25.     do {
  26.         cout << " --> ";
  27.         // Takes a value from the user and assigns it to 'input'.
  28.         cin.sync();
  29.         input = cin.get();
  30.         /* Checking that input.
  31.         *   If it's an operator it performs the operation and prints the result.
  32.         *   If it's a numerical value, the char is cast to an int and passed to the _operands vector using push().
  33.         */
  34.         switch (input) {
  35.         case '+' :
  36.             c->add();
  37.             cout << endl << " Result: " << c->answer() << endl;
  38.             break;
  39.         case '-' :
  40.             c->subtract();
  41.             cout << endl << " Result: " << c->answer() << endl;
  42.             break;
  43.         case '*' :
  44.             c->multiply();
  45.             cout << endl << " Result: " << c->answer() << endl;
  46.             break;
  47.         case '/' :
  48.             c->divide();
  49.             cout << endl << " Result: " << c->answer() << endl;
  50.             break;
  51.         case '0' :
  52.         case '1' :
  53.         case '2' :
  54.         case '3' :
  55.         case '4' :
  56.         case '5' :
  57.         case '6' :
  58.         case '7' :
  59.         case '8' :
  60.         case '9' :
  61.             cout << input << endl;
  62.             i_value = static_cast<int>(input);
  63.             c->push(i_value);
  64.         case 'q' :
  65.         case 'Q' :
  66.             play = false;
  67.             break;
  68.         default :
  69.             cout << " Please enter an operator, a number, or Q to quit " <<endl;
  70.             break;
  71.         }
  72.     } while (play);
  73.     delete c;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement