import java.util.*;
class Main {
public int solution(String str){
int answer=0;
Stack<Integer> stack = new Stack<>();
for(char x : str.toCharArray()) {
if(Character.isDigit(x)) stack.push(x-48); // 48 ='0' '5'-48 -> 숫자 5가 됨
else{
//숫자 두개꺼내기. 항상 lt 연산자 rt로 연산해야함
int rt = stack.pop();
int 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);
}
}
answer = stack.get(0);
return answer;
}
public static void main(String[] args){
Main T = new Main();
Scanner kb = new Scanner(System.in);
String str=kb.next();
System.out.println(T.solution(str));
}
}
'Coding Test Java' 카테고리의 다른 글
인프런 -공주구하기 (0) | 2024.01.29 |
---|---|
인프런 - 쇠막대기 (0) | 2024.01.29 |
인프런 - 크레인 인형 뽑기 (카카오) (0) | 2024.01.29 |
인프런 - 괄호 문자 제거 (0) | 2024.01.29 |
인프런 - 모든 아나그램 찾기 (0) | 2024.01.22 |