-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpolish.java
85 lines (66 loc) · 2.7 KB
/
polish.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Brett Fazio
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
public class polish {
public static void main(String[] args) throws IOException {
BufferedReader bu = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
int cases = 1;
String s = bu.readLine();
while (s != null && s.equals("") == false) {
String[] in = s.split(" ");
ArrayList<String> list = new ArrayList<>();
for (String i : in) {
list.add(i);
}
boolean change = true;
while (change) {
change = false;
for (int i =0; i < list.size()-2; i++) {
String cur = list.get(i);
String next = list.get(i+1);
String nexter = list.get(i+2);
if (cur.equals("+") || cur.equals("-") || cur.equals("*")) {
if (isInteger(next,10) && isInteger(nexter,10)) {
list.remove(i+1);
list.remove(i+1);
change = true;
if (cur.equals("+")) {
list.set(i, Integer.parseInt(next)+Integer.parseInt(nexter)+"");
}else if (cur.equals("-")) {
list.set(i, Integer.parseInt(next)-Integer.parseInt(nexter)+"");
}else if (cur.equals("*")) {
list.set(i, Integer.parseInt(next)*Integer.parseInt(nexter)+"");
}
}
}
}
}
out.print("Case ");
out.print(cases);
out.print(": ");
for (String ss : list) {
out.print(ss);
out.print(' ');
}
out.println();
cases++;
s = bu.readLine();
}
out.close();
}
public static boolean isInteger(String s, int radix) {
if(s.isEmpty()) return false;
for(int i = 0; i < s.length(); i++) {
if(i == 0 && s.charAt(i) == '-') {
if(s.length() == 1) return false;
else continue;
}
if(Character.digit(s.charAt(i),radix) < 0) return false;
}
return true;
}
}