c#从一个字符串中计算多个计算
c# calculate multiple calculations from a string
我正在用 c# (winforms) 制作一个计算器,我想知道是否有一种简单的方法来计算其中包含多个计算的字符串,例如:如果您有字符串 "124+241/2*5"
让程序计算它然后得到一个 int 输出。
提前致谢。
嗯,您基本上是在寻找与 Javascript 的 eval()
函数等效的 C#。
我推荐图书馆NCalc。
var result = new Expression("124+241/2*5").Evaluate()
另一个计算引擎是 Jace.NET。
Dictionary<string, double> variables = new Dictionary<string, double>();
variables.Add("var1", 2.5);
variables.Add("var2", 3.4);
CalculationEngine engine = new CalculationEngine();
double result = engine.Calculate("var1*var2", variables);
var result = new Interpreter().Eval("124+241/2*5");
并且你可以使用Roslyn's ScriptingEngine来评估:
Roslyn.Scripting.Session session = roslynEngine.CreateSession();
session.Execute("124+241/2*5");
没有自动的方法,您要么必须手动解析它,要么使用某种等同于 JavaScript 的 eval()
.
的功能
不需要使用外部库的一个不错的选择是 System.Data.DataTable.Compute
:
public double Evaluate(string expr)
{
System.Data.DataTable table = new System.Data.DataTable();
return Convert.ToDouble(table.Compute(expr, String.Empty));
}
CodeDom 和 Reflection 使您能够将 C# 代码动态构建为字符串,对其进行编译,然后 运行 这一切都在您的程序中。 .NET 中的这一非常强大的功能使我们能够创建 CodeDom 计算器,该计算器计算 Windows 表单内的表达式(甚至 C# 代码行)。
如果我没看错你需要这样的东西:Dynamic Expresso
您可以使用 NuGet
将其添加到您的程序中
好吧,"simple" 实际上是一个相当宽泛的术语……我应该说是相对的。无论如何,有一些库已经证明自己非常强大,并且在诸如此类的事情上非常受欢迎。
第一个是NCALC (http://ncalc.codeplex.com/):
// One of the examples.
Expression e = new Expression("2 + 3 * 5");
第二个是Jace.NET (https://github.com/pieterderycke/Jace):
// One of the examples.
CalculationEngine engine = new CalculationEngine();
Func<Dictionary<string, double>, double> formula = engine.Build("var1+2/(3*otherVariable)");
Dictionary<string, double> variables = new Dictionary<string, double>();
variables.Add("var1", 2);
variables.Add("otherVariable", 4.2);
double result = formula(variables);
两者都很棒,但根据以前的经验,我发现 Jace.NET 在更复杂的表达式上表现更好(在这里对您来说应该不是问题)。
我正在用 c# (winforms) 制作一个计算器,我想知道是否有一种简单的方法来计算其中包含多个计算的字符串,例如:如果您有字符串 "124+241/2*5"
让程序计算它然后得到一个 int 输出。
提前致谢。
嗯,您基本上是在寻找与 Javascript 的 eval()
函数等效的 C#。
我推荐图书馆NCalc。
var result = new Expression("124+241/2*5").Evaluate()
另一个计算引擎是 Jace.NET。
Dictionary<string, double> variables = new Dictionary<string, double>();
variables.Add("var1", 2.5);
variables.Add("var2", 3.4);
CalculationEngine engine = new CalculationEngine();
double result = engine.Calculate("var1*var2", variables);
var result = new Interpreter().Eval("124+241/2*5");
并且你可以使用Roslyn's ScriptingEngine来评估:
Roslyn.Scripting.Session session = roslynEngine.CreateSession();
session.Execute("124+241/2*5");
没有自动的方法,您要么必须手动解析它,要么使用某种等同于 JavaScript 的 eval()
.
不需要使用外部库的一个不错的选择是 System.Data.DataTable.Compute
:
public double Evaluate(string expr)
{
System.Data.DataTable table = new System.Data.DataTable();
return Convert.ToDouble(table.Compute(expr, String.Empty));
}
CodeDom 和 Reflection 使您能够将 C# 代码动态构建为字符串,对其进行编译,然后 运行 这一切都在您的程序中。 .NET 中的这一非常强大的功能使我们能够创建 CodeDom 计算器,该计算器计算 Windows 表单内的表达式(甚至 C# 代码行)。
如果我没看错你需要这样的东西:Dynamic Expresso 您可以使用 NuGet
将其添加到您的程序中好吧,"simple" 实际上是一个相当宽泛的术语……我应该说是相对的。无论如何,有一些库已经证明自己非常强大,并且在诸如此类的事情上非常受欢迎。
第一个是NCALC (http://ncalc.codeplex.com/):
// One of the examples.
Expression e = new Expression("2 + 3 * 5");
第二个是Jace.NET (https://github.com/pieterderycke/Jace):
// One of the examples.
CalculationEngine engine = new CalculationEngine();
Func<Dictionary<string, double>, double> formula = engine.Build("var1+2/(3*otherVariable)");
Dictionary<string, double> variables = new Dictionary<string, double>();
variables.Add("var1", 2);
variables.Add("otherVariable", 4.2);
double result = formula(variables);
两者都很棒,但根据以前的经验,我发现 Jace.NET 在更复杂的表达式上表现更好(在这里对您来说应该不是问题)。