我正在努力为DDMathParser添加一个新函数(nCr)。我试图遵循DDMathParser的作者的这些说明:
https://stackoverflow.com/a/15599814/2521277
我的问题:我无法将DDExpressions与带变量的复杂表达式结合起来......
你能帮我添加函数nCr(http://en.wikipedia.org/wiki/Binomial_coefficient#Factorial_formula)
吗?谢谢!
答案 0 :(得分:0)
DDMathParser作者在这里。
所有功能的基本签名是:
DDExpression* ^(NSArray *arguments, NSDictionary *variables, DDMathEvaluator *evaluator, NSError **error);
因此,您需要创建一个实现nCr算法的块。有几种方法可以做到:
就个人而言,我建议使用最后一个,因为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结束。