-3

给定字符串:

"10 + 20 - 5 * 7 + (20 / 5)"

是否有任何可用的解析器可以对此进行评估,还是我需要自己编写?

4

1 回答 1

1

看看newacct的这篇文章。

NSString *equation = @"10 + 20 - 5 * 7 + (20 / 5)";

// dummy predicate that contains our expression
NSPredicate *pred = [NSPredicate predicateWithFormat:
                      [equation stringByAppendingString:@" == 42"]];//42 is just a rightExpression to make it an equation
NSExpression *exp = [pred leftExpression];
NSNumber *result = [(NSComparisonPredicate *)exp expressionValueWithObject:nil context:nil];
NSLog(@"%@", result); // logs "-1"
于 2012-09-10T13:29:46.617 回答