Algorithm/Java

[백준/JAVA] 후위 표기식2

dbfl9911 2024. 10. 17. 10:37
반응형

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


- 문제 풀이

==> 후위 표기식 
ABC*+DE/-
==> 원래 식
A+(B*C)-(D/E)

💡스택을 사용해 피연산자(A,B,C..)가 나오면 스택에 집어넣고 연산자가 나올 때마다 스택에 있는 피연산자를 꺼내 연산!

ex) 스택에 A,B,C가 들어있고 다음에 나오는 피연산자 *가 나오면 스택 상단에 있는 두개의 피연산자를 꺼내(.pop())

      피연산자와 연산한다 (B*C) 

 


 

- 정답 코드

import java.io.*;
import java.util.Stack;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(br.readLine()); // 5
        String s = br.readLine(); // ABC*+DE/-
        // 숫자 담을 배열
        double[] arr = new double[N];
        for(int i = 0; i < arr.length; i++) {
            arr[i] = Double.parseDouble(br.readLine()); // 1 2 3 4 5
        }
        // 피연산자가 나올때마다 담을 스택
        Stack<Double> stack = new Stack<>();

        double answer = 0;

        for(int i = 0; i < s.length(); i++) {
            // 피연산자가 나왔을 때
            if(s.charAt(i) >= 'A' && s.charAt(i) <= 'Z') {
                // 알파벳 - 'A' 차이 = 알파벳의 순서
                // ex) 'A' - 'A' = 0
                //     'B' - 'A' = 1
                stack.add(arr[s.charAt(i) - 'A']);
            // 연산자가 나왔을 때
            }else{
                double a = stack.pop(); // C
                double b = stack.pop(); // B
                switch(s.charAt(i)) {
                    case '+':
                        answer = b + a;
                        stack.push(answer);
                        continue;
                    case '-':
                        answer = b - a;
                        stack.push(answer);
                        continue;
                    case '*':
                        answer = b * a;
                        stack.push(answer);
                        continue;
                    case '/':
                        answer = b / a;
                        stack.push(answer);
                        continue;
                }
            }
        }
        // 소숫점 둘째 자리까지 출력
        System.out.printf("%.2f",stack.pop());
    }
}
반응형