-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostfixStack.cpp
52 lines (47 loc) · 920 Bytes
/
postfixStack.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include<iostream>
#include<string>
using namespace std;
int t,n;
class Stack{
private:
int size=-1;
int arr[100];
public:
void push(int x);
int pop();
};
void Stack::push(int x) {
size++;
arr[size]=x;
}
int Stack::pop() {
return arr[size--];
}
int main(){
cin>>t;
string str;
Stack st;
for(int i=0;i<t;i++){
cin>>str;
for(int j=0;j<str.length();j++){
if(str[j]-'0'>=0&&str[j]-'0'<=9){
st.push(str[j]-'0');
}
else{
int a=st.pop();
int b=st.pop();
if(str[j]=='+'){
st.push(a+b);
}
else if(str[j]=='-'){
st.push(b-a);
}
else if(str[j]=='*'){
st.push(a*b);
}
}
}
cout<<st.pop()%10<<endl;
}
return 0;
}