How to evaluate the string equation in ios

后端 未结 6 1266
一向
一向 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: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);
    

提交回复
热议问题