-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMain.java
186 lines (162 loc) Β· 4.86 KB
/
Main.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package Stack.P3425;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Stack;
public class Main {
static Program pro;
public static void main(String[] args) throws Exception {
System.setIn(new FileInputStream("src/Stack/P3425/input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String cmd = br.readLine();
while (!cmd.equals("QUIT")) {
ArrayList<String> commands = new ArrayList<>();
while(!cmd.equals("END")) {
commands.add(cmd);
cmd = br.readLine();
}
int N = Integer.parseInt(br.readLine());
for (int i = 0; i < N; i++) {
int num = Integer.parseInt(br.readLine());
pro = new Program(num, commands);
}
System.out.println("");
br.readLine();
cmd = br.readLine();
}
}
}
class Program {
Stack<Integer> stack = new Stack<>();
final int MAX = 1000000000;
boolean error = false;
public Program(int num, ArrayList<String> commands) {
this.stack.push(num);
for (String cmd : commands) {
if (this.error) break;
if (cmd.matches(".*[0-9]")) {
this.num(Integer.parseInt(cmd.substring(4)));
} else {
switch (cmd) {
case "POP":
this.pop();
break;
case "INV":
this.inv();
break;
case "DUP":
this.dup();
break;
case "SWP":
this.swp();
break;
default:
this.oper(cmd);
break;
}
}
}
if (this.error || stack.size() != 1) System.out.println("ERROR");
else System.out.println(stack.pop());
}
void num(int x) {
this.stack.push(x);
}
void pop() {
if (this.validSize(1)) this.stack.pop();
}
void inv() {
if (!this.validSize(1)) return;
int target = this.stack.pop();
this.stack.push(target * (-1));
}
void dup() {
if (!this.validSize(1)) return;
int target = this.stack.peek();
this.stack.push(target);
}
void swp() {
if (!this.validSize(2)) return;
int first = this.stack.pop();
int second = this.stack.pop();
this.stack.push(first);
this.stack.push(second);
}
void oper(String o) {
if (!this.validSize(2)) return;
int first = this.stack.pop();
int second = this.stack.pop();
switch (o) {
case "ADD":
this.add(second, first);
break;
case "SUB":
this.sub(second, first);
break;
case "MUL":
this.mul(second, first);
break;
case "DIV":
this.div(second, first);
break;
case "MOD":
this.mod(second, first);
break;
default:
break;
}
}
void add(int x, int y) {
long result = x + y;
if (this.validNum(result)) this.stack.push((int) result);
}
void sub(int x, int y) {
long result = x - y;
if (this.validNum(result)) this.stack.push((int) result);
}
void mul(int x, int y) {
if (x != 0 && y!= 0) {
int x_len = (int) Math.log10(x) + 1;
int y_len = (int) Math.log10(y) + 1;
if (x_len + y_len > 10) {
this.error = true;
return;
}
}
long result = x * y;
if (this.validNum(result)) this.stack.push((int) result);
}
void div(int x, int y) {
if (y == 0) {
this.error = true;
return;
}
long result = Math.abs(x) / Math.abs(y);
if ((x < 0 && y > 0) || (x > 0 && y < 0)) result *= -1;
if (this.validNum(result)) this.stack.push((int) result);
}
void mod(int x, int y) {
if (y == 0) {
this.error = true;
return;
}
long result = Math.abs(x) % Math.abs(y);
if (x < 0) result *= -1;
if (this.validNum(result)) this.stack.push((int) result);
}
boolean validSize(int targetSize) {
if (this.stack.size() < targetSize) {
this.error = true;
return false;
}
return true;
}
boolean validNum(long target) {
if (Math.abs(target) > this.MAX) {
this.error = true;
return false;
}
return true;
}
}