[알고리즘] 05.Stack, Queue(자료구조) / 04. 후위식 연산(postfix)

less than 1 minute read

후위식 연산(postfix)

설명

후위연산식이 주어지면 연산한 결과를 출력하는 프로그램을 작성하세요.
만약 3(5+2)-9 을 후위연산식으로 표현하면 352+9- 로 표현되며 그 결과는 12입니다.

입력

첫 줄에 후위연산식이 주어집니다. 연산식의 길이는 50을 넘지 않습니다.
식은 1~9의 숫자와 +, -, *, / 연산자로만 이루어진다.

출력

연산한 결과를 출력합니다.

예시 입력 1

352+*9-

예시 출력 1

12

느낀점

후위연산식 문제를 처음 접해봄

코드

import java.util.Scanner;
import java.util.Stack;
public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Main T = new Main();
		Scanner sc = new Scanner(System.in);
		String str = sc.next();
		System.out.print(T.solution(str));
	}

	/*
	352+*9-
	*/

	public int solution(String str) {
		Stack<Integer> stack = new Stack<>();
		int lt, rt =0;

		for(char x : str.toCharArray()) {
			if(Character.isDigit(x)) {
				stack.push(x-48);
			}else {
				rt = stack.pop();
				lt = stack.pop();

				if(x == '+') {
					stack.push(lt + rt);
				}else if(x == '-') {
					stack.push(lt - rt);
				}else if(x == '*') {
					stack.push(lt * rt);
				}else if(x == '/') {
					stack.push(lt / rt);
				}
			}
		}
		return stack.get(0);
	}
}