Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
309 views
in Technique[技术] by (71.8m points)

iphone - Convert to Float and Calculate

I have a string with multiple digits and operators

@"5+4-9/10".

How to get the result from it?

I want to use it within the calculator i am using. I will have to display the result dynamically when a digit or operator is pressed.

I used arity jar file in android. But i am not able to achieve something like that in iPhone.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

What about this -

NSString *formula = @"1+5*6";
NSExpression *exp = [NSExpression expressionWithFormat:formula];
NSNumber *resultForCustomFormula = [exp expressionValueWithObject:nil context:nil];
NSLog(@"%f", [resultForCustomFormula floatValue]);

EDIT : Now I thought about your requirement and made a method using NSScanner You will not believe I didn't use NSScanner before Mr. Borrrden suggested me to use it and I found it awesome. See below method -

-(NSMutableString *)formatString:(NSString *)formula
{
    // Let's check if there any wrong (.) value exm: 1/.2 or .7+3 
    // 1/0.2 and 0.7+3 are okay but above are incorrect so first fix them

    NSString *str = formula;
    NSInteger c = 0;
    for(int i=0; i<[str length]; i++)
    {

        if([[NSString stringWithFormat:@"%c",[str characterAtIndex:i]] isEqualToString:@"+"] ||
           [[NSString stringWithFormat:@"%c",[str characterAtIndex:i]] isEqualToString:@"-"] ||
           [[NSString stringWithFormat:@"%c",[str characterAtIndex:i]] isEqualToString:@"/"] ||
           [[NSString stringWithFormat:@"%c",[str characterAtIndex:i]] isEqualToString:@"*"])
        {
            if([str length] > i+1)
            {
                if([[NSString stringWithFormat:@"%c",[str characterAtIndex:i+1]] isEqualToString:@"."])
                {
                    formula = [formula stringByReplacingCharactersInRange:NSMakeRange(i+1+c, 1) withString:@"0."];
                    c++;
                }
            }
        }
    }

    // Now we will convert all numbers in float

    NSString *aString;
    float aFloat;
    NSMutableString *formattedString = [[NSMutableString alloc]init];

    NSScanner *theScanner = [NSScanner scannerWithString:formula];
    while ([theScanner isAtEnd] == NO) 
    {

        if([theScanner scanFloat:&aFloat])
        {
            [formattedString appendString:[NSString stringWithFormat:@"%f",aFloat]];
        }

        if([theScanner scanUpToCharactersFromSet:[NSCharacterSet decimalDigitCharacterSet] intoString:&aString])
        {
            [formattedString appendString:aString];
        }
    }
    return formattedString;
}

This will convert (2.222/.4)+9999-7+0.7*.13 in to (2.222000/0.400000)+9999.000000-7.000000+0.700000*0.130000.
Just call this method before using NSExpression.

NSString *formula = @"(2.222/.4)+9999-7+0.7*.13";
NSString *formattedString = [self formatString:formula];
NSExpression *exp = [NSExpression expressionWithFormat:formattedString];
NSNumber *resultForCustomFormula = [exp expressionValueWithObject:nil context:nil];
NSLog(@"Result = %f", [resultForCustomFormula floatValue]);

//OutPut: Result = 9997.646484

Note: I'm not saying that it will work in all formula strings. May be it will not work in some case. But it will work in general equations.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...