백준 1935번: 후위 표기식2

Se0ng_1l·2022년 7월 15일
0

백준

목록 보기
35/40

https://www.acmicpc.net/problem/1935

⭐️문제 접근

  1. 스택이용
  2. 2째줄 입력한 문자열의 크기만큼 반복문 수행
  3. 사칙연산기호가 오면 스택의 탑을 pop해서 연산을 수행하고 top에 값 넣기
  4. 영문자가 오면 선언해둔 배열에 해당 문자열의 값을 확인하고 0이면 새로 값을 입력하여 넣고 stack에도 push 아니라면 불러와서 스택에 push
#include <iostream>
#include <stack>
using namespace std;

int main()
{
    int alpha[26] = {0,};
    int num;
    cin >> num;
    string str;
    cin >> str;
    double a;
    stack<double> sd;
    double temp;
    for(int j = 0; j < str.size(); j++){
        switch (str[j]){
            case '+':
                temp = sd.top();
                sd.pop();
                temp += sd.top();
                sd.pop();
                sd.push(temp);
                break;
            case '-':
                temp = sd.top();
                sd.pop();
                sd.top() -= temp;
                break;
            case '*':
                temp = sd.top();
                sd.pop();
                sd.top() *= temp;
                break;
            case '/':
                temp = sd.top();
                sd.pop();
                sd.top() /= temp;
                break;
            default:
                if(alpha[str[j] - 'A'] == 0){
                    cin >> a;
                    alpha[str[j] - 'A'] += (int)a;
                }
                sd.push(alpha[str[j] - 'A']);
        }
    }
    printf("%.2f", sd.top());
}
profile
치타가 되고 싶은 취준생

0개의 댓글