问题
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!
回答1:
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.
回答2:
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
回答3:
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.
回答4:
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.
回答5:
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);
回答6:
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
来源:https://stackoverflow.com/questions/16785873/how-to-evaluate-the-string-equation-in-ios