9

Are there any .NET libraries out there that will parse a string as a formula?

ie; "if the user inputs "(2 +5) * 2", the library will be able to work out the calculation?

Edit:Preferably something that will work with Windows Phone 7 without too much fiddling would be nice!

MrBliz
  • 5,830
  • 15
  • 57
  • 81
  • possible duplicate of [Converting string expression to Integer Value using C#](http://stackoverflow.com/questions/2607798/converting-string-expression-to-integer-value-using-c) – Hans Passant Mar 12 '11 at 02:03
  • Changed the title to reflect the specifics of the actual question. NCalc will not work with WP7. Not without a lot of fiddling anyway. – MrBliz Mar 12 '11 at 12:20

3 Answers3

13

NCalc is a fairly nice library that will parse mathematical expressions in .NET code.

Edit - New location on Github.

RooiWillie
  • 2,198
  • 1
  • 30
  • 36
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
5

I posted source code for one that supports expressions, variables and functions.

You can see it at http://www.blackbeltcoder.com/Articles/algorithms/a-c-expression-evaluator.

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
0

Due to possible complexities, I would recommend starting with a web API - WolframAlpha recently opened theirs and it works great. As a side effect, your application will have to be constantly connected to an active network.

Den
  • 16,686
  • 4
  • 47
  • 87
  • And for my particular mobile phone application, that's an absolute no-no. Thank you for the suggestion anyway. – MrBliz Mar 12 '11 at 12:18