A simple mathematical expression is solved in the lens from left to right

I just have this expression in an object with NSString: @ "10 + 5 * 2". I wanted to solve this expression automatically and came across a solution:

UIWebView *webView = [[UIWebView alloc] init]; NSString *result = [webView stringByEvaluatingJavaScriptFromString:expression]; NSLog(@"%@",result); 

This works well, but it gives the result: 20. Actually, it evaluates "*" first means first 5 * 2 = 10 + 10. But I want to remove this priority and just want each operator to have the same priority from left to right, so that the answer can be equal to 10 + 5 = 15 * 2 = 30.

Although writing your own function is the last option, I really don't want to interfere with string functions. I would like to use something inline.

Thanks,

******* Working solution ********

Thank you for your help Eiko and Ozair !! Based on their answers, I wrote this bunch of code that works great for me!

 NSString *expression = lblBox.text; // Loads the original expression from label. expression = [expression stringByReplacingOccurrencesOfString:@"+" withString:@")+"]; expression = [expression stringByReplacingOccurrencesOfString:@"-" withString:@")-"]; expression = [expression stringByReplacingOccurrencesOfString:@"*" withString:@")*"]; expression = [expression stringByReplacingOccurrencesOfString:@"/" withString:@")/"]; NSUInteger count = 0, length = [expression length]; NSRange range = NSMakeRange(0, length); while(range.location != NSNotFound) { range = [expression rangeOfString: @")" options:0 range:range]; if(range.location != NSNotFound) { range = NSMakeRange(range.location + range.length, length - (range.location + range.length)); count++; } } for (int i=0; i<count; i++) { expression = [@"(" stringByAppendingString:expression]; } UIWebView *webView = [[UIWebView alloc] init]; NSString *result = [webView stringByEvaluatingJavaScriptFromString:expression]; [webView release]; NSLog(@"%@ = %@",expression, result); 
+4
source share
3 answers

Not sure if he is the same as @Ozair Kafray says ...

For each statement added to the line, first add ) and add the corresponding ( at the beginning. That is.

 10 (10 ) + (10 ) + 5 ((10 ) + 5 ) * ((10 ) + 5 ) * 2 

Just make sure that when you delete an operator, parentheses are also removed.

+6
source

There is no built-in way to do this that I know of. I think you just need to roll up your sleeves and write a function. If you want to use objective-c, use NSScanner.

+3
source

Like your answer to my question in the comments, you generate a string during input. I would suggest that you add brackets after each operand except the first.

So you are not adding anything to the line after 10 , then the + operator is added. After that, for each operand which is 5 , add a closing bracket ) and a corresponding opening bracket ( to the beginning of the line. Similarly after 2 Thus, you do not have to write something of your own to process the line.

+3
source

All Articles