Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
October 17, 2018, 08:07:47 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 
   Home   Help Search Login Register Chat  
Pages: [1]   Go Down
  Print  
Author Topic: Chapter 11 - Exercise 1 Solution  (Read 3760 times)
bertobettencourt
Newbie
*
Posts: 45


Email
« on: October 27, 2013, 09:32:57 PM »

Fraction.h

Code: (Objective-C)
#import <Foundation/Foundation.h>

@interface Fraction : NSObject

@property int numerator, denominator;

-(void) setTo: (int) n over: (int) d;
-(void) reduce;
-(double) convertToNum;
-(void) print;

@end

Fraction.m

Code: (Objective-C)
#import "Fraction.h"

@implementation Fraction
@synthesize numerator, denominator;

-(void) setTo: (int) n over: (int) d{
    numerator = n;
    denominator = d;
}

-(void) reduce{
   
    int u = numerator;
    int v = denominator;
    int temp;
   
    while (v != 0){
        temp = u % v;
        u = v;
        v = temp;
    }
   
    numerator /= u;
    denominator /=u;
}
-(double) convertToNum{
    if (denominator != 0)
        return (int) numerator / denominator;
    else
        return NAN;
}

-(void) print{
   
    NSLog(@"%i/%i", numerator, denominator);
}

@end

Fraction+MathOps.h

Code: (Objective-C)
#import "Fraction.h"

@interface Fraction (MathOps)

-(Fraction *) add: (Fraction *) f;
-(Fraction *) mul: (Fraction *) f;
-(Fraction *) sub: (Fraction *) f;
-(Fraction *) div: (Fraction *) f;
-(void) reverse;

@end


Fraction+MathOps.m

Code: (Objective-C)
#import "Fraction+MathOps.h"

@implementation Fraction (MathOps)

-(void) reverse{
   
    int x = self.numerator;
    int y = self.denominator;
    if (x == 0){
        x = 1;}
   
    self.numerator = y;
    self.denominator = x;
}

-(Fraction *) add:(Fraction *)f{
    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{
    Fraction *result = [[Fraction alloc]init];
   
    result.numerator = self.numerator * 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.denominator) + (self.denominator * 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

main.m

Code: (Objective-C)
#import <Foundation/Foundation.h>
#import "Fraction.h"
#import "Fraction+MathOps.h"

int main(int argc, const char * argv[])
{

    @autoreleasepool {
       
        Fraction *a = [[Fraction alloc]init];
        Fraction *b = [[Fraction alloc]init];
        Fraction *result;
       
       
        [a setTo:1 over:3];
        [b setTo:3 over:5];
       
        [a print]; NSLog(@"  +");[b print]; NSLog(@"----");
        result = [a add:b];
        [result print];
        NSLog(@"inverse");
        [result reverse];
        [result print];
       
        NSLog(@"\n");
       
        [a print]; NSLog(@"  -");[b print]; NSLog(@"----");
        result = [a sub:b];
        [result print];
        NSLog(@"inverse");
        [result reverse];
        [result print];
       
        NSLog(@"\n");
       
        [a print]; NSLog(@"  *");[b print]; NSLog(@"----");
        result = [a mul:b];
        [result print];
        NSLog(@"inverse");
        [result reverse];
        [result print];
       
        NSLog(@"\n");

        [a print]; NSLog(@"  /");[b print]; NSLog(@"----");
        result = [a div:b];
        [result print];
        NSLog(@"inverse");
        [result reverse];
        [result print];
       
        NSLog(@"\n");

    }
    return 0;
}


Cheers.
Logged
Noomoviemafie
Newbie
*
Posts: 1


Email
« Reply #1 on: November 01, 2014, 12:44:41 AM »

Well you know what they say about this community.
Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

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 ゥ 2009 classroomM.com. All rights reserved.