Jumat, 19 Februari 2010

MathFormula class

package
{
import MathParser;

public class MathFormula
{
protected var _f:String="";
protected var _p:Object;
protected var _v:Number;
protected var _e:String;

public function MathFormula()
{}

public function get formula():String { return _f; }
public function set formula(s:String):void
{
_f = s.replace(/[ \r\n\t]/g,"");
if (_p != null) evalMe();
}

public function get parameters():Object { return _p; }
public function set parameters(o:Object):void
{
_p = o;
if (_f != "") evalMe();
}

public function setParameters(s:String):Number
{
var ss:String = s.replace(/[ \r\n\t]/g,"");
_p = new Object();
_p.names = [];
_p.values = [];
var ar:Array = s.split(",");
var ai:Array;
var i:int = -1;
while(++i < ar.length)
{
ai=ar[i].split("=");
_p.names.push(ai[0].replace(/ /g, ""));
_p.values.push(Number(ai[1]));
}

if (_f != "") evalMe();
return _v;
}

public function get errorMessage():String { return _e; }

public function get result():Number { return _v; }

protected function evalMe():void
{
var rx:RegExp;
var s:String = "" + _f;
var i:int = -1;
while(++i < _p.names.length)
{
rx = new RegExp(_p.names[i], "g");
s = s.replace(rx,_p.values[i].toString());
}
_v = MathParser.instance.parseFormula(s);
}
}
}

Tidak ada komentar:

Posting Komentar