スタックマシンをDeque<>を用いて実現したいです。
各演算用のクラスにDeque<Double>を渡して、そこからDouble型をpop()しようとしたところで以下のエラーが出てしまいました。
src\stackmachine\StackMachine.java:12: エラー: 不適合な型: Objectをdoubleに変換できません:
double num2 = stack.pop();
^
13,20,21,28,29,36,37行でも同様のエラーが出ています。
doubleValue()を使ってもシンボルが見つかりませんと出てしまいます。
このエラーへの対処方法をご教授願いたいです。
StackMachineが引数に受けるString[]はスペース区切りの後置記法の式をスペースでsplit()したものです。
以下にコードを示します。
java
1package stackmachine; 2 3import java.util.Deque; 4import java.util.ArrayDeque; 5 6 7interface Operation { 8 Deque execute(Deque stack); 9} 10class Add implements Operation { 11 public Deque execute(Deque stack) { 12 double num2 = stack.pop(); 13 double num1 = stack.pop(); 14 stack.push(num1 + num2); 15 return stack; 16 } 17} 18class Sub implements Operation { 19 public Deque execute(Deque stack) { 20 double num2 = stack.pop(); 21 double num1 = stack.pop(); 22 stack.push(num1 - num2); 23 return stack; 24 } 25} 26class Mul implements Operation { 27 public Deque execute(Deque stack) { 28 double num2 = stack.pop(); 29 double num1 = stack.pop(); 30 stack.push(num1 * num2); 31 return stack; 32 } 33} 34class Div implements Operation { 35 public Deque execute(Deque stack) { 36 double num2 = stack.pop(); 37 double num1 = stack.pop(); 38 stack.push(num1 / num2); 39 return stack; 40 } 41} 42class Push implements Operation { 43 double val; 44 Push(double val) { this.val = val; } 45 public Deque execute(Deque stack) { 46 stack.push(this.val); 47 return stack; 48 } 49} 50class Wrt implements Operation { 51 public Deque execute(Deque stack) { 52 System.out.println("Result : "+String.valueOf(stack.pop())); 53 return stack; 54 } 55} 56class Halt implements Operation { 57 public Deque execute(Deque stack) { 58 while (!stack.isEmpty()) { stack.pop(); } 59 return stack; 60 } 61} 62 63public class StackMachine { 64 private String[] stringOpers; 65 private Deque<Operation> operations; 66 private Deque<Double> stack; 67 68 public StackMachine(String[] opers) { 69 this.stringOpers = opers; 70 this.operations = new ArrayDeque<Operation>(); 71 this.stack = new ArrayDeque<Double>(); 72 } 73 74 public void generateOperationCode() { 75 for (String s : this.stringOpers) { 76 if (s.matches("^-?(0|[1-9]\d*)(\.\d+|)$")) { this.operations.push( new Push(Double.parseDouble(s)) ); } 77 else if (s.equals("+")) { this.operations.push(new Add()); } 78 else if (s.equals("-")) { this.operations.push(new Sub()); } 79 else if (s.equals("*")) { this.operations.push(new Mul()); } 80 else if (s.equals("/")) { this.operations.push(new Div()); } 81 } 82 this.operations.push(new Wrt()); 83 this.operations.push(new Halt()); 84 } 85 86 public void doOperation() { 87 for (Operation ope : this.operations) { 88 this.stack = ope.execute(this.stack); 89 } 90 } 91}
【追記】無事完成させられましたので、修正後貼っておきます!!
Java
1package stackmachine; 2 3import java.util.Deque; 4import java.util.ArrayDeque; 5 6interface Operation { 7 void execute(Deque<Double> stack); 8} 9class Add implements Operation { 10 public void execute(Deque<Double> stack) { 11 double val2 = stack.pop(); 12 double val1 = stack.pop(); 13 stack.push(val1 + val2); 14 } 15} 16class Sub implements Operation { 17 public void execute(Deque<Double> stack) { 18 double val2 = stack.pop(); 19 double val1 = stack.pop(); 20 stack.push(val1 - val2); 21 } 22} 23class Mul implements Operation { 24 public void execute(Deque<Double> stack) { 25 double val2 = stack.pop(); 26 double val1 = stack.pop(); 27 stack.push(val1 * val2); 28 } 29} 30class Div implements Operation { 31 public void execute(Deque<Double> stack) { 32 double val2 = stack.pop(); 33 double val1 = stack.pop(); 34 stack.push(val1 / val2); 35 } 36} 37class Push implements Operation { 38 double val; 39 Push(double val) { this.val = val; } 40 public void execute(Deque<Double> stack) { 41 stack.push(this.val); 42 } 43} 44class Wrt implements Operation { 45 public void execute(Deque<Double> stack) { 46 System.out.println("Result : " + String.valueOf(stack.pop())); 47 } 48} 49class Halt implements Operation { 50 public void execute(Deque<Double> stack) { 51 while (!stack.isEmpty()) { stack.pop(); } 52 } 53} 54 55public class StackMachine { 56 private String[] strOpers; 57 private Deque<Operation> operations; 58 private Deque<Double> stack; 59 60 public StackMachine(String[] opers) { 61 this.strOpers = opers; 62 this.operations = new ArrayDeque<Operation>(); 63 this.stack = new ArrayDeque<Double>(); 64 } 65 66 public void generateOperationCode() { 67 for (String s : this.strOpers) { 68 if (s.matches("^-?(0|[1-9]\d*)(\.\d+|)$")) { this.operations.add( new Push(Double.parseDouble(s)) ); } 69 else if (s.equals("+")) { this.operations.add(new Add()); } 70 else if (s.equals("-")) { this.operations.add(new Sub()); } 71 else if (s.equals("*")) { this.operations.add(new Mul()); } 72 else if (s.equals("/")) { this.operations.add(new Div()); } 73 } 74 this.operations.add(new Wrt()); 75 this.operations.add(new Halt()); 76 } 77 78 public void doOperation() { 79 for (Operation o : this.operations) { 80 o.execute(this.stack); 81 } 82 } 83}
ありがとうございました!
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/12/08 01:45 編集