Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a stack in Objective C [closed]

I am working on an RPN calculator for the iPhone using iOS 5 for a programming assignment and I am not sure how to create a stack in Objective-C to push numbers onto. I could use a little help with this part, as the rest of the application is working out fine.

like image 618
seiryuu10 Avatar asked Sep 23 '26 10:09

seiryuu10


1 Answers

-(NSMutableArray *)operandStack // override the getter for lazy instantiation
{
    if (!_operandStack)
    {
        [self setOperandStack:[[NSMutableArray alloc] init]];
    }

    return _operandStack;
}


-(void)pushOperand:(double)operand
{
    NSNumber *operandObject = [NSNumber numberWithDouble:operand];
    [[self operandStack] addObject:operandObject];
}


-(double)popOperand
{
    NSNumber *operandObject = [[self operandStack] lastObject];

    if (operandObject)
    {
        [[self operandStack] removeLastObject];
        return [operandObject doubleValue];
    }
    else
    {
        return 0;
    }
}
like image 116
Richard Altenburg - Brainchild Avatar answered Sep 25 '26 01:09

Richard Altenburg - Brainchild



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!