0

I am using this GCMathParser for a calculator application. As GCMathParser doesn't support factorials, permutation etc, I've to find those operators (e.g !, nPr) manually and replace them with the result. How can I implement this efficiently?

Cœur
  • 37,241
  • 25
  • 195
  • 267
nickAtStack
  • 163
  • 8

1 Answers1

0

Why don't you try DDMathParser instead? It comes with support for factorial built-in, and has a simple API for adding new functions.

rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • thank you sir ! i just saw it works well for factorial too, but for functionality of permutation, i'm still stuck to the same problem(to identify what values are to be used as 'n' and 'r' in 'nPr' for calculation) As there can be many cases of using nPr in an expression. :( – nickAtStack Nov 09 '13 at 09:23
  • By far the simplest solution is to define a function `nPr(x,y)` using `-[DDMathEvaluator registerFunction:forName]:`. If you want to make `nPr` act like an infix operator, that will still be easier with `DDMathParser` than with `GCMathParser`. You'll need to add `nPr` as an operator token and modify the parser to recognize it. – rob mayoff Nov 09 '13 at 17:42
  • And if you haven't found it already, here's a way you can create an nCr function for DDMathParser: http://stackoverflow.com/a/17325997/115730 – Dave DeLong Nov 10 '13 at 03:06
  • that's a pretty good way sir, but i can't ask user to enter expression like nPr(5,2) it have to be the way 5nPr2 or 5P2 . For that even if i use DDMathParser, i'll have to either convert my expression to that (nPr(5,2)) way, or calculate factorials manually and if i have to do that then i think it'd be easy to stick to GCMathParser. With all respect, thanks to you both sir, for me it's time to go native :) – nickAtStack Nov 11 '13 at 05:15
  • @nickAtStack You could define a new binary operator of `nPr` or `P`, and that would let you have that syntax. You would do that by modifying the [list of defined operators](https://github.com/davedelong/DDMathParser/blob/master/DDMathParser/_DDOperatorInfo.m#L96) to insert the operator at the appropriate precedence. You'd also need to (of course) define the nPr function. – Dave DeLong Nov 23 '13 at 07:43