18

Is there way to solve the string equations in ios ?

For example

Input:

NSString * str =@"1+2";

Output:

NSInteger result = 3 // i.e sum of 1+2 from str

How to go about this and get expected result!

Please help!

Vinayak Kini
  • 2,919
  • 21
  • 35

6 Answers6

32

You can use NSExpression for this:

NSExpression *expression = [NSExpression expressionWithFormat:@"1+2"];
NSLog(@"%@", [expression expressionValueWithObject:nil context:nil]);

For further information read the documentation of the used methods.

Amin Negm-Awad
  • 16,582
  • 3
  • 35
  • 50
  • 3
    +1 This is correct. You don't need something like my DDMathParser unless you need much more flexibility in the parsing/evaluation process. – Dave DeLong May 28 '13 at 16:05
4

If your mathematical expressions are simple enough, you could go the manual route as suggested by Rajan Balana.

To evaluate more complex expressions, you could use/abuse NSPredicate in combination with NSExpression as described in this Blog post:http://funwithobjc.tumblr.com/post/1553469975/abusing-nspredicate

Note that NSPredicate is only necessary if your input is really an equation (including the right part):

NSPredicate* parsedExpression = [NSPredicate predicateWithFormat:@"1+2=x"];
NSExpression* leftPart = [(NSComparisonPredicate*)parsedExpression leftExpression];
NSNumber* evaluatedResult = [leftPart expressionValueWithObject:nil context:nil];
NSLog(@"Expr:%@", evaluatedResult);

To achieve proper parsing, you can use one of the math parsers for Objective-C out there. I haven't used them myself, but the popular ones seem to be

Thomas Zoechling
  • 34,177
  • 3
  • 81
  • 112
1

Use this :

NSString * str =@"1+2";

NSArray *arr = [str componentsSeparatedByString:@"+"];
int sum = 0;
for (int i = 0; i < [arr count]; i++) {
    sum += [[arr objectAtIndex:i] intValue];
}
NSLog(@"sum : %d",sum); 

Output : sum = 6

You can use NSExpression also.

Expressions are the core of the predicate implementation. When expressionValueWithObject: is called, the expression is evaluated, and a value returned which can then be handled by an operator. Expressions can be anything from constants to method invocations. Scalars should be wrapped in appropriate NSValue classes.

Example :

    NSLog(@"sum : %@", [[NSExpression expressionWithFormat:@"1+4"] expressionValueWithObject:nil context:nil]);
Output :- sum : 5
    NSLog(@"mulitple : %@", [[NSExpression expressionWithFormat:@"2*4"] expressionValueWithObject:nil context:nil]);
Output :- mulitple : 8

Hope it helps you.

Nishant Tyagi
  • 9,893
  • 3
  • 40
  • 61
0

What you can do is, You can get the components of the string separated by the sign "+" using this method and then you will get the array of components i.e. 1,2.

NSArray* foo = [str componentsSeparatedByString: @"+"];

int result = [[foo objectAtIndex:0] integerValue] + [[foo objectAtIndex:1] integerValue];

and then you can use the integer value for those two elements and add them and store the result in an integer variable.

Rajan Balana
  • 3,775
  • 25
  • 42
0

I think you need to subString your string, and store each number in a new NSstring(integerValue) or in Integer variable.

example:

NSString *str = @"1+2";
    int x = [[str substringToIndex:1]integerValue];
    int y = [[str substringFromIndex:1]integerValue];
    int z = x+y;
    NSLog(@"Sum === %d",z);
M.Alatrash
  • 1,270
  • 1
  • 12
  • 30
0

Depends on complexity of your input string equations. You can use Shunting-yard algorithm for parsing mathematical equations and then calculate the equation from the Abstract Syntact Tree (AST)

But I expect you are looking for some easy, preferably already done solution. In that case you can take a try one of these (no guarantee, I haven't tried those, just googled):

Lukas Kukacka
  • 7,604
  • 2
  • 25
  • 50