将数学方程的 NSString 转换为值

发布于 2024-12-22 21:54:58 字数 193 浏览 6 评论 0原文

我想知道如何评估方程的字符串表示形式,就像它是一个真正的方程一样:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

悟红尘 2024-12-29 21:54:58

以下是如何使用 NSPredicate 执行此操作的示例:

NSPredicate *p = [NSPredicate predicateWithFormat:@"1+2==3"];
NSLog(@"%d", [p evaluateWithObject:nil]);
p = [NSPredicate predicateWithFormat:@"1+2==4"];
NSLog(@"%d", [p evaluateWithObject:nil]);

第一个 NSLog 生成 1 因为 1+2==3 > 是真的;第二个产生 0

Here is an example how to do it with NSPredicate:

NSPredicate *p = [NSPredicate predicateWithFormat:@"1+2==3"];
NSLog(@"%d", [p evaluateWithObject:nil]);
p = [NSPredicate predicateWithFormat:@"1+2==4"];
NSLog(@"%d", [p evaluateWithObject:nil]);

The first NSLog produces 1 because 1+2==3 is true; the second produces 0.

柳若烟 2024-12-29 21:54:58

所以,我认为这个问题比链接的问题要复杂得多(尽管问题是要求一个“简单”的方程解析器)。

对你来说幸运的是,我认为这是一个非常有趣的问题,并且已经为你写了一个:DDMathParser

它有大量文档,包括如何将其添加到您的项目对其功能的高度概述。它支持所有标准数学运算符,包括逻辑和比较运算符(| |&&==!=<= 等)。

在您的情况下,您需要执行以下操作:

NSNumber *result = [@"15+14 == 23" numberByEvaluatingString];
if ([result boolValue] == YES) {
  ....True statement....
} else {
  .....False statement.....
}

首先,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:

NSNumber *result = [@"15+14 == 23" numberByEvaluatingString];
if ([result boolValue] == YES) {
  ....True statement....
} else {
  .....False statement.....
}

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.

思念绕指尖 2024-12-29 21:54:58
NSString *equation = @"15+14==29";


NSPredicate *pred = [NSPredicate predicateWithFormat:equation];

NSExpression *LeftExp = [pred leftExpression];

NSExpression *RightExp = [pred rightExpression];    


NSNumber *left = [LeftExp expressionValueWithObject:nil context:nil];

NSNumber *right = [RightExp expressionValueWithObject:nil context:nil];


if ([left isEqualToNumber:right]) {
    NSLog(@"yes left is equal to right");
}
else{
    NSLog(@"yes left is NOT equal to right");

}

NSLog(@"left %@", left); 

NSLog(@"right %@", right); 
NSString *equation = @"15+14==29";


NSPredicate *pred = [NSPredicate predicateWithFormat:equation];

NSExpression *LeftExp = [pred leftExpression];

NSExpression *RightExp = [pred rightExpression];    


NSNumber *left = [LeftExp expressionValueWithObject:nil context:nil];

NSNumber *right = [RightExp expressionValueWithObject:nil context:nil];


if ([left isEqualToNumber:right]) {
    NSLog(@"yes left is equal to right");
}
else{
    NSLog(@"yes left is NOT equal to right");

}

NSLog(@"left %@", left); 

NSLog(@"right %@", right); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文