评估数学方程式MySQL和C#.net [英] Evaluate Mathematical equations MySQL and C#.net

查看:86
本文介绍了评估数学方程式MySQL和C#.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上。我是编程新手。我是否知道有没有办法评估我在mySQL数据库中存储的数学方程式及其在C#.NET中的变量?

例如,在我的数据库中,有一个等式,比方说x + b + X + d。存储的变量是x = 1,b = 2和d = 2。我该如何评估这个等式?我应该找到的任何线索。我在网上寻找教程,但仍然找不到它。我不知道正确的关键字。谢谢

Hai. I'm new in programming. May I know is there a way to evaluate mathematical equations that I had stored in mySQL database together with its variables in C#.NET?
For example, in my database, there is one equation let say x+b+x+d. The variables stored are x=1, b=2 and d=2. How am I going to evaluate this equation? Any clue that I should find. I'm looking for tutorial online but still cannot find it. I do not know the right keywords. Thanks

推荐答案

简单的问题是用程序中的值替换变量名称。你应该阅读正则表达式,它可能是最合适的工具。



在将值放在变量名之后,很难评估表达式是。为此,您有许多选择:



1.找到第三方数值表达式评估程序,您可以传递一个字符串作为要评估的表达式;编写程序来运行该程序,给它带有变量replace的表达式。



2.使用SQL进行评估。语句 SELECT 1 + 2 * 3 AS [Result] (即有效的Microsoft T-Sql;对于MySql可能存在细微差别)将为您评估您的答案。但是,您必须确保没有任何表达式使用SQL无法理解的运算符或函数,或者是SQL聚合运算符。



3.硬特别是因为你是编程新手:将数据库存储的表达式转换为.Net表达式树。本质上,这是从文本字符串动态创建.net程序。



4.您还可以查看Reflection(查找Emit),它可用于生成.Net类,然后您可以编译并链接到.Net类。可能比你需要的工作多得多,但它总是作为一种选择。



5.比3号和4号更简单,如果你有一个明确定义的集合运算符 - 实现自己的计算引擎。您必须动态解析表达式(或者,当缓存的预解析表达式被添加到数据库时保存它们)并构建引擎可以遍历的对象图,并计算结果。



这些选项都没有陷阱。如果你的表达式是用户键入的字符串,他们几乎可以保证有随机错误 - 不平衡的括号,不属于数学表达式的奇怪字符等等。无论你采用哪种方法,你都必须处理出现的错误来自这样的表达。



如果你赶时间,1号可能是你最好的选择。



如果找不到第1号的解决方案,那么第2号是最简单的(实际上,可能比第1号更容易)。



第3号是技术上最具挑战性的。如果你有时间和倾向,那将是非常有益的。



No.4我真的不推荐。它会产生很多可执行代码(DLL),这将会有很多东西需要学习,而且你会遇到这样的错误:当你拥有的唯一工具是锤子时,一切都是一个钉子。



第5号会很有趣,比第3号更容易,并且会教你一些关于数据结构和算法的有价值的东西(并且会有所帮助)如果你尝试第3号,你就会明白发生了什么。



玩得开心!
The easy problem is replacing variable names with the values from your program. You should read up on Regular Expressions which are perhaps the most appropriate tool for that purpose.

The hard part is evaluating the expression after you've put values where variable names were. For that, you have a number of options:

1. Find a third-party numerical expression evaluation program to which you can pass a string that is the expression to evaluate; write your program to run that program, giving it the expression with its variables replace.

2. Use SQL to do that evaluation. The statement SELECT 1+2*3 AS [Result] (that is valid Microsoft T-Sql; for MySql there may be minor differences) will evaluate your answer for you. But, you'll have to ensure that none of your expressions use operators or functions that SQL can't understand, or that are SQL aggregation operators.

3. Hard, especially since you're new to programming: translate your database-stored expressions into .Net expression trees. Essentially, this is dynamically creating a .net program from a text string.

4.You could also look into Reflection (look for Emit) which can be used to generate .Net classes which you can then compile and link to. Probably far more work than you need, but it's always there as an option.

5. Simpler than Nos. 3 and 4, if you have a well-defined set of operators - implement your own calculation engine. You'll have to dynamically parse the expressions (or, save cached pre-parsed expressions when they get added to the database) and build a graph of objects that your engine can traverse, calculating a result as it goes.

None of these options is without pitfalls. If your expressions are strings typed by a user they are almost guaranteed to have random errors - unbalanced parentheses, strange characters that don't belong in a mathematical expression, etc. Whichever approach you take, you'll have to handle the errors that arise from such expressions.

If you're in a hurry, No. 1 is probably your best option.

If you can't find a solution to No. 1, No. 2 is the next easiest (actually, maybe easier than No. 1).

No. 3 is the most technically challenging. If you have the time and inclination it will be very rewarding.

No. 4 I really don't recommend. It'll generate a lot of executable code (DLLs) lying around, there'll be a lot to learn, and there's a risk that you'll fall into the error of "when the only tool you have is a hammer, everything is a nail."

No. 5 would be fun, easier than No. 3, and will teach you some valuable things about data structures and algorithms (and, will help you understand what's going on if you try No. 3).

Have fun!


这篇关于评估数学方程式MySQL和C#.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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