思路:
用stl的stack模拟就好了,感觉比一些L1的都简单好多,而且没有坑点
代码:
#include<iostream>
#include<cstdio>
#include<stack>
using namespace std;
int n,a,ans;
char ch;
stack<int> s1;
stack<char> s2;
char op;
int main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>a;
s1.push(a);
}
for(int i=1;i<n;i++){
cin>>ch;
s2.push(ch);
}
while(!s2.empty()){
int n1=s1.top();
s1.pop();
int n2=s1.top();
s1.pop();
char op=s2.top();
s2.pop();
if(op=='/'){
if(!n1){
printf("ERROR: %d/0\n",n2);
ans=-1;
break;
}
else ans=n2/n1;
}
else if(op=='+') ans=n1+n2;
else if(op=='-') ans=n2-n1;
else if(op=='*') ans=n1*n2;
s1.push(ans);
}
if(ans!=-1) cout<<ans<<endl;
system("pause");
}