-1

Can I do something like this in Objc:

NSString* myEquation = @"(3 * 4 / 2 ) - 5";
[myEquation calculate]; // return 1

Thanks.

2
  • 1
    what does this have to do with javascript?
    – jbabey
    Aug 3, 2012 at 13:40
  • OK sorry for that, I've removed the tag Aug 3, 2012 at 13:45

2 Answers 2

2

http://apptree.net/parser.htm should do what you want.

NSString to equation The question seems to have been already asked here. So make sure to search first :).

1

You can do it without external libraries: create a predicate with your expression followed by == 0, and take its left expression. You can then evaluate that expression, like this:

NSExpression *e = [(NSComparisonPredicate *)[NSPredicate
    predicateWithFormat:@"(3 * 4 / 2 ) - 5==0"]
    leftExpression];
NSLog(@"%@", [e expressionValueWithObject:self context:nil]);

Not the answer you're looking for? Browse other questions tagged or ask your own question.