In a recent project that I was working on, there was a requirement of taking a basic mathematical formula as a string input from the user and process it at run time.
The initial analysis was to use regular expressions to parse out the operators and get write different cases for different operations.
But I ended up using a Third party open source library to solve the issue.
NCalc is such a library that allows you to parse mathematical expressions.
NCalc is a mathematical expressions evaluator in .NET. NCalc can parse any expression and evaluate the result, including static or dynamic parameters and custom functions.
NCalc is pretty fast when it comes to evaluating expressions.
Below is a simple code block that shows how to use the Expression parser to evaluate mathematical expressions.
public void Expression_Test()
{
Expression e = new Expression("2 + 3 * 5");
Debug.Assert(17 == e.Evaluate());
}