需要一种方法来验证包含未知变量的数学表达式 [英] Need a method to Validate Mathematical expression which contains unknown variables
问题描述
我正在编写一个包含文本框和保存"按钮的工具.
用户将在文本框中输入数学表达式,例如"2 *(UpperBound-LowerBound)",然后单击保存"按钮以将表达式保存为xml.
但是在将表达式保存到xml中之前,需要对其进行验证.
为此,我尝试创建一个类似ValidateExpression(string expresion,List< string> unknownvariables)的方法,在此列表中包含字符串"UpperBound","LowerBound" ant等.
有没有这样的方法来验证包含未知变量的表达式.
I am writing a tool which contains a textbox and Save button.
In the textbox user will enter mathematical expression like "2*(UpperBound - LowerBound)" and then clicks the save button to save the expression in xml.
But before saving the expression into xml want to validate it.
For this i tried to create a method like ValidateExpression(string expresion,List<string> unknownvariables) here the list contains the strings "UpperBound ","LowerBound" ant etc.
Is there any method like this to validate the expression containing unknown variables.
推荐答案
当然,有一种方法:解析数学表达式.您可能会发现很多示例,例如谷歌搜索 [ ^ ].
(可选)在解析之后,您可以使用未知"列表来验证所有找到的标识符.
Of course there is a method: parsing the mathematical expressions. You may find a lot of examples, just Googling[^].
Optionally, after parsing, you may validate all the found identifiers using the list of ''unknowns''.
这篇关于需要一种方法来验证包含未知变量的数学表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!