Assignment #1 Task #2

Please note, this blog entry is from a previous course. You might want to check out the current one.

Your calculator already works with floating point numbers (e.g. if you touch the buttons 3 Enter 4 / it will properly show the resulting value of 0.75), however, there is no way for the user to enter a floating point number. Remedy this. Allow only legal floating point numbers to be entered (e.g. “192.168.0.1” is not a legal floating point number). Don’t worry too much about precision in this assignment.

Add an additional button on the left hand side of the zero button, by copying one of the number buttons. Add a new property userIsInTheMiddleOfEnteringAFloat to CalculatorViewController.m, synthesize it and adjust the digitPressed andenterPressed functions:

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

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

Leave a Reply

Your email address will not be published.