DDMathParser:解析包含if语句的公式 [英] DDMathParser: Parsing formulas which contain if statements
问题描述
我已经开始在当前项目中实现DDMathParser
,并使用此框架结合字典中给出的变量(变量替换)来计算公式的结果.
I have started to implement DDMathParser
in my current project and I use this framework to calculate the results of formulas in combination with variables given in a dictionary (variable substitution).
是否可以在公式中使用类似if
语句的内容,例如"if ($a > 0) { $b / $c } else {$b * 1000}
?
Would it be possible to use something like if
statements in the formulas, e.g. "if ($a > 0) { $b / $c } else {$b * 1000}
?
我认为if
语句的创建方式可以与新函数类似(如 DDMathParser Wiki ).如果是这样的话,如果有人可以向我展示如何做到这一点,我将感到非常高兴.
I assume that if
statements could be created in a similar way than new functions (as described in the DDMathParser Wiki). If this would be the case, I would be glad if someone could show me how to do this.
关于如何在DDMathParser
中使用if
语句的任何想法或提示?
Any ideas or hints on how to use if
statements in DDMathParser
?
谢谢!
推荐答案
DDMathParser
作者在这里...
这实际上是不可能的,但这不是您想的原因. DDMathParser
可以很好地解析if
.这是花括号,它将有问题.您也许可以这样做:
This isn't really possible, but not for the reason you think. DDMathParser
can parse if
just fine. It's the curly braces it would have issues with. You could maybe do:
if ($a > 0) ($b / $c) else ($b * 1000)
这将被解析为:
if($a > 0) * ($b / $c) * else($b *1000)
NSLogged
的哪个是:
multiply(multiply(if(l_gt($a,0)),divide($b,$c)),else(multiply($b,1000)))
因此,您可以执行此操作,但是尝试自己进行范围确定将是一件很痛苦的事情.我认为将字符串预分解成各个部分可能会更容易:
So, you could do it, but it would be a total pain to try and handle the scoping yourself. I think it'd probably be easier to pre-parse the string into various parts:
if ($a > 0) => if(l_get($a, 0))
($b / $c) => divide($b, $c)
($b * 1000) => multiply($b, 1000)
然后自己处理.
这篇关于DDMathParser:解析包含if语句的公式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!