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

来源:互联网 发布:品茗软件是什么意思 编辑:程序博客网 时间:2024/05/19 09:17

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

{

   int sum=0;

    

    NSMutableString *strOperator=[[NSMutableStringalloc] init];

    NSMutableString *strOperand=[[NSMutableStringalloc] init];

    

    

    NSMutableArray *arrOperand=[mathStringcomponentsSeparatedByCharactersInSet:[NSCharacterSetcharacterSetWithCharactersInString:@"+-/*"]];

    NSMutableArray *arrOperator=[mathStringcomponentsSeparatedByCharactersInSet:[NSCharacterSetcharacterSetWithCharactersInString:@"0123456789 "]];

   int f1=(int)[mathStringcharacterAtIndex:0];

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

        [arrOperatorremoveObjectAtIndex:0];

        [arrOperatorremoveLastObject];

    }else

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

        [arrOperandremoveObjectAtIndex:0];

        [arrOperand insertObject:[NSStringstringWithFormat:@"0"]atIndex:0];

        [arrOperatorremoveLastObject];

    }

    

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

    {

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

            [arrOperatorremoveObjectAtIndex:i];

            i=-1;

        }

    }

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

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

   while ([arrOperator count]) {

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

           //先运算符中的*/

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

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

               int count=

                [[arrOperandobjectAtIndex:i] intValue]*([[arrOperandobjectAtIndex:i+1]intValue]);

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

               NSRange range={i,2};

                [arrOperandremoveObjectsInRange:range];

             

                [arrOperandinsertObject:[NSStringstringWithFormat:@"%d",count]atIndex:i];

                

                [arrOperatorremoveObjectAtIndex:i];

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

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

                i=-1;//从头检测

                

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

            {

               int count=

                ([[arrOperandobjectAtIndex:i] intValue])/([[arrOperandobjectAtIndex:i+1]intValue]);

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

                

               NSRange range={i,2};

                [arrOperandremoveObjectsInRange:range];

                

                [arrOperandinsertObject:[NSStringstringWithFormat:@"%d",count]atIndex:i];

                [arrOperatorremoveObjectAtIndex:i];

                i=-1;//从头检测

            }

            

        }

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

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

            {

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

               int count=

                ([[arrOperandobjectAtIndex:i] intValue])-([[arrOperandobjectAtIndex:i+1]intValue]);

    

               NSRange range={i,2};

                [arrOperandremoveObjectsInRange:range];

                

                [arrOperandinsertObject:[NSStringstringWithFormat:@"%d",count]atIndex:i];

                [arrOperatorremoveObjectAtIndex:i];

                i=-1;

                

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

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

               int count=

                ([[arrOperandobjectAtIndex:i] intValue])+([[arrOperandobjectAtIndex:i+1]intValue]);

               

               NSRange range={i,2};

                [arrOperandremoveObjectsInRange:range];

                

                [arrOperandinsertObject:[NSStringstringWithFormat:@"%d",count]atIndex:i];

                [arrOperatorremoveObjectAtIndex:i];

                i=-1;

                

            }

        }

        

        

        

    }

    

  

    

   return [arrOperand objectAtIndex:0];

}

0 0
原创粉丝点击