fork download
  1. #include <iostream>
  2. #include <stack> //stack header file
  3.  
  4. using namespace std;
  5.  
  6. stack <int> operands;
  7.  
  8.  
  9. int main()
  10. {
  11. int input();
  12. void evaluate();
  13.  
  14. }
  15.  
  16.  
  17.  
  18. int input(int, char) //user inputs 3 items into a stack
  19. {
  20. int item1, item2, item3; //push 3 input items onto a stack and ask for the two operators
  21. char op, op2;
  22. cout << "Enter your postfix expression" << endl;
  23. cout << "(Enter three numbers followed by two operators, one per line)" << endl;
  24. cin >> item1;
  25. operands.push(item1);
  26. cin >> item2;
  27. operands.push(item2);
  28. cin >> item3;
  29. operands.push(item3);
  30. cin >> op;
  31. cin >> op2;
  32. return op, op2, item1, item2, item3;
  33.  
  34. }
  35.  
  36. void evaluate(char &op, char &op2, int &item1, int &item2, int &item3) //evaluate function operations included
  37. {
  38.  
  39.  
  40. item3 = operands.top(); //ask for the second operator and do the destignated operation
  41. operands.pop();
  42. item2 = operands.top();
  43. operands.pop();
  44. if(op =='+')
  45. item2 = item3+item2;
  46. else if(op=='*')
  47. item2 = item3*item2;
  48. else if(op=='-')
  49. item2 = item3-item2;
  50. else if(op=='/')
  51. item2 = item3/item2;
  52. else if(op=='%')
  53. item2 = item3%item2;
  54. else
  55. cout << "You have entered an incorrect operator" << endl;
  56. operands.push(item2); //push the result back to top of stack
  57.  
  58. operands.pop();
  59. operands.pop();
  60.  
  61. if(op2 =='+')
  62. item1 = item2+item1;
  63. else if(op2=='*')
  64. item1 = item2+item1;
  65. else if(op2=='-')
  66. item1 = item2-item1;
  67. else if(op2=='/')
  68. item1 = item2/item1;
  69. else if(op2=='%')
  70. item1=item2%item1;
  71. else
  72. cout << "You have entered an incorrect operator" << endl;
  73. operands.push(item1); //push result back to top of stack
  74. cout << "The number remaining in the stack after those operations is " << item1 << endl;
  75.  
  76. }
  77.  
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
Standard output is empty