[转]FLASH 四则运算


package ghostcat.algorithm
{
/**
* 四则计算
* @author flashyiyi
*
*/
public class Arithmetic
{
private static function isStart(exp:String,i:int):Boolean
{
return i <= 0 || exp.charAt(i - 1) == "(";
}
private static function isOperator(exp:String,i:int):Boolean
{
if (i >= exp.length || i < 0)
return false;

if (isStart(exp,i))//首字母不是运算符
return false;

var op:String = exp.charAt(i);
return op == '+' || (op == "-" && !isOperator(exp,i - 1)) || op == "*" || op == "/";
}

private static function isNumber(exp:String,i:int):Boolean
{
if (i >= exp.length || i < 0)
return false;

var op:String = exp.charAt(i);
return op >= '0' && op <= "9" || op == "." || op == "-" && (isOperator(exp,i - 1) || isStart(exp,i));
}

private static function getPriority(op:String):int
{
switch(op)
{
case ')':
return 3;
case '*':
case '/':
return 2;
case '+':
case '-':
return 1;
case '(':
return -1;
default:
return 0;
}
}

private static function getValue(op:String, operand1:Number, operand2:Number):Number
{
switch (op)
{
case '*':
return(operand2 * operand1);
case '/':
return(operand2 / operand1);
case '-':
return(operand2 - operand1);
case '+':
return(operand2 + operand1);
}
return NaN;
}


public static function exec(exp:String):Number
{
var operator:Array = [];
var operand:Array = [];

var op:String;
var operand1:Number;
var operand2:Number;
var pos:int=0;

while (pos < exp.length)
{
var ch:String = exp.charAt(pos);
if (ch ==' ')
{
pos++;
}
if (ch =='(')
{
operator.push(ch);
pos++;
}
else if(ch ==')')
{
op = operator.pop();
while (op != '(')
{
operand1 = operand.pop();
operand2 = operand.pop();
operand.push(getValue(op,operand1,operand2));
op = operator.pop();
}
pos++;
}
else if (isOperator(exp,pos))
{
while(getPriority(ch) <= getPriority(operator[operator.length - 1]) && operator.length)
{
op = operator.pop();
operand1=operand.pop();
operand2=operand.pop();
operand.push(getValue(op,operand1,operand2));
}
operator.push(exp.charAt(pos));
pos++;
}
else
{
var v:String = "";
while (isNumber(exp,pos) && pos < exp.length)
{
v = v + exp.charAt(pos);
pos++;
}
operand.push(Number(v));
}

}

while (operator.length)
{
op = operator.pop();
operand1=operand.pop();
operand2=operand.pop();
operand.push(getValue(op,operand1,operand2));
}
return operand.pop();
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值