需要VB.net中的自动计算引擎 [英] Need a automated calculated engine in VB.net

查看:130
本文介绍了需要VB.net中的自动计算引擎的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先我有一个公式,即A = B + C - D.



我想动态设定目标并生成新公式。



例如,

如果我通过更改任何其他变量A,B或C将目标设置为D.

然后我应该得到公式为D = B + C - A.并且D的值应该重新计算。



我可以设置任何目标并且可以更改任何一个变量时间。



注意它必须是动态的,因为我有很多这样的公式要实现。





任何帮助对我来说都很棒。





谢谢

解决方案

这是计算机代数系统(CAS)的一个极为简化的例子:

http://en.wikipedia.org/wiki/Computer_algebra_system [ ^ ]。



传统方法基于解析一些符号语言。如果找到某些表达式,则需要解析表达式并输出语法错误。请参阅一些CodeProject文章:

Math Parser [ ^ ],

Math Parser .NET [ ^ ],

用muParser解析数学表达式 [ ^ ],

一个微小的分析器生成器v1.2 [ ^ ]。



你可以找到更多: http://www.codeproject.com/search.aspx?q=Parser+%22C%23%22&doctypeid=1 [ ^ ]。







那么,该怎么办?您应该将表达式解析为名为二进制表达式树的数据结构: http://en.wikipedia.org / wiki / Expression_tree [ ^ ]。



这样一个简单的事情,如替换数值和计算表达式的数值,只需遍历一棵树并在每个节点上执行计算,将结果传递给子节点。如果需要,可以在运行时使用 System.Reflection.Emit 将其编译为IL代码 - 这是非常高级的主题;掌握它需要良好的IL经验;并且调试将非常困难。



[结束编辑]



还有另一种方法:您可以将表达式包装在一些有效的C#或VB.NET代码中,并尝试使用CodeDOM将其编译为程序集。同样,它可以获得所有编译错误诊断。请查看我过去的答案:

使用CodeDom生成代码 [ ^ ],

创建使用可重新加载插件的WPF应用程序...... [ ^ ]。



-SA

To start with i have a formula i.e. A = B + C - D .

I want to dynamically set target and generate new formula.

For example,
If i set target as D by changing any other variable A,B,or C.
Then i should get the formula as D = B + C - A. and D's value should be recalculated.

I can set any target and can change any one variable at a time.

Note it has to be dynamic as i have many formulas like this to be implemented.


Any help will be great for me.


Thanks

解决方案

This is a greatly simplified example of Computer algebra system (CAS):
http://en.wikipedia.org/wiki/Computer_algebra_system[^].

Traditional approach to it is based on parsing some symbolic language. You need to parse the expression and output the syntax errors if some are found. Please see some CodeProject articles:
Math Parser[^],
Math Parser .NET[^],
Parsing Mathematical Expressions with muParser[^],
a Tiny Parser Generator v1.2[^].

You can find some more: http://www.codeproject.com/search.aspx?q=Parser+%22C%23%22&doctypeid=1[^].

[EDIT]

So, what to do with this? You should parse the expression into the data structure called binary expression tree: http://en.wikipedia.org/wiki/Expression_tree[^].

Such a simple thing as substitution of a numeric value and calculation of the numeric value of the expression would be just traversing a tree and performing the calculation on each node, passing the result to child nodes. If you want, you can compiler it into IL code during runtime, using System.Reflection.Emit — this is pretty advanced topic; mastering it would require good IL experience; and debugging would be pretty difficult.

[END EDIT]

There is another approach to this: you can wrap your expression in some valid C# or VB.NET code and try to compile it as an assembly using CodeDOM. Likewise, it can get all the compilation error diagnostics. Please see my past answers:
code generating using CodeDom[^],
Create WPF Application that uses Reloadable Plugins...[^].

—SA


这篇关于需要VB.net中的自动计算引擎的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆