#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=100000+10;
typedef struct node
{
int *data;
int top;
int bottom;
}Stack;
Stack S;
void initstack(Stack &S)
{
S.data=new int [maxn];
S.top=S.bottom=0;
}
void push(Stack &S, int s)
{
S.data[S.top]=s;
S.top++;
}
void pop(Stack &S)
{
S.top--;
cout<<S.data[S.top]<<endl;
}
int main()
{
ios::sync_with_stdio(false);
Stack S;
string s;
initstack(S);
cin>>s;
for(int i=0; s[i]!='#';i++)
{
if(s[i]=='*')
{
S.data[S.top-2]=S.data[S.top-1]*S.data[S.top-2];
S.top-=1;
}
else if(s[i]=='+')
{
S.data[S.top-2]=S.data[S.top-1]+S.data[S.top-2];
S.top-=1;
}
else if(s[i]=='-')
{
S.data[S.top-2]=S.data[S.top-2]-S.data[S.top-1];
S.top-=1;
}
else if(s[i]=='/')
{
S.data[S.top-2]=S.data[S.top-2]/S.data[S.top-1];
S.top-=1;
}
else
{
int x=s[i]-'0';
push(S, x);
}
}
cout<<S.data[S.top-1]<<endl;
return 0;
}
2133 数据结构实验之栈三:后缀式求值
最新推荐文章于 2020-10-11 15:35:50 发布