加算、減算、乗算、除算演算子をどどんと。これで四則演算ができますね。(実際はそれぞれファイル分ける)
加算、減算、乗算、除算演算子をどどんと。これで四則演算ができますね。(実際はそれぞれファイル分ける)
/** * 加法演算子です。 */ public class Add implements Node { private Node left; private Node right; public Add(Node left, Node right) { this.left = left; this.right = right; } @Override public Node evaluation() { left = left.evaluation(); right = right.evaluation(); double result = ((Numeric)left).getValue() + ((Numeric)right).getValue(); return new Numeric(result); } } /** * 減算演算子です。 */ public class Sub implements Node { private Node left; private Node right; public Sub(Node left, Node right) { this.left = left; this.right = right; } @Override public Node evaluation() { left = left.evaluation(); right = right.evaluation(); double result = ((Numeric)left).getValue() - ((Numeric)right).getValue(); return new Numeric(result); } } /** * 乗算演算子です。 */ public class Multi implements Node { private Node left; private Node right; public Multi(Node left, Node right) { this.left = left; this.right = right; } @Override public Node evaluation() { left = left.evaluation(); right = right.evaluation(); double result = ((Numeric)left).getValue() * ((Numeric)right).getValue(); return new Numeric(result); } } /** * 除算演算子です。 */ public class Div implements Node { private Node left; private Node right; public Div(Node left, Node right) { this.left = left; this.right = right; } @Override public Node evaluation() { left = left.evaluation(); right = right.evaluation(); double result = ((Numeric)left).getValue() / ((Numeric)right).getValue(); return new Numeric(result); } }