davedelong/DDMathParser

Can i evaluate operation left to right by ignoring operator precedence?

hadwanihardik opened this issue · 2 comments

Can i evaluate operation left to right by ignoring operator precedence?

Are you asking the same question as asked in issue #35?

+(NSString *)evaluateMathOperationWithoutOperatorPrecedence:(NSString *)string
{
if (string.length>0)
{
NSString *result= @"";
NSInteger position=0;

    for (NSInteger charIdx=0; charIdx<string.length; charIdx++)
    {
        char ch=[string characterAtIndex:charIdx];
      //  NSLog(@"%c", ch);
        NSCharacterSet *numericSet = [NSCharacterSet decimalDigitCharacterSet];
        if (![numericSet characterIsMember:ch] && (ch!='.'))
        {
            if (result.length==0)
            {
             //   NSLog(@"%@",[string substringWithRange:NSMakeRange(position, charIdx)]);
               result=[self evaluateMathOperation:[string substringWithRange:NSMakeRange(position, charIdx)]];
            }
            else
            {
               // NSLog(@"%@",[string substringWithRange:NSMakeRange(position, charIdx-position)]);
               // NSLog(@"%@",NSStringFromRange(NSMakeRange(position, charIdx-position)));
               result= [self evaluateMathOperation:[result stringByAppendingString:[string substringWithRange:NSMakeRange(position, charIdx-position)]]];

            }
            position=charIdx;
        }

        if (charIdx==string.length-1 && ([numericSet characterIsMember:ch]||ch=='%'))
        {
            result= [self evaluateMathOperation:[result stringByAppendingString:[string substringFromIndex:position]]];

        }
        else if (charIdx==string.length-1 && ch=='.')
        {
           DDMathEvaluator *eval=[DDMathEvaluator defaultMathEvaluator];
         //  NSLog(@"%@",[eval evaluateString:string withSubstitutions:nil]);

           [[DDMathOperatorSet defaultOperatorSet] setInterpretsPercentSignAsModulo:NO];
            return  [[eval evaluateString:string withSubstitutions:nil] stringValue];

        }

    }
    return result;

}
else
{
    return @"";
}

}