Need to write a calculator in Objective-C

Give some tips on which algorithms you use or your solution to solve some problems as follows:

//Some string
NSString *s = @"5*(2.56-1.79)-4.1";
-1
source share
2 answers

You can do this:

NSExpression *expression = [NSExpression expressionWithFormat:s];
float result = [[expression expressionValueWithObject:nil context:nil] floatValue];
NSLog(@"%f", result);
0
source

This is easy to do with expressionWithFormat

NSString *equation = @"5*(2.56-1.79)-4.1";
NSNumber *result = [NSExpression expressionWithFormat:equation];
NSLog(@"%@", result);
+3
source

Source: https://habr.com/ru/post/1548113/


All Articles