计算器应用程序执行问题
为了熟悉XCode和Objective-C,我目前正在尝试制作一个计算器应用程序,并有一个问题: 我现在尝试将方程写入 NSString,然后求解该方程。您认为完成这项工作最简单的方法是什么?
我希望它如何工作的示例代码:
float result;
...
calcstring = @" 10+4(6*8+2)^2 ";
result = calcstring;
=>这个想法是打印 NSString,因此运行标准计算方式,所以就像您刚刚输入的一样:
float result;
...
result = 10+4(6*8+2)^2;
否则我将不得不编写一个相当复杂的执行函数? 例如:
result = [calcstring execute];
-(float) execute: (NSString *) formula
{
//lots of code..
}
谢谢您的帮助。
To familiarize 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我理解正确,那么您要做的是将字符串评估为代码片段。虽然我不知道评估 Objective-C 代码的任何可能性,但您可以尝试通过以下方式将公式评估为 javascript 代码:
其中
webView
是一些 UIWebView 您创建的(例如可以隐藏)。这可能适用于所有简单的公式。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 :
where
webView
would be some UIWebView you create (could be hidden for example). That might work just as well for all simple formulas.