4

Is there any method in C# in which you will pass a mathematical statement, and that method will give you the result for example you will pass this string

1+2-3/4*5

and it will give you in return

0

dash
  • 89,546
  • 4
  • 51
  • 71
  • 5
    I think you mean, it will give you in return [-0.75](http://www.wolframalpha.com/input/?i=1+%2B+2+-+3%2F4*5) ;-) – Kevin Oct 10 '13 at 14:10
  • 1
    You don't need third party libraries at all. try this ` var res = new DataTable().Compute("1+2-3/4*5", null);` – Sriram Sakthivel Oct 10 '13 at 14:14
  • I am immediately suspicious of a solution that requires spinning up a DataTable to do something that is essentially unrelated to Data operations. You will find the NCalc library very robust and better optimized for this kind of thing. – Mark Bailey Oct 10 '13 at 14:52
  • Do you really want it to return 0? In other words, you want it to specifically ignore correct order of operations and just perform operations in the order in which they appear? – Mark Bailey Oct 10 '13 at 14:54

1 Answers1

3

Use NCalc

Expression e = new Expression("2 + 3 * 5");   
Debug.Assert(17 == e.Evaluate());
Jaycee
  • 3,098
  • 22
  • 31