一、题目
二、解题思路
按照题意定义两个栈进行运算。
三、代码
#include<iostream>
using namespace std;
#include<stack>
int main()
{
int n;
stack<int> s1;
stack<char> s2;
cin>>n;
for(int i=0;i<n;i++)
{
int t;
cin>>t;
s1.push(t);
}
for(int i=1;i<n;i++)
{
char t;
cin>>t;
s2.push(t);
}
while(s1.size()>1)
{
int n1=s1.top();
s1.pop();
int n2=s1.top();
s1.pop();
char op=s2.top();
s2.pop();
if(op=='+')
{
s1.push(n2+n1);
}
else if(op=='-')
{
s1.push(n2-n1);
}
else if(op=='*')
{
s1.push(n2*n1);
}
else if(op=='/')
{
if(n1!=0)
{
s1.push(n2/n1);
}
else
{
cout<<"ERROR: "<<n2<<"/0";
return 0;
}
}
}
cout<<s1.top();
return 0;
}
四、总结
比较简单的题,注意两个栈一个是 int ,一个是 char 。