fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. class Stack{
  5. private:
  6. int *arr { nullptr };
  7. int size {};
  8. int top {};
  9. public:
  10. Stack(){
  11. size = 20;
  12. arr = new int[size]{ };
  13. top = -1;
  14. }
  15. ~Stack(){
  16. delete[] arr;
  17. arr = nullptr;
  18. }
  19. void push(char n){
  20. if(is_full())
  21. cout << "Stack overflow!!\n";
  22. assert(-1 <= top && top < size);
  23. arr[++top] = n;
  24. }
  25. int pop(){
  26. if(is_empty())
  27. cout << "The Stack is empty!!\n";
  28. assert(0 <= top && top < size);
  29. return arr[top--];
  30. }
  31. int peek(){
  32. return arr[top];
  33. }
  34. bool is_empty(){
  35. if (top == -1)
  36. return true;
  37. return false;
  38. }
  39. bool is_full(){
  40. if(top == size-1)
  41. return true;
  42. return false;
  43. }
  44. double PostfixEvaluate(string Ex){
  45. Stack stk; int N = 0; bool ok = false;
  46. for(int i=0; i<Ex.size(); i++){
  47. if (isdigit(Ex[i])){
  48. if(!ok) ok = true;
  49. N = N*10 + (Ex[i]-'0');
  50. }
  51. else {
  52. if (ok) stk.push(N), ok = false, N = 0;
  53. if(Ex[i]==' ')
  54. continue;
  55. int y = stk.pop();
  56. int x = stk.pop();
  57. if (Ex[i]=='+') stk.push(x+y);
  58. else if (Ex[i]=='-') stk.push(x-y);
  59. else if (Ex[i]=='*') stk.push(x*y);
  60. else if (Ex[i]=='/') stk.push(x/y);
  61. }
  62. }
  63. return stk.peek();
  64. }
  65. string infix2postfix(string infix){
  66. Stack stk;
  67. stk.push('(');
  68. infix.push_back(')');
  69. string postfix = "";
  70. for(int i = 0; i < infix.size(); i++){
  71. if(isdigit(infix[i]))
  72. postfix.push_back(infix[i]);
  73. else{
  74. if(infix[i] == '(')
  75. stk.push('(');
  76. else if(infix[i] == '*' || infix[i] == '/'){
  77. if(stk.peek() == '/' || stk.peek() == '*'){
  78. while (stk.peek() != '('){
  79. postfix.push_back(stk.pop());
  80. }
  81. }
  82. stk.push(infix[i]);
  83. }
  84. else if(infix[i] == '+' || infix[i] == '-'){
  85. if(stk.peek() != '('){
  86. while (stk.peek() != '('){
  87. postfix.push_back(stk.pop());
  88. }
  89. }
  90. stk.push(infix[i]);
  91. }
  92. else if(infix[i] == ')'){
  93. while (stk.peek() != '('){
  94. postfix.push_back(stk.pop());
  95. } stk.pop();
  96. }
  97. }
  98. }
  99. return postfix;
  100. }
  101. };
  102.  
  103. int main() {
  104. Stack A;
  105. /*A.push(1);
  106.   A.push(2);
  107.   A.push(3);
  108.   A.push(4);
  109.   A.push(5);
  110.   cout << A.peek() << '\n';
  111.   A.pop();
  112.   A.pop();
  113.   cout << A.peek() << '\n';*/
  114. //cout << A.PostfixEvaluate("31 82 2 / + 5 -") << '\n'; //67
  115. //cout << A.PostfixEvaluate("3 8 2/+5 -") << '\n'; //2
  116. //cout << A.infix2postfix("1+2") << '\n'; //12+
  117. //cout << A.infix2postfix("(1+2*3-2)") << '\n'; //123*+2-
  118. //cout << A.infix2postfix("(1+2)*3-2") << '\n'; //12+3*2-
  119. return 0;
  120. }
  121.  
Success #stdin #stdout 0.01s 5292KB
stdin
Standard input is empty
stdout
Standard output is empty