1.传入一个带四则运算的字符串。(如:var a:String = "2*(4-3.001)+3.5";).
2.返回值为被解析后四则运算出来的结果值。
package com.zzz
{
public class MathExpress
{
public function MathExpress()
{
}
public static function MathExpressByStr(str:String):String{
str = "(" + str.replace(/[^\d.+-*\/()]+/g,"") + ")";
while (/\([^()]*\)/.test(str)){
str = str.replace(/\(([^()]*)\)/g,function(s, b){
while (/[*\/]/.test(b)){
b = b.replace(/([\d.]+)([*\/])([\d.]+)/g,function(c, m, l, n){
var i:Number = parseFloat(m);
var j:Number = parseFloat(n);
return l=="*" ? i*j : i/j;
})
}
while (/[+-]/.test(b)){
b = b.replace(/([\d.]+)([+-])([\d.]+)/g,function(c, m, l, n){
var i:Number = parseFloat(m);
var j:Number = parseFloat(n);
return l=="+" ? i+j : i-j;
})
}
return b;
})
}
return str;
}
}
}
3.得最后结果:7.498