-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP1935_후위_to_중위.java
62 lines (56 loc) · 2 KB
/
P1935_후위_to_중위.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package net.acmicpc.스택;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;
public class P1935_후위_to_중위 {
static Stack<Double> stack;
static double result;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine()); // 피연산자 수
double[] value = new double[N];
char[] chars = br.readLine().toCharArray();
int L = chars.length;
for (int i = 0; i < N; ++i) {
value[i] = Double.parseDouble(br.readLine());
}
stack = new Stack<>();
for (int i = 0; i < L; ++i) {
char c = chars[i];
switch (c) {
case '+':
double b1 = stack.pop();
double a1 = stack.pop();
result = a1 + b1;
stack.push(result);
break;
case '-':
double b2 = stack.pop();
double a2 = stack.pop();
result = a2 - b2;
stack.push(result);
break;
case '*':
double b3 = stack.pop();
double a3 = stack.pop();
result = a3 * b3;
stack.push(result);
break;
case '/':
double b4 = stack.pop();
double a4 = stack.pop();
result = a4 / b4;
stack.push(result);
break;
default:
stack.push(value[c - 'A']);
break;
}
}
System.out.printf("%.2f", stack.peek());
System.out.println();
// System.out.println(Character.getNumericValue('A')); // 10
// System.out.println(Character.getNumericValue('Z')); // 35
}
}