후위 연산식 : 연산 기호를 피 연산자 뒤에 두는 방법
일상생활에서 쓰는 1+1 은 중위연산식 후위는 1 1 +
문제 해결 방법 =>
1. 문자를 만나면 stack에 넣는다
2. 연산기호를 만나면 stack의 원소 2개를 불러와 연산식을 진행한 후 stack에 넣는다
1, 2를 반복한다
반복 한 후에 stack에 남은 원소 하나가 결과 값
#include<bits/stdc++.h>
using namespace std;
double alpha[27];
int main(){
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
cout.precision(2); cout << fixed;
int n; cin >> n;
string s; cin >> s;
for (int i = 0; i < n; i++)
cin >> alpha[i];
stack<double> stk;
for (int i = 0; i < s.length(); i++) {
if (s[i] >= 'A' && s[i] <= 'Z')
stk.push(alpha[s[i] - 'A']);
else {
double a = stk.top(); stk.pop();
double b = stk.top(); stk.pop();
if (s[i] == '*')
stk.push(b * a);
else if(s[i] == '+')
stk.push(b + a);
else if(s[i] == '-')
stk.push(b - a);
else if(s[i] == '/')
stk.push(b / a);
}
}
cout << stk.top() << '\n';
return 0;
}