Question

Part of this assignment includes printing out on the display the current equation that is present to be solved, for that I use the following methods:

+ (NSString *)descriptionOfTopOfStack:(NSMutableArray *)stack {
    NSMutableString *programFragment = [NSMutableString stringWithString:@""];

    id topOfStack = [stack lastObject];
    if (topOfStack) [stack removeLastObject];

    if ([topOfStack isKindOfClass:[NSNumber class]]) {
        [programFragment appendFormat:@"%g", [topOfStack doubleValue]];
    } else if ([topOfStack isKindOfClass:[NSString class]]) {
        NSString *operation = topOfStack;
        if ([self isDoubleOperandOperation:operation]) {
            [programFragment appendFormat:@"(%@ %@ %@)", [self descriptionOfTopOfStack:stack], operation, [self descriptionOfTopOfStack:stack]];
        } else if ([self isSingleOperandOperation:operation]) {
            [programFragment appendFormat:@"%@( %@ )", operation, [self descriptionOfTopOfStack:stack]];
        } else if ([ self isNoOperandOperation:operation]) {
            [programFragment appendFormat:@"%@", operation];
        } else if ([self isVariable:operation]) {
            [programFragment appendFormat:@"%@", operation];
        }
    }

    return programFragment;
}

+ (NSString *)descriptionOfProgram:(id)program {
    NSMutableArray *stack;
    if ([program isKindOfClass:[NSArray class]]) {
        stack = [program mutableCopy];
    }

    return [self descriptionOfTopOfStack:stack];
}

My program computes the results and everything just fine, the only problem is that when I enter a variable, digit or single operand operation the display only shows said last entry, because it doesn't continue to iterate over the rest of the values present in the array, because no other recursive calls are made, any idea how I can make the program execute throughout the entire stack and not have it break the output?

Was it helpful?

Solution

I am not quite sure what you mean. The recursion should stop at a variable, digit or single operand operation. Although for a sin(operand) operation it should continue with the operand.

Did you take into account that your stack might be not completely defined?

Say you enter: 3 Enter 5 + 6 Enter 7 * 9 sqrt

this should translate to: 3+5, 6, sqrt(7*9)

So you have three elements still on your stack, but your approach stopped at sqrt(7*9).

You need to add a check at the to see if there is anything left on the stack, and continue if necessary (and add the comma's).

OTHER TIPS

OK, another hint then (to be added at the end):

if ([stack count]) {    // did I finish the entire stack?
   [programFragment appendFormat:@"%@, %@", [self describeStack:stack], programFragment];
}

Interestingly you have used a NSMutableString, I did it with a NSString and used the class method stringWithFormat. So each time my result is a new string. I do not know if either approach is better.

aleene already answered, but just to clarify. I added the [stack count] check in the method that calls the recursive function.

+ (NSString *)descriptionOfProgram:(id)program {
        NSMutableArray *stack;
        NSString *strDesc = @"";

        if ([program isKindOfClass:[NSArray class]]) {
            // Make a consumable, mutable copy:
            stack = [program mutableCopy];
        }

        while (stack.count) {
            strDesc = [strDesc stringByAppendingString:[self descriptionOfTopOfStack:stack]];
            if (stack.count) {
                // More statements still on stack. We will loop again, but first, append comma separator:
                strDesc = [strDesc stringByAppendingString:@", "];
            }
        }

        return strDesc;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top