File tree Expand file tree Collapse file tree 1 file changed +48
-0
lines changed
src/main/java/sgyj/inflearn/yeji/week4 Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments