arithmetic.as

来自「用于flash/flex的 as3的 2D图形图像图表的动态生成」· AS 代码 · 共 132 行

AS
132
字号
package flare.query
{
	/**
	 * Expression operator for arithmetic operations. Performs addition,
	 * subtraction, multiplication, or division of sub-expression values.
	 */
	public class Arithmetic extends BinaryExpression
	{
		/** Indicates an addition operation. */
	    public static const ADD:int = 0;
	    /** Indicates a subtraction operation. */
	    public static const SUB:int = 1;
	    /** Indicates a multiplication operation. */
	    public static const MUL:int = 2;
	    /** Indicates a division operation. */
	    public static const DIV:int = 3;
	    /** Indicates a modulo operation. */
	    public static const MOD:int = 4;
		
		/** Returns a string representation of the arithmetic operator. */
		public override function get operatorString():String
		{
        	switch (_op) {
	        	case ADD: return '+';
	        	case SUB: return '-';
	        	case MUL: return '*';
	        	case DIV: return '/';
	        	case MOD: return '%';
	        	default: return '?';
	        }
		}
		
		/**
	     * Create a new Arithmetic expression.
	     * @param operation the operation to perform
	     * @param left the left sub-expression
	     * @param right the right sub-expression
	     */
	    public function Arithmetic(op:int, left:*, right:*)
	    {
	        super(op, ADD, MOD, left, right);
	    }
		
		/**
		 * @inheritDoc
		 */
		public override function clone():Expression
		{
			return new Arithmetic(_op, _left.clone(), _right.clone());
		}
		
		/**
		 * @inheritDoc
		 */
		public override function eval(o:Object=null):*
		{
			var x:Number = Number(_left.eval(o));
			var y:Number = Number(_right.eval(o));
			
	        // compute return value
	        switch (_op) {
	        	case ADD: return x+y;
	        	case SUB: return x-y;
	        	case MUL: return x*y;
	        	case DIV: return x/y;
	        	case MOD: return x%y;
	        }
	        throw new Error("Unknown operation type: "+_op);
		}
		
		// -- Static constructors ---------------------------------------------
		
		/**
		 * Creates a new Arithmetic operator for adding two numbers.
		 * @param left the left-hand input expression
		 * @param right the right-hand input expression
		 * @return the new Arithmetic operator
		 */
		public static function Add(left:*, right:*):Arithmetic
		{
			return new Arithmetic(ADD, left, right);
		}
		
		/**
		 * Creates a new Arithmetic operator for subtracting one number
		 *  from another.
		 * @param left the left-hand input expression
		 * @param right the right-hand input expression
		 * @return the new Arithmetic operator
		 */
		public static function Subtract(left:*, right:*):Arithmetic
		{
			return new Arithmetic(SUB, left, right);
		}
		
		/**
		 * Creates a new Arithmetic operator for multiplying two numbers.
		 * @param left the left-hand input expression
		 * @param right the right-hand input expression
		 * @return the new Arithmetic operator
		 */
		public static function Multiply(left:*, right:*):Arithmetic
		{
			return new Arithmetic(MUL, left, right);
		}
		
		/**
		 * Creates a new Arithmetic operator for dividing one number
		 *  by another.
		 * @param left the left-hand input expression
		 * @param right the right-hand input expression
		 * @return the new Arithmetic operator
		 */
		public static function Divide(left:*, right:*):Arithmetic
		{
			return new Arithmetic(DIV, left, right);
		}
		
		/**
		 * Creates a new Arithmetic operator for computing the modulo
		 *  (remainder) of a number.
		 * @param left the left-hand input expression
		 * @param right the right-hand input expression
		 * @return the new Arithmetic operator
		 */
		public static function Mod(left:*, right:*):Arithmetic
		{
			return new Arithmetic(MOD, left, right);
		}
		
	} // end of class Arithmetic
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?