There are many algorithms to evaluate expressions, for example:
Is there any way to evaluate any mathematical expression using C# .net reflection or other modern .net technology?
To evaluate an algebraic expression, you have to substitute a number for each variable and perform the arithmetic operations. In the example above, the variable x is equal to 6 since 6 + 6 = 12. If we know the value of our variables, we can replace the variables with their values and then evaluate the expression.
An expression is a sequence of operands and operators that reduces to a single value. For example, the expression, 10+5 reduces to the value of 15.
The evaluation operator (=) EQUAL SIGN is used to evaluate a variable, function, or expression numerically. The Boolean equal to operator (=) CTRL+EQUAL SIGN is used to evaluate the equality condition in a Boolean statement. It is also used for programming, solving, and in symbolic equations.
Further to Thomas's answer, it's actually possible to access the (deprecated) JScript libraries directly from C#, which means you can use the equivalent of JScript's eval
function.
using Microsoft.JScript; // needs a reference to Microsoft.JScript.dll
using Microsoft.JScript.Vsa; // needs a reference to Microsoft.Vsa.dll
// ...
string expr = "7 + (5 * 4)";
Console.WriteLine(JScriptEval(expr)); // displays 27
// ...
public static double JScriptEval(string expr)
{
// error checking etc removed for brevity
return double.Parse(Eval.JScriptEvaluate(expr, _engine).ToString());
}
private static readonly VsaEngine _engine = VsaEngine.CreateEngine();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With