Amazon.com Widgets Chapter 11 Program 1
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 25, 2013, 12:55:37 PM
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
| |-+  Chapter 11
| | |-+  Chapter 11 Program 1
Pages: [1]   Go Down
Print
Author Topic: Chapter 11 Program 1  (Read 772 times)
clouded
Full Member
***
Posts: 123






« on: June 26, 2012, 06:29:10 AM »

Fully separated for easy clarification, in the book the Class is together in the same file... either way will work.
Code: (Objective-C)
//
//  main.m
//  Chapter 11 Program 1

#import "Fraction.h"
#import "Fraction+MathOps.h"

int main (int argc, char * argv[])
{    
    @autoreleasepool {
        Fraction *a = [[Fraction alloc] init];
        Fraction *b = [[Fraction alloc] init];
        Fraction *result;
        
        [a setTo: 1 over: 3];
        [b setTo: 2 over: 5];
        
        [a print]; NSLog (@" +"); [b print]; NSLog (@"-----");
        result = [a add: b];
        [result print];
        NSLog (@"\n");
        
        [a print]; NSLog (@" -"); [b print]; NSLog (@"-----");
        result = [a sub: b];
        [result print];
        NSLog (@"\n");
        
        [a print]; NSLog (@" *"); [b print]; NSLog (@"-----");
        result = [a mul: b];
        [result print];
        NSLog (@"\n");
        
        [a print]; NSLog (@" /"); [b print]; NSLog (@"-----");
        result = [a div: b];
        [result print];
        NSLog (@"\n");
    }
    return 0;
}
Code: (Objective-C)
//
//  Fraction+MathOps.h
//  Chapter 11 Program 1

#import "Fraction.h"

@interface Fraction (MathOps)
-(Fraction *) add: (Fraction *) f;
-(Fraction *) mul: (Fraction *) f;
-(Fraction *) sub: (Fraction *) f;
-(Fraction *) div: (Fraction *) f;
@end
Code: (Objective-C)
//
//  Fraction+MathOps.m
//  Chapter 11 Program 1

#import "Fraction+MathOps.h"

@implementation Fraction (MathOps)
-(Fraction *) add: (Fraction *) f
{
    // To add two fractions:
    // a/b + c/d = ((a*d) + (b*c)) / (b * d)
    Fraction *result = [[Fraction alloc] init];
    result.numerator = (self.numerator * f.denominator) + (self.denominator * f.numerator);
    result.denominator = self.denominator * f.denominator;
    [result reduce];
    return result;
}
-(Fraction *) sub: (Fraction *) f
{
    // To sub two fractions:
    // a/b - c/d = ((a*d) - (b*c)) / (b * d)
    Fraction *result = [[Fraction alloc] init];
    result.numerator = (self.numerator * f.denominator) - (self.denominator * f.numerator);
    result.denominator = self.denominator * f.denominator;
    [result reduce];
    return result;
}
-(Fraction *) mul: (Fraction *) f
{
    Fraction *result = [[Fraction alloc] init];
    result.numerator = self.numerator * f.numerator;
    result.denominator = self.denominator * f.denominator;
    [result reduce];
    return result;
}
-(Fraction *) div: (Fraction *) f
{
    Fraction *result = [[Fraction alloc] init];
    result.numerator = self.numerator * f.denominator;
    result.denominator = self.denominator * f.numerator;
    [result reduce];
    return result;
}
@end
Code: (Objective-C)
//
//  Fraction.h
//  Chapter 11 Program 1

@interface Fraction : NSObject
@property int numerator, denominator;
-(NSString *) description;
-(void) print;
-(double) convertToNum;
-(void) setTo: (int) n over: (int) d;
-(Fraction *) add: (Fraction *) f;
-(Fraction *) subtract: (Fraction *) f;
-(Fraction *) multiply: (Fraction *) f;
-(Fraction *) divide: (Fraction *) f;
-(void) reduce;
@end
Code: (Objective-C)
//
//  Fraction.m
//  Chapter 11 Program 1

#import "Fraction.h"

@implementation Fraction  
@synthesize numerator, denominator;  
-(NSString *) description
{
    return [NSString stringWithFormat: @"%i/%i", numerator, denominator];
}
-(void) print  
{  
    NSLog (@"%i/%i", numerator, denominator);  
}
-(double) convertToNum  
{  
    if (denominator != 0)  
        return (double) numerator / denominator;  
    else  
        return NAN;  
}  
-(void) setTo: (int) n over: (int) d  
{  
    numerator = n;  
    denominator = d;  
}  
-(Fraction *) add: (Fraction *) f  
{  
    Fraction *result = [[Fraction alloc] init];  
    result.numerator = numerator * f.denominator + denominator * f.numerator;  
    result.denominator = denominator * f.denominator;  
    
    [result reduce];  
    return result;  
}  
-(Fraction *) subtract: (Fraction *) f  
{  
    Fraction *result = [[Fraction alloc] init];  
    result.numerator = numerator * f.denominator - denominator * f.numerator;  
    result.denominator = denominator * f.denominator;  
    
    [result reduce];  
    return result;  
}  
-(Fraction *) multiply: (Fraction *) f  
{  
    Fraction *result = [[Fraction alloc] init];  
    result.numerator = numerator * f.numerator;  
    result.denominator = denominator * f.denominator;  
    
    [result reduce];  
    return result;  
}  
-(Fraction *) divide: (Fraction *) f  
{  
    Fraction *result = [[Fraction alloc] init];  
    result.numerator = numerator * f.denominator;  
    result.denominator = denominator * f.numerator;  
    
    [result reduce];  
    return result;  
}  
-(void) reduce  
{  
    int u = numerator;  
    int v = denominator;  
    int temp;  
    while (v != 0) {  
        temp = u % v;  
        u = v;  
        v = temp;  
    }  
    numerator /= u;  
    denominator /= u;  
}
@end
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.