向DDMathParser添加nCr函数(具有可变参数) [英] Adding nCr Function to DDMathParser (with variable parameters)
问题描述
我正在努力向DDMathParser添加新功能(nCr)。我尝试遵循DDMathParser作者的以下指示:
I'm struggling to add a new function (nCr) to DDMathParser. I've tried to follow these instructions by the author of DDMathParser:
https ://stackoverflow.com/a/15599814/2521277
我的问题:我无法将DDExpressions组合到带有变量的复杂表达式中。 。
My problem: I can't manage to combine DDExpressions to a complex expression with variables...
您能帮我添加函数nCr吗?( http://en.wikipedia.org/wiki/Binomial_coefficient#Factorial_formula )
Can you help me to add the function nCr (http://en.wikipedia.org/wiki/Binomial_coefficient#Factorial_formula)
谢谢!
推荐答案
DDMathParser作者在这里。
DDMathParser author here.
所有功能的基本签名是:
The basic signature for all functions is this:
DDExpression* ^(NSArray *arguments, NSDictionary *variables, DDMathEvaluator *evaluator, NSError **error);
因此,您需要创建实现nCr算法的这些块之一。您可以通过以下两种方法进行操作:
So you need to create one of these blocks that implements the nCr algorithm. There are a couple of ways you could do it:
- 您可以完全按照本文中所示的方式实现该功能,计算阶乘,然后执行
- 您可以完全根据现有的阶乘,减法和除法函数来实现该功能。
- 您可以将两者结合起来。
我个人建议使用最后一个,因为它省去了重新评估参数的麻烦,但也意味着您不必自己编写大多数逻辑。
Personally, I'd recommend the last one, because it saves having to re-evaluate the arguments, but also means you don't have to write most of the logic yourself.
应该是这样的:
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];
警告:在浏览器中键入但未编译的代码。 Caveat实施器
如果这是您想要的功能,要查看DDMathParser的内置文件,请在Github上打开新的问题。
Incidentally, if this is a function you'd like to see built-in to DDMathParser, please open a new issue over at Github.
这篇关于向DDMathParser添加nCr函数(具有可变参数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!