简体   繁体   English

阅读数学 function f(x,y) 命令行参数的最佳方法是什么?

[英]What's the best way to read a mathematical function f(x,y) command line argument?

From main() , I want the user to input a mathematical function (I,e: 2xy) through the command line.main() ,我希望用户通过命令行输入数学 function (I,e: 2xy)。 From there, I initially thought to iterate through the string and parse out different arithmetic operators, x, y, etc. However, this could become fairly complicated for more intricate functions, (eg: (2x^2)/5 +sqrt(x^4) ).从那里开始,我最初考虑遍历字符串并解析出不同的算术运算符 x、y 等。但是,对于更复杂的函数,这可能会变得相当复杂(例如: (2x^2)/5 +sqrt(x ^4) )。 Is there a more general method to be able to parse a mathematical function string like this one?有没有更通用的方法能够解析像这样的数学 function 字符串?

One of the most helpful ways to deal with parsing issues like that is to switch the input methods from equations like that to an RPN based input where the arguments come first and the operators come last.处理此类解析问题的最有用的方法之一是将输入方法从这样的方程式切换到基于 RPN 的输入,其中 arguments 排在第一位,运算符排在最后。

Rewriting your complex equation would end up looking like:重写你的复杂方程最终看起来像:

2 2 x ^ * 5 / x 4 ^ sqrt +

This is generally easier to implement, as you can do it with a simple stack -- pushing new arguments on, while the operators pull the require pieces off the stack and put the result back on.这通常更容易实现,因为您可以使用简单的堆栈来实现 - 将新的 arguments 推入,而操作员将需要的部分从堆栈中拉出并将结果放回。 Greatly simplifies the parsing, but you still need to implement the functions.大大简化了解析,但你仍然需要实现这些功能。

What you need is an expression evaluator .你需要的是一个表达式求值器

A while ago, I wrote a complete C expression evaluator (ie evaluated expressions written using C syntax) for a command line processor and scripting language on an embedded system.不久前,我为嵌入式系统上的命令行处理器和脚本语言编写了一个完整的 C 表达式求值器(即使用 C 语法编写的求值表达式)。 I used this description of the algorithm as a starting point.我使用 这个算法的描述作为起点。 You could use the accompanying code directly, but I did not like the implementation, and wrote my own from the algorithm description.您可以直接使用随附的代码,但我不喜欢该实现,并根据算法描述编写了自己的代码。

It needed some work to support all C operators, function calls, and variables, but is a clear explanation and therefore a good starting point, especially if you don't need that level of completeness.它需要一些工作来支持所有 C 运算符、function 调用和变量,但这是一个清晰的解释,因此是一个很好的起点,特别是如果您不需要那种级别的完整性。

The basic principle is that expression evaluation is easier for a computer using a stack and 'Reverse Polish Notation', so the algorithm converts an in-fix notation expression with associated order of precedence and parentheses to RPN, and then evaluates it by popping operands, performing operations, and pushing results, until there are no operations left and one value left on the stack.基本原理是使用堆栈和“反向波兰表示法”的计算机更容易表达式评估,因此该算法将具有相关优先顺序和括号的固定表示法表达式转换为 RPN,然后通过弹出操作数对其进行评估,执行操作,并压入结果,直到堆栈上没有任何操作和一个值。

It might get a bit more complicated is you choose to deal with implicit multiply operators ( 2xy rather then 2 * x * y for example. Not least because you'd need to unambiguously distinguish the variables x and y from a single variable xy . That is probably only feasible if you only allow single character variable names. I suggest you either do that and insert explicit multiply operators on the operator stack as part of the parse, or you disallow implicit multiply.如果您选择处理隐式乘法运算符(例如2xy而不是2 * x * y ,则可能会变得更复杂一些。尤其是因为您需要明确地区分变量xy与单个变量xy 。那只有当你只允许单个字符变量名时才可能是可行的。我建议你要么这样做,并在运算符堆栈上插入显式乘法运算符作为解析的一部分,或者你不允许隐式乘法。

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

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