我在NSString中有公式...如何评估它 [英] I have formula in a NSString... How to evaluate it
问题描述
可能重复:
什么是快速C或Objective-C数学解析器?
我有一个创建一个函数,它将公式作为NSString。我必须在String中替换一些东西并在执行公式后获取值。我怎么能这样做...
I have a create a function which takes a formula as a NSString. And I have to replace few things in the String and get the value after executing the formula. How can I do this...
Ex:NSString * formulaString = @((A / B)-1)* 100;
Ex: NSString *formulaString = @"((A/B)-1)*100";
现在我可以将A替换为20,将B替换为10.
但是如何将计算值((20/10)-1)* 100替换为100
Now I can replace A by 20, and B by 10. But how will get calculated value that is ((20/10)-1)*100 as 100
谢谢,
Ben
Thanks, Ben
推荐答案
你可以用以下字符替换这个方法
You can replace the characters by this method
NSString *formulaString = @"((A/B)-1)*100";
NSString *modifiedFormulaString = [formulaString stringByReplacingOccurrencesOfString:@"A" withString:@"10"];
modifiedFormulaString = [modifiedFormulaString stringByReplacingOccurrencesOfString:@"B" withString:@"10"];
但要计算数据,你需要使用一些解析器来解析它。
But to calculate dat u need to parse it using some parser.
这篇关于我在NSString中有公式...如何评估它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!