質問編集履歴
2
修正後のコード
test
CHANGED
File without changes
|
test
CHANGED
@@ -212,42 +212,178 @@
|
|
212
212
|
|
213
213
|
}
|
214
214
|
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
215
|
```
|
216
|
+
|
217
|
+
【追記】無事完成させられましたので、修正後貼っておきます!!
|
218
|
+
|
219
|
+
```Java
|
220
|
+
|
221
|
+
package stackmachine;
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
import java.util.Deque;
|
226
|
+
|
227
|
+
import java.util.ArrayDeque;
|
228
|
+
|
229
|
+
|
230
|
+
|
231
|
+
interface Operation {
|
232
|
+
|
233
|
+
void execute(Deque<Double> stack);
|
234
|
+
|
235
|
+
}
|
236
|
+
|
237
|
+
class Add implements Operation {
|
238
|
+
|
239
|
+
public void execute(Deque<Double> stack) {
|
240
|
+
|
241
|
+
double val2 = stack.pop();
|
242
|
+
|
243
|
+
double val1 = stack.pop();
|
244
|
+
|
245
|
+
stack.push(val1 + val2);
|
246
|
+
|
247
|
+
}
|
248
|
+
|
249
|
+
}
|
250
|
+
|
251
|
+
class Sub implements Operation {
|
252
|
+
|
253
|
+
public void execute(Deque<Double> stack) {
|
254
|
+
|
255
|
+
double val2 = stack.pop();
|
256
|
+
|
257
|
+
double val1 = stack.pop();
|
258
|
+
|
259
|
+
stack.push(val1 - val2);
|
260
|
+
|
261
|
+
}
|
262
|
+
|
263
|
+
}
|
264
|
+
|
265
|
+
class Mul implements Operation {
|
266
|
+
|
267
|
+
public void execute(Deque<Double> stack) {
|
268
|
+
|
269
|
+
double val2 = stack.pop();
|
270
|
+
|
271
|
+
double val1 = stack.pop();
|
272
|
+
|
273
|
+
stack.push(val1 * val2);
|
274
|
+
|
275
|
+
}
|
276
|
+
|
277
|
+
}
|
278
|
+
|
279
|
+
class Div implements Operation {
|
280
|
+
|
281
|
+
public void execute(Deque<Double> stack) {
|
282
|
+
|
283
|
+
double val2 = stack.pop();
|
284
|
+
|
285
|
+
double val1 = stack.pop();
|
286
|
+
|
287
|
+
stack.push(val1 / val2);
|
288
|
+
|
289
|
+
}
|
290
|
+
|
291
|
+
}
|
292
|
+
|
293
|
+
class Push implements Operation {
|
294
|
+
|
295
|
+
double val;
|
296
|
+
|
297
|
+
Push(double val) { this.val = val; }
|
298
|
+
|
299
|
+
public void execute(Deque<Double> stack) {
|
300
|
+
|
301
|
+
stack.push(this.val);
|
302
|
+
|
303
|
+
}
|
304
|
+
|
305
|
+
}
|
306
|
+
|
307
|
+
class Wrt implements Operation {
|
308
|
+
|
309
|
+
public void execute(Deque<Double> stack) {
|
310
|
+
|
311
|
+
System.out.println("Result : " + String.valueOf(stack.pop()));
|
312
|
+
|
313
|
+
}
|
314
|
+
|
315
|
+
}
|
316
|
+
|
317
|
+
class Halt implements Operation {
|
318
|
+
|
319
|
+
public void execute(Deque<Double> stack) {
|
320
|
+
|
321
|
+
while (!stack.isEmpty()) { stack.pop(); }
|
322
|
+
|
323
|
+
}
|
324
|
+
|
325
|
+
}
|
326
|
+
|
327
|
+
|
328
|
+
|
329
|
+
public class StackMachine {
|
330
|
+
|
331
|
+
private String[] strOpers;
|
332
|
+
|
333
|
+
private Deque<Operation> operations;
|
334
|
+
|
335
|
+
private Deque<Double> stack;
|
336
|
+
|
337
|
+
|
338
|
+
|
339
|
+
public StackMachine(String[] opers) {
|
340
|
+
|
341
|
+
this.strOpers = opers;
|
342
|
+
|
343
|
+
this.operations = new ArrayDeque<Operation>();
|
344
|
+
|
345
|
+
this.stack = new ArrayDeque<Double>();
|
346
|
+
|
347
|
+
}
|
348
|
+
|
349
|
+
|
350
|
+
|
351
|
+
public void generateOperationCode() {
|
352
|
+
|
353
|
+
for (String s : this.strOpers) {
|
354
|
+
|
355
|
+
if (s.matches("^-?(0|[1-9]\d*)(\.\d+|)$")) { this.operations.add( new Push(Double.parseDouble(s)) ); }
|
356
|
+
|
357
|
+
else if (s.equals("+")) { this.operations.add(new Add()); }
|
358
|
+
|
359
|
+
else if (s.equals("-")) { this.operations.add(new Sub()); }
|
360
|
+
|
361
|
+
else if (s.equals("*")) { this.operations.add(new Mul()); }
|
362
|
+
|
363
|
+
else if (s.equals("/")) { this.operations.add(new Div()); }
|
364
|
+
|
365
|
+
}
|
366
|
+
|
367
|
+
this.operations.add(new Wrt());
|
368
|
+
|
369
|
+
this.operations.add(new Halt());
|
370
|
+
|
371
|
+
}
|
372
|
+
|
373
|
+
|
374
|
+
|
375
|
+
public void doOperation() {
|
376
|
+
|
377
|
+
for (Operation o : this.operations) {
|
378
|
+
|
379
|
+
o.execute(this.stack);
|
380
|
+
|
381
|
+
}
|
382
|
+
|
383
|
+
}
|
384
|
+
|
385
|
+
}
|
386
|
+
|
387
|
+
```
|
388
|
+
|
389
|
+
ありがとうございました!
|
1
test
CHANGED
File without changes
|
test
CHANGED
@@ -22,16 +22,6 @@
|
|
22
22
|
|
23
23
|
|
24
24
|
|
25
|
-
java初心者でinterfaceの扱い方、Deque<>の使い方等よく分からず書いています。
|
26
|
-
|
27
|
-
特にstackを毎回returnしているところなど自分から見てもきもいです。
|
28
|
-
|
29
|
-
また、今回書きたいのは「interfaceを利用したスタックマシン計算機」なので、もっと良い構造(?)があればそれも教えて頂きたいです。
|
30
|
-
|
31
|
-
など色々書きましたが、どうかよろしくお願いします。
|
32
|
-
|
33
|
-
|
34
|
-
|
35
25
|
StackMachineが引数に受けるString[]はスペース区切りの後置記法の式をスペースでsplit()したものです。
|
36
26
|
|
37
27
|
|