c# - Mathematical expressions parser -


possible duplicate:
is there string math evaluator in .net?

i need implement mathematical expression parser. has evaluate following expressions:

  • basic operators(+,-,*,/,%[modulo operator])
  • priority operations such 2*(4-1)
  • sin,cos,tan,cotan, log(base,number) functions
  • allows compositions such sin(cos(log(2,32)))
  • power operation such 2^5

first wanted use jscript eval function. it's not unsafe , no power operator. secondary searched libraries in internet. turned out, cannot use them. question following: how safe jscript eval function calling , there standard .net tools task? if not, algorithm best? of i'm trying build expression tree , compile result.

you can try ncalc

dictionary<string, int> dict = new dictionary<string, int>() {     {"a",1} ,     {"b",4} };  ncalc.expression exp = new ncalc.expression("a + b / 2 * myfunc(3.14)");  exp.evaluatefunction += (s, e) =>     {         var d = (double)e.parameters[0].evaluate();         e.result = math.cos(d);     };  exp.evaluateparameter += (s, e) =>     {         e.result = dict[s];     };  var result = exp.evaluate(); 

Comments