开发者

calculator app execution question

开发者 https://www.devze.com 2023-02-19 07:39 出处:网络
To fam开发者_开发问答iliarize myself with XCode and Objective-C, I am currently trying to make a calculator app, and have a question:

To fam开发者_开发问答iliarize myself with XCode and Objective-C, I am currently trying to make a calculator app, and have a question: I am now trying to write the equation into a NSString, and then solve this equation. What do you think is the easiest way to make this work?

example code of how I would like it to work:

float result;
...
calcstring = @" 10+4(6*8+2)^2 ";
result = calcstring;

=> the idea being to print the NSString, so the standard way of calculating is run, so it would be like you just typed in:

float result;
...
result = 10+4(6*8+2)^2;

otherwise I would have to write a reasonably complex execution function? e.g.:

result = [calcstring execute];

-(float) execute: (NSString *) formula
{
    //lots of code..
}

thank You for your help.


If I get you correctly, what you're trying to do is evaluate string as a code snippet. While I'm not aware of any possibility to evaluate objective-c code, you might try to evaluate the formula as javascript code in the following way :

NSString *result= [webView stringByEvaluatingJavaScriptFromString:formula];

where webView would be some UIWebView you create (could be hidden for example). That might work just as well for all simple formulas.

0

精彩评论

暂无评论...
验证码 换一张
取 消