Amazon.com Widgets 21-5 with ARC pt 3 Calculator code
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 22, 2013, 03:17:04 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
|-+  Old Stuff
| |-+  Answers to Exercises
| | |-+  Chapter 21
| | | |-+  21-5 with ARC pt 3 Calculator code
Pages: [1]   Go Down
Print
Author Topic: 21-5 with ARC pt 3 Calculator code  (Read 324 times)
Fred
Newbie
*
Posts: 46






« on: November 02, 2011, 02:06:49 PM »

part 3 Calculator code

Code: (Objective-C)
//  Calculator.h
//  Fraction-Calculator
//
//  Created by Fred H Koch on 10/25/11.
//

#import <UIKit/UIKit.h>
#import "Fraction.h"

@interface Calculator : NSObject
{
    Fraction *operand1;
    Fraction *operand2;
    Fraction *accumulator;
}

@property (strong, nonatomic) Fraction *operand1, *operand2, *accumulator;

- (Fraction *) performOperation: (char) op;
- (void) clear;

@end


Code: (Objective-C)
//  Calculator.m
//  Fraction-Calculator
//
//  Created by Fred H Koch on 10/25/11.
//

#import "Calculator.h"

@implementation Calculator

@synthesize operand1, operand2, accumulator;

- (id) init
{
    self = [super init];
    
    if (self) {
        operand1 = [[Fraction alloc] init];
        operand2 = [[Fraction alloc] init];
        accumulator = [[Fraction alloc] init];
    }
    
    return self;
}

-(void) clear
{
    accumulator.numerator = 0;
    accumulator.denominator = 1;
}

- (Fraction *) performOperation:(char)op
{
    Fraction *result;
    
    switch (op) {
        case '+':
            result = [operand1 add: operand2];
            break;
        case '-':
            result = [operand1 subtract: operand2];
            break;
        case '*':
            result = [operand1 multiply: operand2];
            break;
        case '/':
            result = [operand1 divide: operand2];
            break;
    }
    
    accumulator.numerator = result.numerator;
    accumulator.denominator = result.denominator;
    
    return accumulator;
}

@end


« Last Edit: November 02, 2011, 02:09:09 PM by Fred » Logged

Fred
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.