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.
-(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;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With