3

Possible Duplicate:
Converting string expression to Integer Value using C#

I'm just wondering if there are any built-in functions in C# / .NET in order to parse and evaluate mathematical expressions at run-time.

Our users should be able to write expressions into a text file, like

bmi = weight / (height * height)

and the program should then be able to calculate these formulas

For our expressions, we need at least these capabilities:

  • Basic arithmetic operations + - * /
  • Brackets
  • Functions for calculating square roots and natural logarithms, like sqrt(), ln()
  • Use of pre-defined variables. Variables will be filled in by our program, and the user should then be able to calculate with them.

If there is no such thing in .NET, is there a library you could recommend?

Well, I would be able to write an expression parser myself - I know all the theory about it, this was even one of the greatest things at university :-) - but still, it just feels like re-inventing the wheel.

Community
  • 1
  • 1
  • 2
    I'd suggest the title be changed to "Library for evaluating..." As this isn't asking HOW to accomplish such. (+1 all the same.) – BlackVegetable Jul 20 '12 at 19:09

1 Answers1

3

The closest you can get within the BCL is the Expression class, though it will have no parsing capabilities.

There is a bunch of third party of mathematical expression parsers for .NET, so you don't need to reinvent the wheel.

Oded
  • 489,969
  • 99
  • 883
  • 1,009