Reputation: 47348
I'm building an app that would analyze phone's motion through accelerometer and gyroscope output. It is likely that I will capture this motion as a set of variables that the app would keep track of internally.
I'm trying to include a way to graph these variables and equations involving these variables. I would like to offer the user an ability to adjust how the app displays equations of these variables by typing equations like a + b - c = d with the a,b,c variables being calculated by my app. The user can write equations and graph variables in the field. Since it has been ages since I wrote an algebraic parser in any language, I'm wandering if there's an expression parser in objective-c that I can readily plug into my app, where a user would type something like a + b - c = d into a text field and the app would tokenize and substitute variables that I provide in place of the user-entered placeholders.
Upvotes: 2
Views: 1458
Reputation: 919
There is a much easier approach to do this :) Check out the NSExpression foundation class. It is the same class used when you type an expression into the spotlight.
In Objective-C
NSExpression *expression = [NSExpression expressionWithFormat:@"2*4*3" argumentArray:nil];
NSNumber *result = [expression expressionValueWithObject:nil context:nil];
In Swift
var expression:NSExpression = NSExpression(format: "4 + (3 * 2)", argumentArray: [])
var result = expression.expressionValueWithObject(nil, context: nil)
Upvotes: 1
Reputation: 906
A couple of open source options:
GCMathParser http://www.apptree.net/parser.htm
DDMathParser https://github.com/davedelong/DDMathParser
Upvotes: 4