我正在使用Objective-C,我正在尝试设置存储在 NSString 中的等式,以便进行评估并存储在 NSInteger中。
I am using Objective-C and I am trying to set an equation that is stored in an NSString to be evaluated and stored in an NSInteger.
类似于以下内容:
equation = [[NSString alloc] initWithString:@"1+5*6"];然后将其评估为31并将其存储到 NSInteger 。任何想法如何做到这一点?
and then evaluate that to become 31 and store it into an NSInteger. any ideas how to do this?
推荐答案你想要美妙,神奇,美妙的GCMathParser,(免费!) apptree: apptree/parser.htm 它完全符合您的要求,甚至允许你做变量替换(3x + 42,用x = 7评估)。它甚至支持数学函数,如sin(),cos(),tan(),它们的反转,dtor(),log(),....
You want the wonderful, amazing, and fabulous GCMathParser, available (FOR FREE!) on apptree: apptree/parser.htm It does exactly what you're asking, and even allows you to do variable substitutions (3x+42, evaluate with x = 7). It even has support for mathematical functions like sin(), cos(), tan(), their inverses, dtor(), log(), ....
很久以后编辑 ......
edit a long time later...
虽然GCMathParser非常棒,但它有一个不可扩展的缺陷。因此,如果您需要一个本身不支持的功能,那就太糟糕了。所以我决定对此做些什么,并想出了一个完全原生的数学解析器和评估器: http:// github。 com / davedelong / DDMathParser
While GCMathParser is pretty awesome, it has the flaw of not being extensible. So if you need a function that it doesn't natively support, then too bad. So I decided to do something about it, and came up with an entirely native math parser and evaluator: github/davedelong/DDMathParser
更多推荐
NSString到方程式
发布评论