算術操作符包括 +、-、*、/、%
算術操作符
檔案:OperatorMathematic.js
function OperatorMathematic() {<br /> this.DeriveFrom(new OperatorBinaryBase());<br /> this.set_ClassName("OperatorMathematic");<br /> this.set_Sign("undefined");<br />};</p><p>function OperatorAdd() {<br /> this.DeriveFrom(new OperatorMathematic());<br /> this.set_ClassName("OperatorAdd");<br /> this.set_Sign("+");<br />};</p><p>OperatorAdd.prototype.Evaluate = function(operands) {<br /> var values = this._getTwoNumberOperands(operands);</p><p> return new OperandNumber(values[0].get_Value() + values[1].get_Value());<br />};</p><p>function OperatorSubtract() {<br /> this.DeriveFrom(new OperatorMathematic());<br /> this.set_ClassName("OperatorSubtract");<br /> this.set_Sign("-");<br />};</p><p>OperatorSubtract.prototype.Evaluate = function(operands) {<br /> var values = this._getTwoNumberOperands(operands);</p><p> return new OperandNumber(values[0].get_Value() - values[1].get_Value());<br />};</p><p>function OperatorMultiply() {<br /> this.DeriveFrom(new OperatorMathematic());<br /> this.set_ClassName("OperatorMultiply");<br /> this.set_Sign("*");<br />};</p><p>OperatorMultiply.prototype.Evaluate = function(operands) {<br /> var values = this._getTwoNumberOperands(operands);</p><p> return new OperandNumber(values[0].get_Value() * values[1].get_Value());<br />};</p><p>function OperatorDivide() {<br /> this.DeriveFrom(new OperatorMathematic());<br /> this.set_ClassName("OperatorDivide");<br /> this.set_Sign("/");<br />};</p><p>OperatorDivide.prototype.Evaluate = function(operands) {<br /> var values = this._getTwoNumberOperands(operands);</p><p> return new OperandNumber(values[0].get_Value() / values[1].get_Value());<br />};</p><p>function OperatorModule() {<br /> this.DeriveFrom(new OperatorMathematic());<br /> this.set_ClassName("OperatorModule");<br /> this.set_Sign("%");<br />};</p><p>OperatorModule.prototype.Evaluate = function(operands) {<br /> var values = this._getTwoNumberOperands(operands);</p><p> if (values[1].get_Value() == 0)<br /> throw new Exception(this, "Evaluate", "Divide by zero");</p><p> return new OperandNumber(values[0].get_Value() % values[1].get_Value());<br />};