题如其名,简单,栈最基本用法,题怎么说就怎么做,没有坑。
Code:
#include<bits/stdc++.h>
using namespace std;
stack<int> S1;
stack<char> S2;
int main(){int N, M;cin >> N;M = N - 1;while(N--){int v;cin >> v;S1.push(v);}while(M--){char op;cin >> op;S2.push(op);}while(!S2.empty()){int n1, n2;char op;n1 = S1.top();S1.pop();n2 = S1.top();S1.pop();op = S2.top();S2.pop();int res;if(op == '+'){res = n1 + n2;}else if(op == '-'){res = n2 - n1;}else if(op == '*'){res = n2 * n1;}else if(op == '/'){if(n1 == 0){cout << "ERROR: " << n2 << "/0";return 0;}res = n2 / n1;}S1.push(res);}cout << S1.top();return 0;
}