백준 - 15851번 천재 수학자 성필 (Silver III)
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] arr = br.readLine().split("");
Stack<Integer> numbers = new Stack<>();
Queue<Character> signs = new LinkedList<>();
String sign = "+-*/";
for (int i = 0; i < arr.length; i++) {
if (i == 0) {
numbers.push(Integer.parseInt(arr[i]));
} else {
if (sign.indexOf(arr[i]) == -1) {
if (sign.indexOf(arr[i-1]) != -1) {
func(numbers, signs);
numbers.push(Integer.parseInt(arr[i]));
} else {
numbers.push(Integer.parseInt(arr[i]));
}
} else {
signs.add(arr[i].charAt(0));
}
}
}
func(numbers, signs);
System.out.println(numbers.pop());
}
public static void func(Stack<Integer> numbers, Queue<Character> signs) {
while(!signs.isEmpty()) {
char sign = signs.poll();
int a = numbers.pop();
int b = numbers.pop();
if (sign == '+') {
numbers.push(a + b);
} else if (sign == '-') {
numbers.push(b - a);
} else if (sign == '*') {
numbers.push(a * b);
} else {
numbers.push(b / a);
}
}
}
}