|
| 1 | +package Stack.P3425; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.FileInputStream; |
| 5 | +import java.io.InputStreamReader; |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.Stack; |
| 8 | + |
| 9 | +public class Main { |
| 10 | + static Program pro; |
| 11 | + |
| 12 | + public static void main(String[] args) throws Exception { |
| 13 | + System.setIn(new FileInputStream("src/Stack/P3425/input.txt")); |
| 14 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 15 | + |
| 16 | + String cmd = br.readLine(); |
| 17 | + while (!cmd.equals("QUIT")) { |
| 18 | + ArrayList<String> commands = new ArrayList<>(); |
| 19 | + |
| 20 | + while(!cmd.equals("END")) { |
| 21 | + commands.add(cmd); |
| 22 | + cmd = br.readLine(); |
| 23 | + } |
| 24 | + |
| 25 | + int N = Integer.parseInt(br.readLine()); |
| 26 | + for (int i = 0; i < N; i++) { |
| 27 | + int num = Integer.parseInt(br.readLine()); |
| 28 | + pro = new Program(num, commands); |
| 29 | + } |
| 30 | + System.out.println(""); |
| 31 | + |
| 32 | + br.readLine(); |
| 33 | + cmd = br.readLine(); |
| 34 | + } |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +class Program { |
| 39 | + Stack<Integer> stack = new Stack<>(); |
| 40 | + final int MAX = 1000000000; |
| 41 | + boolean error = false; |
| 42 | + |
| 43 | + public Program(int num, ArrayList<String> commands) { |
| 44 | + this.stack.push(num); |
| 45 | + for (String cmd : commands) { |
| 46 | + if (this.error) break; |
| 47 | + |
| 48 | + if (cmd.matches(".*[0-9]")) { |
| 49 | + this.num(Integer.parseInt(cmd.substring(4))); |
| 50 | + } else { |
| 51 | + switch (cmd) { |
| 52 | + case "POP": |
| 53 | + this.pop(); |
| 54 | + break; |
| 55 | + case "INV": |
| 56 | + this.inv(); |
| 57 | + break; |
| 58 | + case "DUP": |
| 59 | + this.dup(); |
| 60 | + break; |
| 61 | + case "SWP": |
| 62 | + this.swp(); |
| 63 | + break; |
| 64 | + default: |
| 65 | + this.oper(cmd); |
| 66 | + break; |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + if (this.error || stack.size() != 1) System.out.println("ERROR"); |
| 72 | + else System.out.println(stack.pop()); |
| 73 | + } |
| 74 | + |
| 75 | + void num(int x) { |
| 76 | + this.stack.push(x); |
| 77 | + } |
| 78 | + |
| 79 | + void pop() { |
| 80 | + if (this.validSize(1)) this.stack.pop(); |
| 81 | + } |
| 82 | + |
| 83 | + void inv() { |
| 84 | + if (!this.validSize(1)) return; |
| 85 | + int target = this.stack.pop(); |
| 86 | + this.stack.push(target * (-1)); |
| 87 | + } |
| 88 | + |
| 89 | + void dup() { |
| 90 | + if (!this.validSize(1)) return; |
| 91 | + int target = this.stack.peek(); |
| 92 | + this.stack.push(target); |
| 93 | + } |
| 94 | + |
| 95 | + void swp() { |
| 96 | + if (!this.validSize(2)) return; |
| 97 | + int first = this.stack.pop(); |
| 98 | + int second = this.stack.pop(); |
| 99 | + this.stack.push(first); |
| 100 | + this.stack.push(second); |
| 101 | + } |
| 102 | + |
| 103 | + void oper(String o) { |
| 104 | + if (!this.validSize(2)) return; |
| 105 | + int first = this.stack.pop(); |
| 106 | + int second = this.stack.pop(); |
| 107 | + switch (o) { |
| 108 | + case "ADD": |
| 109 | + this.add(second, first); |
| 110 | + break; |
| 111 | + case "SUB": |
| 112 | + this.sub(second, first); |
| 113 | + break; |
| 114 | + case "MUL": |
| 115 | + this.mul(second, first); |
| 116 | + break; |
| 117 | + case "DIV": |
| 118 | + this.div(second, first); |
| 119 | + break; |
| 120 | + case "MOD": |
| 121 | + this.mod(second, first); |
| 122 | + break; |
| 123 | + default: |
| 124 | + break; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + void add(int x, int y) { |
| 129 | + long result = x + y; |
| 130 | + if (this.validNum(result)) this.stack.push((int) result); |
| 131 | + } |
| 132 | + |
| 133 | + void sub(int x, int y) { |
| 134 | + long result = x - y; |
| 135 | + if (this.validNum(result)) this.stack.push((int) result); |
| 136 | + } |
| 137 | + |
| 138 | + void mul(int x, int y) { |
| 139 | + if (x != 0 && y!= 0) { |
| 140 | + int x_len = (int) Math.log10(x) + 1; |
| 141 | + int y_len = (int) Math.log10(y) + 1; |
| 142 | + if (x_len + y_len > 10) { |
| 143 | + this.error = true; |
| 144 | + return; |
| 145 | + } |
| 146 | + } |
| 147 | + long result = x * y; |
| 148 | + if (this.validNum(result)) this.stack.push((int) result); |
| 149 | + } |
| 150 | + |
| 151 | + void div(int x, int y) { |
| 152 | + if (y == 0) { |
| 153 | + this.error = true; |
| 154 | + return; |
| 155 | + } |
| 156 | + long result = Math.abs(x) / Math.abs(y); |
| 157 | + if ((x < 0 && y > 0) || (x > 0 && y < 0)) result *= -1; |
| 158 | + if (this.validNum(result)) this.stack.push((int) result); |
| 159 | + } |
| 160 | + |
| 161 | + void mod(int x, int y) { |
| 162 | + if (y == 0) { |
| 163 | + this.error = true; |
| 164 | + return; |
| 165 | + } |
| 166 | + long result = Math.abs(x) % Math.abs(y); |
| 167 | + if (x < 0) result *= -1; |
| 168 | + if (this.validNum(result)) this.stack.push((int) result); |
| 169 | + } |
| 170 | + |
| 171 | + boolean validSize(int targetSize) { |
| 172 | + if (this.stack.size() < targetSize) { |
| 173 | + this.error = true; |
| 174 | + return false; |
| 175 | + } |
| 176 | + return true; |
| 177 | + } |
| 178 | + |
| 179 | + boolean validNum(long target) { |
| 180 | + if (Math.abs(target) > this.MAX) { |
| 181 | + this.error = true; |
| 182 | + return false; |
| 183 | + } |
| 184 | + return true; |
| 185 | + } |
| 186 | +} |
0 commit comments