Amazon.com Widgets Exercise 21.3
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 18, 2013, 01:25:21 AM
Home Help Search chat Login Register   
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+  Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+  Programming in Objective-C, 4th edition
| |-+  Exercises
| | |-+  Chapter 21
| | | |-+  Exercise 21.3
Pages: [1]   Go Down
Print
Author Topic: Exercise 21.3  (Read 490 times)
tknp
Newbie
*
Posts: 23






« on: February 06, 2012, 03:18:28 AM »

I made all the changes in the clicksEquals method using the same .m file I have posted for the previous exercise.

ViewController.m changes
Code: (Objective-C)
-(IBAction) clickEquals
{
    if ( firstOperand == NO ) {
        [self storeFracPart];
       
        if (!myCalculator.operand1.denominator || !myCalculator.operand2.denominator) { // Checks for division by zero
            [myCalculator clear];   // Clears calc to avoid a valid result from the previous calculation messing up Convert button
            [displayString setString: @"Error"];
            display.text = displayString;
        }
        else {
            [myCalculator performOperation: op];
           
            [displayString appendString: @" = "];
            [displayString appendString: [myCalculator.accumulator convertToString]];
            display.text = displayString;
        }
       
        currentNumber = 0;
        isNumerator = YES;
        isNegative = NO;
        isWriting = NO;
        firstOperand = YES;
        [displayString setString: @""];
    }
}


full ViewController.m
Code: (Objective-C)
#import "ViewController.h"

@implementation ViewController
{
    char                op;
    int                 currentNumber;
    BOOL                firstOperand, isNumerator, isNegative, isWriting;
    Calculator          *myCalculator;
    NSMutableString     *displayString;
}

@synthesize display;

- (void)viewDidLoad
{
    firstOperand = YES;
    isNumerator = YES;
    isNegative = NO;
    isWriting = NO;
    displayString = [NSMutableString stringWithCapacity: 40];
    myCalculator = [[Calculator alloc] init ];
}

-(void) processDigit: (int) digit
{
    // Since we are processing a digit, set isWriting to YES
    if(!isWriting)
        isWriting = YES;
   
    if (isNegative)
        currentNumber = currentNumber * 10 - digit;
    else
        currentNumber = currentNumber * 10 + digit;
   
    [displayString appendString: [NSString stringWithFormat: @"%i", digit]];
    display.text = displayString;
}

-(IBAction) clickDigit: (UIButton *) sender
{
    int digit = sender.tag;
   
    [self processDigit: digit];
}

-(void) processOp: (char) theOp
{
    NSString *opStr;
   
    if (theOp == '-' && isWriting == NO) {   // checks for negative sign before first digit of entered num
        isNegative = YES;
        [displayString appendString: @"-"];
        display.text = displayString;
       
        return;
    }
   
    op = theOp;
   
    switch (theOp) {
        case '+':
            opStr = @" + ";
            break;
        case '-':
            opStr = @" - ";
            break;
        case '*':
            opStr = @" × ";
            break;
        case '/':
            opStr = @" ÷ ";
            break;
    }
   
    [self storeFracPart];
    firstOperand = NO;
    isNumerator = YES;
    isNegative = NO;
    isWriting = NO;
   
    [displayString appendString: opStr];
    display.text = displayString;
}

-(void) storeFracPart
{
    if (firstOperand) {
        if (isNumerator) {
            myCalculator.operand1.numerator = currentNumber;
            myCalculator.operand1.denominator = 1;  // e.g. 3 * 4/5
        }
        else
            myCalculator.operand1.denominator = currentNumber;
    }
    else if (isNumerator) {
        myCalculator.operand2.numerator = currentNumber;
        myCalculator.operand2.denominator = 1;  // e.g. 3/2 * 4
    }
    else {
        myCalculator.operand2.denominator = currentNumber;
        firstOperand = YES;
    }

    currentNumber = 0;
}

-(IBAction) clickOver
{
    [self storeFracPart];
    isNumerator = NO;
    isNegative = NO;
    [displayString appendString: @"/"];
    display.text = displayString;
}

// Arithmetic Operation keys

-(IBAction) clickPlus
{
    [self processOp: '+'];
}

-(IBAction) clickMinus
{
    [self processOp: '-'];
}

-(IBAction) clickMultiply
{
    [self processOp: '*'];
}

-(IBAction) clickDivide
{
    [self processOp: '/'];
}

// Misc. Keys

-(IBAction) clickEquals
{
    if ( firstOperand == NO ) {
        [self storeFracPart];
       
        if (!myCalculator.operand1.denominator || !myCalculator.operand2.denominator) { // Checks for division by zero
            [myCalculator clear];   // Clears calc to avoid a valid result from the previous calculation messing up Convert button
            [displayString setString: @"Error"];
            display.text = displayString;
        }
        else {
            [myCalculator performOperation: op];
           
            [displayString appendString: @" = "];
            [displayString appendString: [myCalculator.accumulator convertToString]];
            display.text = displayString;
        }
       
        currentNumber = 0;
        isNumerator = YES;
        isNegative = NO;
        isWriting = NO;
        firstOperand = YES;
        [displayString setString: @""];
    }
}

-(IBAction) clickClear
{
    isNumerator = YES;
    isNegative = NO;
    isWriting = NO;
    firstOperand = YES;
    currentNumber = 0;
    [myCalculator clear];
   
    [displayString setString: @""];
    display.text = displayString;
}

-(IBAction) clickConvert
{
    display.text = [NSString stringWithFormat: @"%f", [myCalculator.accumulator convertToNum]];
}

@end
Logged
jimmac
Newbie
*
Posts: 13






« Reply #1 on: May 15, 2012, 03:18:57 PM »

tknp's code satisfies the Exercise completely, but, going beyond the Exercise's requirements, there is still a possibility for a divide by zero condition:

What if a user enters a first fraction, then chooses a divide operation, and then enters a second fraction with a numerator of 0?

Appending another condition to the if statement in clickEquals checks for the above scenario:

Code: (Objective-C)

if ( (!myCalculator.operand1.denominator) || (!myCalculator.operand2.denominator) || (!myCalculator.operand2.numerator && (op == '/')) ) {

Logged
Pages: [1]   Go Up
Print
Jump to:  



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.