Advertisement
Guest User

RPN Calc

a guest
Oct 20th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. /*
  2. Lily O'Malley
  3. Date: 1/20/17
  4. Description: This is a modification of a simple
  5. RPN calculator to add multiplication and devision
  6. operations to the calculator alongside a command
  7. to display entered operands.
  8. */
  9. #include <iostream>
  10. #include <vector>
  11. #include <stack>
  12. #include <stdio.h>
  13. using namespace std;
  14.  
  15. int
  16. main()
  17. {
  18. stack<double> operands;
  19. stack<double> temp;
  20.  
  21. double nextNumber;
  22. char nextOperation;
  23.  
  24. double operand1;
  25. double operand2;
  26.  
  27. cout << "Type \"q\" when you are done\n";
  28.  
  29. while ( cin && cin.peek() != EOF )
  30. {
  31. char ch = cin.peek();
  32.  
  33. switch ( ch )
  34. {
  35. // digit or decimal point
  36. case '0':
  37. case '1':
  38. case '2':
  39. case '3':
  40. case '4':
  41. case '5':
  42. case '6':
  43. case '7':
  44. case '8':
  45. case '9':
  46. case '.':
  47. cin >> nextNumber;
  48. operands.push( nextNumber );
  49. break;
  50.  
  51. // arithmetic operation
  52. case '+':
  53. case '-':
  54. case '*':
  55. case '/':
  56. cin >> nextOperation;
  57.  
  58. // handle unary minus
  59. if ( ( cin.peek() >= '0' && cin.peek() <= '9' ) || cin.peek() == '.' )
  60. {
  61. cin >>nextNumber;
  62. operands.push( -nextNumber );
  63. break;
  64. }
  65.  
  66. // do binary operation
  67. if ( operands.size() < 2 )
  68. {
  69. cout << "Not enough operands for " << nextOperation << "\n";
  70. }
  71. else
  72. {
  73. operand2 = operands.top();
  74. operands.pop();
  75. operand1 = operands.top();
  76. operands.pop();
  77.  
  78. switch ( nextOperation )
  79. {
  80. case '+':
  81. operands.push( operand1 + operand2 );
  82. break;
  83. case '-':
  84. operands.push( operand1 - operand2 );
  85. break;
  86. case '*':
  87. operands.push( operand1 * operand2 );
  88. break;
  89. case '/':
  90. operands.push( operand1 / operand2 );
  91. break;
  92. }
  93.  
  94. cout << "-----\n";
  95. cout << operands.top() << "\n\n";
  96. }
  97.  
  98. break;
  99.  
  100. // display stack operation
  101. case ':':
  102.  
  103. //INFINITE LOOP -
  104. for (temp = operands; !temp.empty(); temp.pop())
  105. {
  106. cout << "Operand: " << temp.top() << endl;
  107. }
  108.  
  109. /*
  110. while (!operands.empty())
  111. {
  112. cout << operands.top() << endl;
  113. operands.pop();
  114. }
  115. */
  116.  
  117. break;
  118.  
  119. // ignore other characters
  120. default:
  121. cin.ignore();
  122. break;
  123. }
  124.  
  125. if (ch == 'q')
  126. break;
  127. }
  128.  
  129. // report errors
  130. if ( operands.size() > 1 )
  131. {
  132. cout << "Left over operands in the stack\n";
  133. return -1;
  134. }
  135.  
  136. return 0; // normal exit code
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement