将数学方程的 NSString 转换为值
我想知道如何评估方程的字符串表示形式,就像它是一个真正的方程一样:
if(@"15+14==23")
{
//True statement...
}
else
{
//False statement....
}
我想返回“false”,因为 15+14 不等于 23。我怎样才能让它工作?
I would like to know how to evaluate a string representation of an equation as if it were a real equation:
if(@"15+14==23")
{
//True statement...
}
else
{
//False statement....
}
I want to return "false" because 15+14 does not equal 23. How can I get this to work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
以下是如何使用
NSPredicate
执行此操作的示例:第一个
NSLog
生成1
因为1+2==3
> 是真的;第二个产生0
。Here is an example how to do it with
NSPredicate
:The first
NSLog
produces1
because1+2==3
is true; the second produces0
.所以,我认为这个问题比链接的问题要复杂得多(尽管问题是要求一个“简单”的方程解析器)。
对你来说幸运的是,我认为这是一个非常有趣的问题,并且已经为你写了一个:
DDMathParser
。它有大量文档,包括如何将其添加到您的项目 和 对其功能的高度概述。它支持所有标准数学运算符,包括逻辑和比较运算符(
| |
、&&
、==
、!=
、<=
等)。在您的情况下,您需要执行以下操作:
首先,
DDMathParser
是根据 MIT 许可证提供的,该许可证要求您在任何内容中包含版权信息和许可证的全文使用它的。So, this is a problem that I believe is a lot more complicated than the linked question lets on (although the question is asking for a "simple" equation parser).
Fortunately for you, I think this is a really interesting problem and have already written one for you:
DDMathParser
.It has a good amount of documentation, including things like how to add it to your project and a high overview of its capabilities. It supports all of the standard mathematical operators, including logical and comparison operators (
||
,&&
,==
,!=
,<=
, etc).In your case, you'd do something like this:
As a heads up,
DDMathParser
is made available under the MIT license, which requires you to include the copyright information and the full text of the license in anything that uses it.