Skip to content

Commit 97d8d9a

Browse files
committed
feat : 인프런 3주차 39 후위식 연산
1 parent 9fee723 commit 97d8d9a

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package sgyj.inflearn.yeji.week4;
2+
3+
import java.util.Scanner;
4+
import java.util.Stack;
5+
6+
7+
class Solution39{
8+
// 후위식 연산
9+
public static int solution(String[] input){
10+
Stack<Integer> stack = new Stack();
11+
12+
for(String i : input){
13+
if(isNumeric(i)){
14+
stack.push(Integer.parseInt(i));
15+
}else{
16+
stack.push(checkNum(stack.pop(),stack.pop(), i));
17+
}
18+
}
19+
return stack.pop();
20+
}
21+
22+
public static boolean isNumeric(String i){
23+
try{
24+
Integer.parseInt(i);
25+
return true;
26+
}catch(NumberFormatException e){
27+
return false;
28+
}
29+
}
30+
31+
public static int checkNum(int stack1, int stack2, String target){
32+
if(target.equals("+")){
33+
return stack2+stack1;
34+
}else if(target.equals("-")){
35+
return stack2-stack1;
36+
}else if(target.equals("*")){
37+
return stack2*stack1;
38+
}
39+
return stack2/stack1;
40+
}
41+
42+
public static void main(String[] args){
43+
Scanner sc = new Scanner(System.in);
44+
String input = sc.nextLine();
45+
System.out.println(solution(input.split("")));
46+
}
47+
48+
}

0 commit comments

Comments
 (0)