0

A text field contains mathematical expressions like: 12+45-6 Is possible to convert this string to a number?

Can we use the predicate facility?

NSString *foo = @"((a*b)/c+(d/5))+15";
// dummy predicate that contains our expression
NSPredicate *pred = [NSPredicate predicateWithFormat:[foo stringByAppendingString:@" == 42"]];
NSExpression *exp = [pred leftExpression];
NSNumber *result = [exp expressionValueWithObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:4], @"a",[NSNumber numberWithInt:7], @"b",[NSNumber numberWithInt:2], @"c",[NSNumber numberWithInt:20], @"d",nil]context:nil];
NSLog(@"%@", result); // logs "33"<br/>

I also found this https://github.com/unixpickle/ANExpressionParser

jscs
  • 63,694
  • 13
  • 151
  • 195
  • Just FYI - there is this discussion going on, too: http://stackoverflow.com/questions/6809655/math-expression-evaluation-very-fast-with-objective-c/6809698#6809698 – Monolo Jul 24 '11 at 21:54
  • 2
    "Please send the codez" is frowned upon here at SO. Just sayin'. – Seva Alekseyev Jul 24 '11 at 23:41
  • Possible duplicate of http://stackoverflow.com/questions/4892152/what-is-a-fast-c-or-objective-c-math-parser – lhf Jul 25 '11 at 11:40

4 Answers4

2

One solution that doesn't rely on external libraries is to first convert the string into a sequence of tokens, then evaluate the expression using the shunting-yard algorithm chained onto a simple stack machine.

Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365
  • thanks for the help ^^, can you give a link to a sample xcode project please? –  Jul 24 '11 at 22:09
  • The page I linked to provides a C implementation. I don't know that you'll find a pre-baked XCode project for this anywhere. If this sounds like too much hard work (it's more a couple-of-hours project than a quick snap-the-lego-together exercise), then you should use the built-in JavaScript engine, though that entails firing up the a web browser control, which is quite a heavyweight solution. – Marcelo Cantos Jul 24 '11 at 22:17
1

You need to parse this input using a parser then evaluate it. I have heard of people having success using muParser for this, to save you writing it all yourself. Check it out. http://muparser.sourceforge.net/

0

Yes. Read up on "expression evaluation", or use a ready-made script interpreter. JavaScript is available to all iOS programs as a part of WebView.

Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281
  • Works, but I don't think this simplicity is worth the terrible performance. –  Jul 24 '11 at 21:57
  • thanks for the help ^^, can you give a link to a sample xcode project please? –  Jul 24 '11 at 22:10
0

Yes, of course it's possible.

You should look into infix to postfix conversion using a stack: http://scriptasylum.com/tutorials/infix_postfix/algorithms/infix-postfix/index.htm

And then look into how to evaluate a postix expression, also using a stack: http://scriptasylum.com/tutorials/infix_postfix/algorithms/postfix-evaluation/index.htm

Paul
  • 139,544
  • 27
  • 275
  • 264
  • thanks for the help ^^, can you give a link to a sample xcode project please? –  Jul 24 '11 at 22:10