CSharp · Console
using System;
// see: https://stackoverflow.com/questions/13071823/convert-expression-from-a-textbox-to-math-expression-in-code-behind/13071974#13071974
public class Program
{
public static void Main()
{
Console.WriteLine(Eval("15 * (3 + 5) / (7 - 2)"));
}
public static string Eval(string expr)
{
var temp = new System.Data.DataTable();
string result = null;
try
{
result = $"{temp.Compute(expr, string.Empty)}";
}
catch (System.Data.EvaluateException ex)
{
if (ex.Message.ToLower().Contains("cannot find column"))
throw new System.Data.SyntaxErrorException($"Syntax error: Invalid expression: '{expr}'."
+ " Variables as operands are not supported.");
else
throw;
}
return result;
}
}