[백준] 1935 후위 표기식 2
#include <algorithm>
#include <string>
#include <stack>
#include <vector>
#include <iostream>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int n;
cin >> n;
string str;
cin >> str;
vector<double> alpha;
for (int i = 0; i < n; ++i) {
double input;
cin >> input;
alpha.push_back(input);
}
stack<double> st;
for (int i = 0; i < str.length(); ++i) {
char ch = str[i];
if (ch == '+') {
double db2 = st.top();
st.pop();
double db1 = st.top();
st.pop();
st.push(db1 + db2);
}
else if (ch == '-') {
double db2 = st.top();
st.pop();
double db1 = st.top();
st.pop();
st.push(db1 - db2);
}
else if (ch == '*') {
double db2 = st.top();
st.pop();
double db1 = st.top();
st.pop();
st.push(db1 * db2);
}
else if (ch == '/') {
double db2 = st.top();
st.pop();
double db1 = st.top();
st.pop();
st.push(db1 / db2);
}
else {
st.push(alpha[ch - 'A']);
}
}
cout << fixed;
cout.precision(2);
cout << st.top();
return 0;
}