Advertisement
thepowderguy

Reverse polish notation

Jun 13th, 2013
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.70 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <stack>
  4. #include <math.h>
  5. #include <stdlib.h>
  6.  
  7. using namespace std;
  8.  
  9. int main()
  10. {
  11.     stack <double> RPN_stk;
  12.     double in1;
  13.     double in2;
  14.     double cur_num;
  15.     bool only_0s = true;
  16.     bool do_out = true;
  17.     unsigned int stk_size;
  18.     string cur_ins;
  19.     cout << "Write something in reverse polish notation with\nspaces seperating numbers/operators and\n'=' / 'equals' to mark the end of equations."
  20.         "\nTo get a list of possible operations, write '?' and press enter." << endl;
  21.     while (true)
  22.     {
  23.         while (true)
  24.         {
  25.             cin >> cur_ins;
  26.             if (cur_ins == "?")
  27.             {
  28.                 cout << "\nOperators are:\na b + (a plus b / addition)\na b - (a minus b / subtraction)\n"
  29.                     "a b * (a times b / multiplication)\na b / (a divided by b / division)\n"
  30.                     "a b % (the remainder of a divided by b / modulo)\na b ^ (a to the power of b / exponentiation)\n"
  31.                     "a b hypo (hypotenuse of a and b)\na b atant (arctangent of a and b)\n"
  32.                     "a b max (the biggest number between a and b)\na b min (the smallest number between a and b)\n"
  33.                     "x sqrt (square root of x)\nx cbrt (cube root of x)\nx abs (absolute value of x)\nx sign (sign of x)\n"
  34.                     "x flor (x rounded down)\nx ceil (x rounded up)\nx round (x rounded to nearest integer)\n"
  35.                     "x nlog (natural logarithm of x / base e log)\nx clog (common decimal logarithm of x "
  36.                     "/ base 10 log)\nx blog (common binary logarithm of x / base 2 log)\n"
  37.                     "x sin (sine of x)\nx cos (cosine of x)\nx tan (tangent of x)\n"
  38.                     "x asin (arcsine of x)\nx acos (arccosine of x)\nx atan (arctangent of x)\n"
  39.                     "x sinh (hyperbolic sine of x)\nx cosh (hyperbolic cosine of x)\nx tanh (hyperbolic tangent of x)\n"
  40.                     "x sinh (hyperbolic arcsine of x)\nx cosh (hyperbolic arccosine of x)\nx tanh (hyperbolic arctangent of x)\n"
  41.                     "\nConstants are:\npi - it is pi\neu - euler's number\ngr - golden ratio\n" << endl;
  42.             }
  43.             else if (cur_ins == "+" || cur_ins == "-" || cur_ins == "*" || cur_ins == "/" ||
  44.                 cur_ins == "^" || cur_ins == "%" || cur_ins == "hypo" || cur_ins == "atant" ||
  45.                 cur_ins == "max" || cur_ins == "min" || cur_ins == "sqrt" || cur_ins == "cbrt" ||
  46.                 cur_ins == "abs" || cur_ins == "sign" || cur_ins == "flor" || cur_ins == "ceil" ||
  47.                 cur_ins == "round" || cur_ins == "nlog" || cur_ins == "clog" || cur_ins == "blog" ||
  48.                 cur_ins == "sin" || cur_ins == "cos" || cur_ins == "tan" ||
  49.                 cur_ins == "asin" || cur_ins == "acos" || cur_ins == "atan" ||
  50.                 cur_ins == "sinh" || cur_ins == "cosh" || cur_ins == "tanh" ||
  51.                 cur_ins == "asinh" || cur_ins == "acosh" || cur_ins == "atanh")
  52.             {
  53.                 if (RPN_stk.size() > 0)
  54.                 {
  55.                     if (cur_ins == "sqrt" || cur_ins == "cbrt" || cur_ins == "abs" ||
  56.                         cur_ins == "sign" || cur_ins == "flor" || cur_ins == "ceil" ||
  57.                         cur_ins == "nlog" || cur_ins == "clog" || cur_ins == "blog" ||
  58.                         cur_ins == "sin" || cur_ins == "cos" || cur_ins == "tan" ||
  59.                         cur_ins == "asin" || cur_ins == "acos" || cur_ins == "atan" ||
  60.                         cur_ins == "sinh" || cur_ins == "cosh" || cur_ins == "tanh" ||
  61.                         cur_ins == "asinh" || cur_ins == "acosh" || cur_ins == "atanh")
  62.                     {
  63.                         in2 = RPN_stk.top();
  64.                         RPN_stk.pop();
  65.                         cout << "POP: " << in2 << " BECAUSE OF OPERATION: '" << cur_ins << "', ";
  66.                     }
  67.                     else if (RPN_stk.size() > 1)
  68.                     {
  69.                         in2 = RPN_stk.top();
  70.                         RPN_stk.pop();
  71.                         in1 = RPN_stk.top();
  72.                         RPN_stk.pop();
  73.                         cout << "POP: " << in1 << " AND " << in2 << " BECAUSE OF OPERATION: '" << cur_ins << "', ";
  74.                     }
  75.                     else
  76.                     {
  77.                         cerr << "ERROR: Not enough numbers for operation. OPERATOR IS: " << cur_ins << endl;
  78.                         do_out = false;
  79.                         break;
  80.                     }
  81.                    
  82.                          if (cur_ins == "+") RPN_stk.push (in1 + in2);
  83.                     else if (cur_ins == "-") RPN_stk.push (in1 - in2);
  84.                     else if (cur_ins == "*") RPN_stk.push (in1 * in2);
  85.                     else if (cur_ins == "/") RPN_stk.push (in1 / in2);
  86.                     else if (cur_ins == "^") RPN_stk.push (pow(in1, in2));
  87.                     else if (cur_ins == "%") RPN_stk.push (fmod(in1, in2));
  88.                     else if (cur_ins == "hypo") RPN_stk.push (hypot(in1, in2)); //only c++11
  89.                     else if (cur_ins == "atant") RPN_stk.push (atan2(in1, in2));
  90.                     else if (cur_ins == "max") RPN_stk.push (max(in1, in2)); //only c++11
  91.                     else if (cur_ins == "min") RPN_stk.push (min(in1, in2)); //only c++11
  92.                     else if (cur_ins == "sqrt") RPN_stk.push (sqrt(in2));
  93.                     else if (cur_ins == "cbrt") RPN_stk.push (cbrt(in2)); //only c++11
  94.                     else if (cur_ins == "abs") RPN_stk.push (fabs(in2));
  95.                     else if (cur_ins == "sign") RPN_stk.push (copysign(1, in2)); //only c++11
  96.                     else if (cur_ins == "flor") RPN_stk.push (floor(in2));
  97.                     else if (cur_ins == "ceil") RPN_stk.push (ceil(in2));
  98.                     else if (cur_ins == "round") RPN_stk.push (round(in2)); //only c++11
  99.                     else if (cur_ins == "nlog") RPN_stk.push (log(in2));
  100.                     else if (cur_ins == "clog") RPN_stk.push (log10(in2));
  101.                     else if (cur_ins == "blog") RPN_stk.push (log2(in2)); //only c++11
  102.                     else if (cur_ins == "sin") RPN_stk.push (sin(in2));
  103.                     else if (cur_ins == "cos") RPN_stk.push (cos(in2));
  104.                     else if (cur_ins == "tan") RPN_stk.push (tan(in2));
  105.                     else if (cur_ins == "asin") RPN_stk.push (asin(in2));
  106.                     else if (cur_ins == "acos") RPN_stk.push (acos(in2));
  107.                     else if (cur_ins == "atan") RPN_stk.push (atan(in2));
  108.                     else if (cur_ins == "sinh") RPN_stk.push (sinh(in2));
  109.                     else if (cur_ins == "cosh") RPN_stk.push (cosh(in2));
  110.                     else if (cur_ins == "tanh") RPN_stk.push (tanh(in2));
  111.                     else if (cur_ins == "asinh") RPN_stk.push (asinh(in2)); //only c++11
  112.                     else if (cur_ins == "acosh") RPN_stk.push (acosh(in2)); //only c++11
  113.                     else if (cur_ins == "atanh") RPN_stk.push (atanh(in2)); //only c++11
  114.                    
  115.                     cout << " PUSH: " << RPN_stk.top() << " AS THE RESULT" << ", STACK SIZE: " << RPN_stk.size() << endl;
  116.                 }
  117.                 else
  118.                 {
  119.                     cerr << "ERROR: Not enough numbers for operation. OPERATOR IS: " << cur_ins << endl;
  120.                     do_out = false;
  121.                     break;
  122.                 }
  123.             }
  124.             else if (cur_ins == "=" || cur_ins == "equals")
  125.             {
  126.                 break;
  127.             }
  128.             else if (cur_ins == "pi" || cur_ins == "eu" || cur_ins == "gr")
  129.             {
  130.                      if (cur_ins == "pi") cur_num = 3.1415926535897932384626433832795;
  131.                 else if (cur_ins == "eu") cur_num = 2.7182818284590452353602874713526;
  132.                 else if (cur_ins == "gr") cur_num = 1.6180339887498948482045868343656;
  133.                
  134.                 RPN_stk.push (cur_num);
  135.                 cout << "PUSH: " << cur_num << " (" << cur_ins << "), STACK SIZE: " << RPN_stk.size() << "\n" ;
  136.             }
  137.             else
  138.             {
  139.                 for (unsigned int cur_char = 0; cur_char < cur_ins.length(); cur_char++)
  140.                 {
  141.                     if (cur_ins[cur_char] != '0')
  142.                     {
  143.                         only_0s = false;
  144.                     }
  145.                 }
  146.                 cur_num = atof (cur_ins.c_str());
  147.                 if (cur_num == 0)
  148.                 {
  149.                     if (only_0s)
  150.                     {
  151.                         RPN_stk.push (cur_num);
  152.                         cout << "PUSH: " << cur_num << ", STACK SIZE: " << RPN_stk.size() << "\n";
  153.                     }
  154.                     else
  155.                     {
  156.                         cout << "'" << cur_ins << "' IS NOT COUNTED AS A NUMBER OR OPERATOR" << endl;
  157.                         only_0s = true;
  158.                     }
  159.                 }
  160.                 else
  161.                 {
  162.                     RPN_stk.push (cur_num);
  163.                     cout << "PUSH: " << cur_num << ", STACK SIZE: " << RPN_stk.size() << "\n";
  164.                     only_0s = true;
  165.                 }
  166.             }
  167.         }
  168.         if (RPN_stk.size() == 1)
  169.         {
  170.             if (do_out) cout << "ANSWER: " << RPN_stk.top() << "\n" << endl;
  171.             RPN_stk.pop();
  172.         }
  173.         else if (RPN_stk.size() > 1)
  174.         {
  175.             if (do_out) cerr << "ERROR: Too many inputs / stack too large. STACK SIZE: " << RPN_stk.size() << endl;
  176.         }
  177.         else if (RPN_stk.size() < 1)
  178.         {
  179.             if (do_out) cerr << "ERROR: Too few inputs / stack too small. STACK SIZE: " << RPN_stk.size() << endl;
  180.         }
  181.         stk_size = RPN_stk.size();
  182.         for (unsigned int stk_pos = 0; stk_pos < stk_size; stk_pos++)
  183.         {
  184.             RPN_stk.pop();
  185.         }
  186.         do_out = true;
  187.     }
  188.     return 0;
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement