实现一个函数,计算一个字符串的值,该字符串中只有+ - * /四种运算符, 没有括号。 //参数mathString:要计算的字符串; //返回值:把计算出来的结果返回 例如:传入:@"1+2-1

- (NSString *)calcString:(NSString *)mathString

{

    int sum=0;

    

    NSMutableString *strOperator=[[NSMutableString alloc] init];

    NSMutableString *strOperand=[[NSMutableString alloc] init];

    

    

    NSMutableArray *arrOperand=[mathString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"+-/*"]];

    NSMutableArray *arrOperator=[mathString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"0123456789 "]];

    int f1=(int)[mathString characterAtIndex:0];

    if (f1>48&&f1<57) {//第一个字母是数字

        [arrOperator removeObjectAtIndex:0];

        [arrOperator removeLastObject];

    }else

    {//第一个字母是运算符

        [arrOperand removeObjectAtIndex:0];

        [arrOperand insertObject:[NSString stringWithFormat:@"0"] atIndex:0];

        [arrOperator removeLastObject];

    }

    

    for(int i=0;i<[arrOperator count];i++)

    {

        if ([[arrOperator objectAtIndex:i] compare:@""]==NSOrderedSame) {

            [arrOperator removeObjectAtIndex:i];

            i=-1;

        }

    }

//    NSLog(@"%@",arrOperand);

//    NSLog(@"%@",arrOperator);

    while ([arrOperator count]) {

        for (int i=0; i<[arrOperator count]; i++) {

            //先运算符中的*/

            if ([[arrOperator objectAtIndex:i] characterAtIndex:0]=='*') {

                //计算运算符'*'两边的数字

                int count=

                [[arrOperand objectAtIndex:i] intValue]*([[arrOperand objectAtIndex:i+1] intValue]);

                  // NSLog(@"%d-%d-%d",[[arrOperand objectAtIndex:i] intValue],[[arrOperand objectAtIndex:i+1] intValue],i);

                NSRange range={i,2};

                [arrOperand removeObjectsInRange:range];

             

                [arrOperand insertObject:[NSString stringWithFormat:@"%d",count] atIndex:i];

                

                [arrOperator removeObjectAtIndex:i];

//                NSLog(@"%@",arrOperand);

//                NSLog(@"%@",arrOperator);

                i=-1;//从头检测

                

            }else if([[arrOperator objectAtIndex:i] characterAtIndex:0]=='/')

            {

                int count=

                ([[arrOperand objectAtIndex:i] intValue])/([[arrOperand objectAtIndex:i+1] intValue]);

                //计算运算符'/'两边的数字

                

                NSRange range={i,2};

                [arrOperand removeObjectsInRange:range];

                

                [arrOperand insertObject:[NSString stringWithFormat:@"%d",count] atIndex:i];

                [arrOperator removeObjectAtIndex:i];

                i=-1;//从头检测

            }

            

        }

        for (int i=0; i<[arrOperator count]; i++) {

            if([[arrOperator objectAtIndex:i] characterAtIndex:0]=='-')

            {

              //计算运算符'-'两边的数字

                int count=

                ([[arrOperand objectAtIndex:i] intValue])-([[arrOperand objectAtIndex:i+1] intValue]);

    

                NSRange range={i,2};

                [arrOperand removeObjectsInRange:range];

                

                [arrOperand insertObject:[NSString stringWithFormat:@"%d",count] atIndex:i];

                [arrOperator removeObjectAtIndex:i];

                i=-1;

                

            }else if ([[arrOperator objectAtIndex:i] characterAtIndex:0]=='+') {

               //计算运算符'+'两边的数字

                int count=

                ([[arrOperand objectAtIndex:i] intValue])+([[arrOperand objectAtIndex:i+1] intValue]);

               

                NSRange range={i,2};

                [arrOperand removeObjectsInRange:range];

                

                [arrOperand insertObject:[NSString stringWithFormat:@"%d",count] atIndex:i];

                [arrOperator removeObjectAtIndex:i];

                i=-1;

                

            }

        }

        

        

        

    }

    

  

    

    return [arrOperand objectAtIndex:0];

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值