#include <bits/stdc++.h>
using namespace std;
class Stack{
private:
int *arr { nullptr };
int size {};
int top {};
public:
Stack(){
size = 20;
arr = new int[size]{ };
top = -1;
}
~Stack(){
delete[] arr;
arr = nullptr;
}
void push(char n){
if(is_full())
cout << "Stack overflow!!\n";
assert(-1 <= top && top < size);
arr[++top] = n;
}
int pop(){
if(is_empty())
cout << "The Stack is empty!!\n";
assert(0 <= top && top < size);
return arr[top--];
}
int peek(){
return arr[top];
}
bool is_empty(){
if (top == -1)
return true;
return false;
}
bool is_full(){
if(top == size-1)
return true;
return false;
}
double PostfixEvaluate(string Ex){
Stack stk; int N = 0; bool ok = false;
for(int i=0; i<Ex.size(); i++){
if (isdigit(Ex[i])){
if(!ok) ok = true;
N = N*10 + (Ex[i]-'0');
}
else {
if (ok) stk.push(N), ok = false, N = 0;
if(Ex[i]==' ')
continue;
int y = stk.pop();
int x = stk.pop();
if (Ex[i]=='+') stk.push(x+y);
else if (Ex[i]=='-') stk.push(x-y);
else if (Ex[i]=='*') stk.push(x*y);
else if (Ex[i]=='/') stk.push(x/y);
}
}
return stk.peek();
}
string infix2postfix(string infix){
Stack stk;
stk.push('(');
infix.push_back(')');
string postfix = "";
for(int i = 0; i < infix.size(); i++){
if(isdigit(infix[i]))
postfix.push_back(infix[i]);
else{
if(infix[i] == '(')
stk.push('(');
else if(infix[i] == '*' || infix[i] == '/'){
if(stk.peek() == '/' || stk.peek() == '*'){
while (stk.peek() != '('){
postfix.push_back(stk.pop());
}
}
stk.push(infix[i]);
}
else if(infix[i] == '+' || infix[i] == '-'){
if(stk.peek() != '('){
while (stk.peek() != '('){
postfix.push_back(stk.pop());
}
}
stk.push(infix[i]);
}
else if(infix[i] == ')'){
while (stk.peek() != '('){
postfix.push_back(stk.pop());
} stk.pop();
}
}
}
return postfix;
}
};
int main() {
Stack A;
/*A.push(1);
A.push(2);
A.push(3);
A.push(4);
A.push(5);
cout << A.peek() << '\n';
A.pop();
A.pop();
cout << A.peek() << '\n';*/
//cout << A.PostfixEvaluate("31 82 2 / + 5 -") << '\n'; //67
//cout << A.PostfixEvaluate("3 8 2/+5 -") << '\n'; //2
//cout << A.infix2postfix("1+2") << '\n'; //12+
//cout << A.infix2postfix("(1+2*3-2)") << '\n'; //123*+2-
//cout << A.infix2postfix("(1+2)*3-2") << '\n'; //12+3*2-
return 0;
}