File tree Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+
3
+ 처음 라인은 케이스
4
+ 두번째는 표현식 -> 스택으로 처리 예정
5
+ 세번째는 숫자 -> 순서대로 해시에 저장할 예정
6
+
7
+ */
8
+ import java .util .*;
9
+ import java .io .*;
10
+ public class Boj1935 {
11
+
12
+ public static void main (String ...args ) throws Exception {
13
+
14
+ var br = new BufferedReader (new InputStreamReader (System .in ));
15
+
16
+ var valueTable = new LinkedHashMap <Character ,Double >();
17
+ var stack = new Stack <Double >();
18
+
19
+ var numberOfLine = Integer .parseInt (br .readLine ());
20
+ var expression = br .readLine ();
21
+
22
+ for (int i = 0 ; i < numberOfLine ; i ++){
23
+ Character key = (char )(65 + i );
24
+ valueTable .put (key , Double .parseDouble (br .readLine ()));
25
+ }
26
+
27
+ for (var element : expression .toCharArray ()){
28
+ // 연산자가 아닌 경우
29
+ if (valueTable .containsKey (element )){
30
+ stack .add (valueTable .get (element ));
31
+ continue ;
32
+ }
33
+
34
+ var last = stack .pop ();
35
+ var first = stack .pop ();
36
+
37
+ var result = switch (element ){
38
+ case '+' -> first + last ;
39
+ case '-' -> first - last ;
40
+ case '*' -> first * last ;
41
+ case '/' -> first / last ;
42
+ default -> throw new IllegalArgumentException (); // 존재하지 않는 연산자.
43
+ };
44
+ stack .push (result );
45
+ }
46
+ System .out .printf ("%.2f\n " , stack .pop ());
47
+ }
48
+ }
You can’t perform that action at this time.
0 commit comments