#include <iostream>
#include <string>
#include<stack>
using namespace std;
int PostFixRun (string ch)
{
stack<int> stk;
for(int i=0;i<ch.size();i++)
{
if(ch[i]>='0'&&ch[i]<='9')
{
int m=ch[i]-48;
stk.push(m);
}
else
{
int b=stk.top();
stk.pop();
int a=stk.top();
stk.pop();
if(ch[i]=='+')
{
stk.push(a+b);
}
else if(ch[i]=='-')
{
stk.push(a-b);
}
else if(ch[i]=='*')
{
stk.push(a*b);
}
else
{
if(b==0){cout<<"除数为0!"<<endl;return 0;}
stk.push(a/b);
}
}
}
return stk.top();
}
int main()
{
string str;
cout<<"请输入一个后缀表达式:"<<endl;
cin>>str;
int t=PostFixRun(str);
cout<<"表达式的值是:"<<t<<endl;
return 0;
}
标签:ch,--,C++,int,str,求值,include,PostFixRun
From: https://blog.csdn.net/m0_65788436/article/details/139573912