ToolGood.Algorithm是一款计算引擎,目前支持四则运算、Excel绝大多数函数,并支持自定义参数。
快速上手 AlgorithmEngine engine = new AlgorithmEngine(); double a=0.0; if (engine.Parse("1+2")) { a = (double)engine.Evaluate(); } var c = engine.TryEvaluate("2+3", 0); var d = engine.TryEvaluate("count({1,2,3,4})", 0);//{}代表数组,返回:4 var s = engine.TryEvaluate("'aa'&'bb'", ""); //字符串连接,返回:aabb var r = engine.TryEvaluate("(1=1)*9+2", 0); //返回:11 var d = engine.TryEvaluate("'2016-1-1'+1", DateTime.MinValue); //返回日期:2016-1-2 var t = engine.TryEvaluate("'2016-1-1'+9*'1:0'", DateTime.MinValue);//返回日期:2016-1-1 9:0自定义参数//定义圆柱信息 public class Cylinder : AlgorithmEngine { private int _radius; private int _height; public Cylinder(int radius, int height) { _radius = radius; _height = height; } protected override Operand GetParameter(Operand curOpd) { if (curOpd.Parameter == "[半径]") { return new Operand(OperandType.NUMBER, _radius); } if (curOpd.Parameter == "[直径]") { return new Operand(OperandType.NUMBER, _radius * 2); } if (curOpd.Parameter == "[高]") { return new Operand(OperandType.NUMBER, _height); } return base.GetParameter(curOpd); } } //调用方法 Cylinder c = new Cylinder(3, 10); c.TryEvaluate("[半径]*[半径]*pi()", 0.0); //圆底面积 c.TryEvaluate("[直径]*pi()", 0.0); //圆的长 c.TryEvaluate("[半径]*[半径]*pi()*[高]", 0.0); //圆的体积Excel函数函数:逻辑函数、数学与三角函数、文本函数、统计函数、日期与时间函数
注:函数名不分大小写,带方括号的参数可省略,示例的返回值,可能为近似值。
评论