How to evaluate the string equation in ios

后端 未结 6 1239
一向
一向 2020-12-15 19:48

Is there way to solve the string equations in ios ?

For example

Input:

NSString * str =@\"1+2\";

Output:

<

相关标签:
6条回答
  • 2020-12-15 19:55

    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.

    0 讨论(0)
  • 2020-12-15 19:55

    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

    • GCMathParser
    • DDMathParser
    0 讨论(0)
  • 2020-12-15 19:56

    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);
    
    0 讨论(0)
  • 2020-12-15 19:57

    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.

    0 讨论(0)
  • 2020-12-15 20:03

    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):

    • DDMathParser
    • https://github.com/stribehou/Infix-expression-calculator
    0 讨论(0)
  • 2020-12-15 20:14

    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.

    0 讨论(0)
提交回复
热议问题