#include<iostream>
#include<string>
#include<stdio.h>
#include<stack>
using namespace std;
int main()
{
stack<int> f;
string k;
while(cin>>k)
{
if(k=="$")
{
cout<<f.top()<<endl;
while(!f.empty())
f.pop();
break;
}
else if(k=="+")
{
int a=f.top();
f.pop();
int b=f.top();
f.pop();
f.push(a+b);
}
else if(k=="-")
{
int a=f.top();
f.pop();
int b=f.top();
f.pop();
f.push(b-a);
}
else if(k=="*")
{
int a=f.top();
f.pop();
int b=f.top();
f.pop();
f.push(a*b);
}
else if(k=="/")
{
int a=f.top();
f.pop();
int b=f.top();
f.pop();
f.push(b/a);
}
else
f.push(atoi(k.c_str()));//其实就是为了这个才弄博客的。。string转换int,atoi函数
}
return 0;
}
输入后缀表达式LOW版
最新推荐文章于 2024-05-19 20:01:24 发布