Stanford CS193P: Entering Floating Point Numbers Into The RPN Calculator

The second task of Assignment 1 from the Stanford CS193P iPhone App Class requires you to make the RPN Calculator accept floating point numbers, as follows:

This task is pretty simple once you know how to do it. So here is what my strategy for solving it was:


To start with, you need to add a period button to your calculator. Since a period is part of the number, you can point it to the already existing “digitPressed” method in the CalculatorViewController implementation file. Now, you will be able to enter a period along with the digits.

The second part of this problem involves making sure the user can only enter the period once while entering a number. To keep track of how many times the user pressed the period, I created an NSMutableArray. This is what my code now looks like up to the digitPressed method.

#import "CalculatorViewController.h"
#import "CalculatorBrain.h"

@interface CalculatorViewController()
@property (nonatomic) BOOL userIsInTheMiddleOfEnteringANumber;
@property (nonatomic, strong) CalculatorBrain *brain;
@property (nonatomic, strong) NSMutableArray *periodPressed;
@end

@implementation CalculatorViewController

@synthesize display = _display;
@synthesize userIsInTheMiddleOfEnteringANumber = _userIsInTheMiddleOfEnteringANumber;
@synthesize brain = _brain;
@synthesize periodPressed = _periodPressed;

- (CalculatorBrain *) brain
{
    if(!_brain) _brain = [[CalculatorBrain alloc] init];
    return _brain;
}

- (NSMutableArray *) periodPressed
{
    if(_periodPressed == nil)_periodPressed = [[NSMutableArray alloc] init];
    return _periodPressed;
}

Now that the periodPressed array (which keeps track of the periods pressed) is initialized, I modified the digitPressed method code to keep track of the periods pressed and to make sure the user can only enter 1 period. To do this, I add a period to the periodPressed array every time a user presses a period. If the length of the period array is equal to 1 (the user only pressed 1 period), then the period is added to the “digit” string. Otherwise, the period is not added. So here is my digitPressed code:

- (IBAction)digitPressed:(UIButton *)sender 
{
    NSString *digit = [sender currentTitle];
    
    if(self.userIsInTheMiddleOfEnteringANumber) {
        if([digit isEqualToString:@"."]) {
            [self.periodPressed addObject:digit];
            if([self.periodPressed count] == 1) {
                self.display.text = [self.display.text stringByAppendingString:digit];  
            }
        } else {
            self.display.text = [self.display.text stringByAppendingString:digit];  
        }
        
    }
    else {
        if([digit isEqualToString:@"."]) {
            [self.periodPressed addObject:digit];
        }
        self.display.text = digit;
        self.userIsInTheMiddleOfEnteringANumber = YES;
    }
    
        
}

But wait, you’re not done yet! Once you click Enter, the period won’t be added to the second number, because the periodPressed array is of a length greater than 1. You therefore need to clear the periodPressed array in the enterPressed method. Here is my code for doing this:

- (IBAction)enterPressed 
{
    [self.brain pushOperand:[self.display.text doubleValue]];
    self.userIsInTheMiddleOfEnteringANumber = NO;
    [self.periodPressed removeAllObjects];
}

So now, you calculator excepts floating point numbers!

Enjoy the article? Join over 17,500+ Swift developers and enthusiasts who get my weekly updates.