Source

CS193P Assignment 1 / Calculator / CalculatorViewController.m

//
//  CalculatorViewController.m
//  Calculator
//
//  Created by Jay Yu on 29/06/12.
//  Copyright (c) 2012 JayYu.com. All rights reserved.
//

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

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

@implementation CalculatorViewController

@synthesize brain = _brain;
@synthesize display = _display;
@synthesize history = _history;
@synthesize userIsInTheMiddleOfEnteringANumber = _userIsInTheMiddleOfEnteringANumber;
@synthesize userHasEnteredDot = _userHasEnteredDot;

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

- (void)appendHistory:(NSString *)aString;
{
    // Implements Required Task #4, Assignment 1
    if (! self.history.text) {
        self.history.text = @"";
    } else {
        self.history.text = [self.history.text stringByAppendingString:@" "];        
    }

    if ([aString isEqualToString:@"="]) {
        // Extra Credit #2: put an "=" at the end of text label
        // Remove extraneous "=" already in history
        self.history.text = [self.history.text stringByReplacingOccurrencesOfString:@" = "
                                                                         withString:@" "];
    }
    self.history.text = [self.history.text stringByAppendingString:aString];

    // Limit no. of characters in history so that font size of label will not
    // get smaller and smaller.
    NSUInteger length = [self.history.text length];
    NSUInteger limit = 38;
    if (length >= limit) {
        self.history.text = [self.history.text substringFromIndex:length - limit];
    }
    
}

- (IBAction)digitPressed:(UIButton *)sender
{
    NSString *digit = [sender currentTitle];
    if ([digit isEqualToString:@"."]) {
        if (self.userHasEnteredDot) {
            return;
        }
        if (! self.userIsInTheMiddleOfEnteringANumber) {
            // shortcut for user: start entering an operand with "." will automatically
            // enter "0."
            self.display.text = @"0.";
            self.userIsInTheMiddleOfEnteringANumber = YES;
            return;
        }
        self.userHasEnteredDot = YES;
    }

    if (self.userIsInTheMiddleOfEnteringANumber) {
        self.display.text = [self.display.text stringByAppendingString:digit];            
    } else {
        self.display.text = digit;
        if (! [digit isEqualToString:@"0"]) {
            self.userIsInTheMiddleOfEnteringANumber = YES;
        }
    }
}

- (IBAction)enterPressed
{
    NSString *dt = self.display.text;
    [self.brain pushOperand:[dt doubleValue]];
    [self appendHistory:dt];
    self.userIsInTheMiddleOfEnteringANumber = NO;
    self.userHasEnteredDot = NO;
}

- (IBAction)operationPressed:(UIButton *)sender
{
    if (self.userIsInTheMiddleOfEnteringANumber) {
        [self enterPressed];
    }
    NSString *operation = sender.currentTitle;
    double result = [self.brain performOperation:operation];
    [self appendHistory:operation];
    self.display.text = [NSString stringWithFormat:@"%g", result];
    
    [self appendHistory:@"="];
}

- (IBAction)allClear
{
    [self.brain clearStates];
    self.display.text = @"0";
    self.history.text = @"";
    self.userIsInTheMiddleOfEnteringANumber = NO;
    self.userHasEnteredDot = NO;
}

- (IBAction)backspacePressed
{
    if (self.userIsInTheMiddleOfEnteringANumber) {
        NSUInteger length = [self.display.text length];
        if (length > 0) {
            self.display.text = [self.display.text substringToIndex:length - 1];
            // Check if a decimal dot is still there
            // Based on Hint #1, Assignment 1
            NSRange range = [self.display.text rangeOfString:@"."];
            if (range.location == NSNotFound) {
                self.userHasEnteredDot = NO;
            } else {
                self.userHasEnteredDot = YES;
            }
        }
        if (length == 0) {
            self.userIsInTheMiddleOfEnteringANumber = NO;
            self.display.text = @"0";
        }
    }
}

- (void)viewDidUnload {
    [self setHistory:nil];
    [super viewDidUnload];
}
@end