【问题标题】:Adding nCr Function to DDMathParser (with variable parameters)将 nCr 函数添加到 DDMathParser(带有可变参数)
【发布时间】:2013-06-22 17:04:02
【问题描述】:

我正在努力向 DDMathParser 添加一个新函数 (nCr)。我尝试按照 DDMathParser 的作者的这些说明进行操作:

https://stackoverflow.com/a/15599814/2521277

我的问题:我无法将 DDExpressions 组合成带有变量的复杂表达式...

你能帮我添加函数 nCr (http://en.wikipedia.org/wiki/Binomial_coefficient#Factorial_formula)

谢谢!

【问题讨论】:

    标签: function add ddmathparser ncr


    【解决方案1】:

    DDMathParser 作者在这里。

    所有函数的基本签名是这样的:

    DDExpression* ^(NSArray *arguments, NSDictionary *variables, DDMathEvaluator *evaluator, NSError **error);
    

    因此,您需要创建实现 nCr 算法的这些块之一。有几种方法可以做到:

    1. 您可以完全按照文章中的方法实现函数,计算阶乘、减法和除法。
    2. 您可以完全根据现有的阶乘、减法和除法函数来实现该函数。
    3. 您可以将两者结合起来。

    我个人推荐最后一个,因为it saves having to re-evaluate the arguments,但也意味着你不必自己编写大部分逻辑。

    应该是这样的:

    DDMathFunction nCrFunction = ^(NSArray *args, NSDictionary *vars, DDMathEvaluator *eval, NSError **error) {
      if ([args count] != 2) {
        *error = [NSError errorWithDomain:DDMathParserErrorDomain code:DDErrorCodeInvalidNumberOfArguments userInfo:@{NSLocalizedDescriptionKey : @"nCr requires 2 arguments"}];
        return nil;
      }
    
      DDExpression *first = [args objectAtIndex:0];
      DDExpression *second = [args objectAtIndex:1];
    
      NSNumber *n = [first evaluateWithSubstitutions:vars evaluator:eval error:error];
      if (n == nil) { return nil; }
    
      NSNumber *k = [second evaluateWithSubstitutions:vars evaluator:eval error:error];
      if (k == nil) { return nil; }
    
      // some validation here to guarantee that 0 ≤ k ≤ n
    
      // now, re-box the numbers in expressions to pass off to other functions
      DDExpression *nExpression = [DDExpression numberExpressionWithNumber:n];
      DDExpression *kExpression = [DDExpression numberExpressionWithNumber:k];
    
      // build the algorithm
      DDExpression *f1 = [DDExpression functionExpressionWithFunction:DDOperatorFactorial arguments:@[kExpression] error:error]; // k!
    
      // the other half of the denominator
      DDExpression *subtract = [DDExpression functionExpressionWithFunction:DDOperatorMinus arguments:@[nExpression, kExpression] error:error]; // (n-k)
      DDExpression *f2 = [DDExpression functionExpressionWithFunction:DDOperatorFactorial arguments:@[subtract] error:error]; // (n-k)!
    
      // the full denominator
      DDExpression *denominator = [DDExpression functionExpressionWithFunction:DDOperatorMultiply arguments:@[f1, f2] error:error]; // k!(n-k)!
    
      // the numerator
      DDExpression *numerator = [DDExpression functionExpressionWithFunction:DDOperatorFactorial arguments:@[nExpression] error:error]; // n!
    
      // the whole thing
      DDExpression *final = [DDExpression functionExpressionWithFunction:DDOperatorDivide arguments:@[numerator, denominator] error:error]; // n!/(k!(n-k)!)
    
      return final;
    };
    

    有了这个块,你可以在你的DDMathEvaluator上注册函数:

    [[DDMathEvaluator sharedMathEvaluator] registerFunction:nCrFunction forName:@"nCr"];
    

    就是这样!

    现在你可以这样做了:

    NSNumber *n = [@"nCr(4, 3)" numberByEvaluatingString];
    

    警告:在浏览器中键入但未编译的代码。 警告实施者


    顺便说一句,如果这是您希望看到 DDMathParser 内置的功能,请在 Github 上 open a new issue

    【讨论】:

      猜你喜欢
      • 2016-09-27
      • 1970-01-01
      • 2022-01-20
      • 1970-01-01
      • 2021-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多