简体   繁体   English

C#解决最简单的方程式

[英]C# solve simplest equations

In C#, I am looking for a way to solve simple equations like this. 在C#中,我正在寻找一种解决此类简单方程的方法。 Z = A + B

I am trying to build a class that would give me the 3rd parameter if I give any of the 2 others. 我正在尝试构建一个类,如果我给出其他2个参数中的任何一个,它将给我第3个参数。

Example, given Z=A+B 示例,给定Z=A+B

If you know A=3 and B=6 then you know Z=9 如果您知道A=3B=6那么您知道Z=9

If you know A=4 and Z=8 then you know B=4 如果您知道A=4Z=8那么您知道B=4

How would I best perform these kinds of tasks in software? 我如何最好地在软件中执行这些任务?

The other idea is to use math expressions evaluates, like ncalc. 另一个想法是使用数学表达式求值,例如ncalc。 They can interpret math expressions, for example convert 3*(8+2) into 30 , but not solve equations like 3*(8+x)=30 --> x=2 . 他们可以解释数学表达式,例如将3*(8+2)转换为30 ,但不能求解3*(8+x)=30 > x=2类的方程。

Are you sure NCalc wouldn't do what you need? 您确定NCalc不会满足您的需求吗? Take a look at an example from http://ncalc.codeplex.com/ . 看一下http://ncalc.codeplex.com/中的示例。

Define parameters, even dynamic or expressions 定义参数,甚至动态或表达式

Expression e = new Expression("Round(Pow([Pi], 2) + Pow([Pi2], 2) + [X], 2)");

e.Parameters["Pi2"] = new Expression("Pi * [Pi]");
e.Parameters["X"] = 10;

e.EvaluateParameter += delegate(string name, ParameterArgs args)
  {
    if (name == "Pi")
    args.Result = 3.14;
  };

Debug.Assert(117.07 == e.Evaluate());

Please note this is untested - but it looks like you could do something like this with NCalc: 请注意,这未经测试-但看起来您可以使用NCalc执行以下操作:

var e = new Expression("[A] + [B]"); 
e.Parameters = /* your input */ 
var result = e.Evaluate(); 

试用C#Expression Evaluator ,看看它是否符合您的要求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM