Amazon.com Widgets Ex. 11.2: the shortest comparison implementation ever!
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 20, 2013, 07:24:32 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 11
| | | |-+  Ex. 11.2: the shortest comparison implementation ever!
Pages: [1]   Go Down
Print
Author Topic: Ex. 11.2: the shortest comparison implementation ever!  (Read 441 times)
Jemcik
Newbie
*
Posts: 22



Email




« on: November 23, 2011, 10:08:29 AM »

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

@interface Fraction (Comparison)

-(BOOL) isEqualTo: (Fraction *) f;
-(int) compare: (Fraction *) f;

@end

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

@implementation Fraction (Comparison)

-(BOOL) isEqualTo: (Fraction *) f
{
    return ![self compare:f] ? TRUE : FALSE;
}

-(int) compare: (Fraction *) f
{
    int a = num * f.den;
    int b = f.num * den;
    return a == b ? 0 : a > b ? 1 : -1;
}

@end

Pay attention that receiver and argument remains unchanged in both methods,
and everything is done in -(int) compare: (Fraction *) f actually Smiley
« Last Edit: November 23, 2011, 10:11:58 AM by Jemcik » 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.