-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculator.java
More file actions
42 lines (42 loc) · 1.5 KB
/
Calculator.java
File metadata and controls
42 lines (42 loc) · 1.5 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
39
40
41
42
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Stack;
public class Calculator {
public static void main(String[] args) throws IOException {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out))) {
String line = reader.readLine();
String[] ch = line.split(" ");
System.out.println(polish_calc(ch));
}
}
private static Integer polish_calc(String[] dataArray) {
Stack<Integer> stack = new Stack<>();
for (String el : dataArray) {
switch (el) {
case "-":
int second = stack.pop();
stack.push(stack.pop() - second);
break;
case "+":
stack.push(stack.pop() + stack.pop());
break;
case "*":
stack.push(stack.pop() * stack.pop());
break;
case "/":
int second1 = stack.pop();
int result = Math.floorDiv(stack.pop(), second1);
stack.push(result);
break;
default:
stack.push(Integer.parseInt(el));
break;
}
}
return stack.pop();
}
}