扩展数学表达式(扩展括号) [英] Expanding math expression (expanding parentheses)
问题描述
我有一个像这样的表达式a*(b+c)
,并且我已经成功解析为AST,所以它最终变成了:
I have a expression like this a*(b+c)
and I have successfully parsed into an AST so it finally becomes:
我正在尝试扩展它最终变成的表达式
a*b + a*c
,但没有运气.
I'm trying to expand the expression it finally becomes
a*b + a*c
, but with no luck.
我想知道一种扩展表达式的算法,或者也许是一个库来实现,最好是.NET.
I would like to know an algorithm to expand the expression, or maybe a library to do it, preferably for .NET.
推荐答案
这是prolog中的单行程序. 作为奖励,它可以双向工作. 也就是说,您将其设计为展开",即可免费获得展开".这是一个使用yap prolog的交互式REPL的示例.都是大写字母的标识符是变量.
This is a one-line program in prolog . And as a bonus , it works both ways . i.e. you design it to "expand" , you get "unexpand" for free . Here is an example that uses the interactive REPL of yap prolog . The identifiers that are all capital letters are the variables .
$ yap
YAP 6.2.2 (x86_64-linux): Sat Sep 17 13:59:03 UTC 2016
?- [user].
/* consulting user_input... */
rewrite(A * (B + C), (A * B + A * C)) .
end_of_file .
/* example usage from the REPL */
?- rewrite(3 * (4 + 5), REWRITTEN) .
REWRITTEN = 3*4+3*5
?- rewrite(a * (b + c), REWRITTEN) .
REWRITTEN = a*b+a*c
/* example usage showing it work the opposite way */
?- rewrite(REWRITABLE,(3*4+3*5)) .
REWRITABLE = 3*(4+5)
这篇关于扩展数学表达式(扩展括号)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!