如何在iOS中解决条件方程

时间:2013-05-16 10:05:03

标签: iphone ios xcode macos

我想在iOS中解决一个条件方程式:

我从数据库得到的等式是NSString格式,例如:

if((height > 0), (weight+ 2 ), ( weight-1 ) )

根据我们的理解,如果我解析上面的字符串并单独height>0条件,它将采用NSString格式。但要评估它如何将字符串转换为条件语句?

获得条件陈述后,可以通过将等式转换为三元方程来解决等式:

Bool status;

NSString *condition=@” height>0”;

If(condition)    //But condition is treated as a string and not as a conditional statement.
{
    status=True;
}
else
{
    status=False;
}

Return status ? weight+ 2 : weight-1;`

此外,方程式可以动态更改,因此无法进行硬编码。简而言之,我如何解决这个等式NSString

感谢您的耐心等待!

2 个答案:

答案 0 :(得分:5)

DDMathParser作者在这里......

要展开Jonathan's answer,您可以在DDMathParser中完全执行此操作。但是,要按原样解析字符串,您需要做两件事。

首先,您需要创建一个if函数:

DDMathEvaluator *evaluator = [DDMathEvaluator sharedMathEvaluator];
[evaluator registerFunction:^DDExpression *(NSArray *args, NSDictionary *vars, DDMathEvaluator *eval, NSError *__autoreleasing *error) {
    if ([args count] == 3) {
        DDExpression *condition = [args objectAtIndex:0];
        DDExpression *resultExpression = nil;
        NSNumber *conditionValue = [condition evaluateWithSubstitutions:vars evaluator:eval error:error];
        if ([conditionValue boolValue] == YES) {
            resultExpression = [args objectAtIndex:1];
        } else {
            resultExpression = [args objectAtIndex:2];
        }
        NSNumber *result = [resultExpression evaluateWithSubstitutions:vars evaluator:eval error:error];
        return [DDExpression numberExpressionWithNumber:result];
    }
    return nil;

} forName:@"if"];

这将创建if()函数,该函数有三个参数。根据第一个参数的计算方式,它可以计算出第二个或第三个参数的结果。

您需要做的另一件事是告诉评估者heightweight是什么意思。由于它们不以$字符开头,因此它们被解释为函数,而不是变量。如果他们以$开头,那就像评估它一样简单:

NSString *expression = @"if(($height > 0), ($weight+ 2 ), ( $weight-1 ) )";
NSDictionary *variables = @{@"height" : @42, @"weight" : @13};
NSNumber *result = [expression evaluateWithSubstitutions:variables evaluator:evaluator error:nil];

但是,由于$开头,因此它们是函数,这意味着您需要告诉评估者函数评估的内容。您可以通过为heightweight创建函数来实现此目的,就像您对if所做的那样:

[evaluator registerFunction:^DDExpression *(NSArray *args, NSDictionary *vars, DDMathEvaluator *eval, NSError **error) {
    return [DDExpression numberExpressionWithNumber:@42];
} forName:@"height"];

或者,您可以使它更具动态性并使用functionResolver DDMathEvaluatorNSDictionary *values = @{@"height": @42, @"weight": @13}; [evaluator setFunctionResolver:^DDMathFunction(NSString *name) { DDMathFunction f = ^(NSArray *args, NSDictionary *vars, DDMathEvaluator *eval, NSError **error) { NSNumber *n = [values objectForKey:name]; if (!n) { n = @0; } return [DDExpression numberExpressionWithNumber:n]; }; return f; }]; ,这是一个返回块(woooooo)的块,看起来像这样:

if

有了这两件(注册height并提供weightNSString *expression = @"if((height > 0), (weight+ 2 ), ( weight-1 ) )"; NSNumber *result = [expression evaluateWithSubstitutions:nil evaluator:evaluator error:nil]; 的值),您可以这样做:

@15

...并取回DDMathParser的正确结果。

(我计划让{{1}}允许未知函数回退到提供的变量值,但我还没完成它)

答案 1 :(得分:4)

您必须编写自己的解释器或找到支持这种表达式的解释器。

第一部分(条件)可以由NSPredicate评估。对于第二部分(计算),您将需要一些数学表达式评估。试试看https://github.com/davedelong/DDMathParser。也许你可以用DDMathParser同时做,但我不确定。