1935번 후위표기식2

1935번 후위표기식2 백준 알고리즘 문제

stack을 사용하는 기본적인 문제이다. stack을 연습해볼 때 풀어보기 좋은 문제다.

// 후위 표기식2
#include <cstdio>
#include <iostream>
#include <stack>
#include <string>
using namespace std;

int n;
double arr[27];
string str;
stack<double> st;

int main() {
  scanf("%d", &n);
  cin >> str;
  for (int i = 0; i < n; i++) {
    scanf("%lf", &arr[i]);
  }
  for (int i = 0; i < str.length(); i++) {
    if (str[i] >= 'A' && str[i] <= 'Z') {
      double temp = arr[str[i] - 'A'];
      st.push(temp);
    } else if (str[i] == '*') {
      double a = st.top();
      st.pop();
      double b = st.top();
      st.pop();
      st.push(b * a);
    } else if (str[i] == '/') {
      double a = st.top();
      st.pop();
      double b = st.top();
      st.pop();
      st.push(b / a);
    } else if (str[i] == '+') {
      double a = st.top();
      st.pop();
      double b = st.top();
      st.pop();
      st.push(b + a);
    } else if (str[i] == '-') {
      double a = st.top();
      st.pop();
      double b = st.top();
      st.pop();
      st.push(b - a);
    }
  }
  printf("%.2lf", st.top());
  return 0;
}