-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBOJ_15815.java
More file actions
38 lines (32 loc) · 1.21 KB
/
Copy pathBOJ_15815.java
File metadata and controls
38 lines (32 loc) · 1.21 KB
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
// BOJ - 15815
// Problem Sheet - https://www.acmicpc.net/problem/15815
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
Stack<String> stack = new Stack<>();
String[] expressionElements = bf.readLine().split("");
int operandA, operandB;
for(String element : expressionElements) {
if(Character.isDigit(element.charAt(0))) {
stack.push(element);
} else { // is operator
operandB = Integer.parseInt(stack.pop());
operandA = Integer.parseInt(stack.pop());
stack.push(Integer.toString(calculate(operandA, operandB, element)));
}
}
System.out.println(stack.pop());
bf.close();
System.exit(0);
}
static int calculate(int operandA, int operandB, String operator) {
switch (operator) {
case "+": return operandA + operandB;
case "-": return operandA - operandB;
case "*": return operandA * operandB;
default: return operandA / operandB;
}
}
}