Question

I am working on my classwork of the calculator. I have been almost finished.

Now, my calculator can do e.g 2+3+5x6-3/2x6x7-8x9 and 6*9+7

But it cannot do 2*4*8.......

Anybody can tell me what's wrong of the code ? This is my code in below :

@implementation CalculatorBrain

- (void)setOperand:(double)aDouble
{
    operand = aDouble;
}


- (double)performOperation:(NSString *)operation
{
    if ([operation isEqual:@"sqrt"]){
        operand = sqrt(operand);
    }
    else if ([@"+/-" isEqual:operation]) {
        operand = - operand;
    }    
    else if ([@"%" isEqual:operation]) {
        operand = operand/ 100;
    }
    else if ([@"1/x" isEqual:operation]) {
        operand = 1/operand;
    }
    else if ([@"C" isEqual:operation]){
        operand = 0;
    }
    else if ([@"AC" isEqual:operation])  {
        operand = 0;
        alwaysFirstDigit = 0;
        digi1 = 0;
        digi2 = 0;

    }else {

        //Assign the first digit to alwaysFirstDigit, and decide the second digit to digi1 or digi2
        if (start == 0){

            alwaysFirstDigit = operand;
            NSLog(@"alwaysFirstDigit = %f", alwaysFirstDigit);

            operationFirst = operation;
            NSLog(@"operationFirst = %@", operationFirst);

            start = 1;

        }else  if (signFlag == 1){
            digi2 = operand;
            NSLog(@"digi2 = %f", digi2);

        }else {
            digi1 = operand;
            NSLog(@"digi1 = %f", digi1);
            }



        // This is an accumulator if first operation is equal to * or /
        if (signFlag == 1) {
        NSLog(@"RUN01 ");


            if ([@"+" isEqual:operationSecond]) 
            {
                digi1 = digi1 + digi2; 
            }
            else if ([@"*" isEqual:operationSecond]) 
            {
                digi1 = digi1 * digi2;
            }
            else if ([@"-" isEqual:operationSecond])
            {
                digi1 = digi1 - digi2;
            }
            else if ([@"/" isEqual:operationSecond]) 
            {
                digi1 = digi1 / digi2;
            }
            digi2 = 0;
            signFlag = 0;
            NSLog(@"This is digi1 in / = %f, %f", digi1, digi2);

        }


        // if operationFirst is = to + or -
        if (([operationFirst isEqual:@"+"]) || ([operationFirst isEqual:@"-"])){  
            NSLog(@"RUN02 ");

        // if operationFirst is = to + or -
            if (([operation isEqual:@"+"]) || ([operation isEqual:@"-"])){

                if ([@"+" isEqual:operationFirst]) 
                {
                    alwaysFirstDigit = alwaysFirstDigit + digi1; 
                }
                else if ([@"-" isEqual:operationFirst]) 
                {
                alwaysFirstDigit = alwaysFirstDigit - digi1;
                }

                operationFirst = operation;  //
                operand = alwaysFirstDigit;  //
            }

            if (([operation isEqual:@"*"]) || ([operation isEqual:@"/"])){
                NSLog(@"RUN03 ");

                signFlag = 1;  //
                operationSecond = operation;  //
                NSLog(@"operationSecond = %@", operationSecond);
            }
        }         


        // it's work
        if (([operationFirst isEqual:@"*"]) || ([operationFirst isEqual:@"/"])){  
            NSLog(@"This is digi1 in *** = %f", alwaysFirstDigit);
            NSLog(@"RUN04 ");

            if (([operation isEqual:@"+"]) || ([operation isEqual:@"-"])){
                NSLog(@"RUN05 ");

                if ([@"*" isEqual:operationFirst]) 
                {
                    alwaysFirstDigit = alwaysFirstDigit * digi1; 
                }
                else if ([@"/" isEqual:operationFirst]) 
                {
                    alwaysFirstDigit = alwaysFirstDigit / digi1;
                }
                operationFirst = operation;  //
                operand = alwaysFirstDigit;  //
            } 

            **// maybe here is the problem !!!!!**
            if (([operation isEqual:@"*"]) || ([operation isEqual:@"/"])){
                NSLog(@"RUN06 ");

                temp1 = digi1;

                operationFirst = operation;  //
                operand = alwaysFirstDigit;  //
            }
        }         


        if([operation isEqual:@"="]){
            NSLog(@"This is temp1 in ***** = %f", temp1);


            if ([@"+" isEqual:operationFirst]) 
            {
                alwaysFirstDigit = alwaysFirstDigit + digi1; 
            }
            else if ([@"*" isEqual:operationFirst]) 
            {
                alwaysFirstDigit = alwaysFirstDigit * digi1;
            }
            else if ([@"-" isEqual:operationFirst])
            {
                alwaysFirstDigit = alwaysFirstDigit - digi1;
            }
            else if ([@"/" isEqual:operationFirst]) 
            {
                alwaysFirstDigit = alwaysFirstDigit / digi1;
            }

                digi1 = 0;
                operand = alwaysFirstDigit;
                operation = @"";
                start = 0;
        }
    }
return operand;
}

@end
Was it helpful?

Solution

Under the section you comment with:

 // if operationFirst is = to + or -

There's some code there in an if clause that begins:

  if (([operation isEqual:@"*"]) || ([operation isEqual:@"/"])){

This code will never run because it's enclosed in an if block that says:

    if (([operationFirst isEqual:@"+"]) || ([operationFirst isEqual:@"-"])){  
        NSLog(@"RUN02 ");

I don't know whether this is your problem or not. You also might want to look at using a parser and grammar for the calculator language you're trying to implement. The book "Compilers, Principles, Techniques and Tools" by Alfred Aho and others, called the "Dragon Book" because it's had a dragon on the cover for decades, is the traditional source for making and understanding parsers for grammars like a desk calculator (which is one of the examples it uses), which really is what you're trying to code up. In addition to providing you with a perfect grammar for a desk calculator, it will also increase your knowledge of compilers by many times.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top