Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
May 23, 2018, 09:47:04 AM *
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: Exercise 11.2 Solution  (Read 2142 times)
BrianLawson
Sr. Member
****
Posts: 262


Email
« on: December 29, 2013, 03:34:52 PM »

Here is my solution for exercise 11.2:

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
{
Fraction *f1 = [[Fraction alloc] init];
Fraction *f2 = [[Fraction alloc] init];

[f1 setTo: self.numerator over: self.denominator];
[f2 setTo: f.numerator over: f.denominator];
[f1 reduce];
[f2 reduce];

return f1.numerator == f2.numerator && f1.denominator == f2.denominator;
}

// returns -1 if reciever is less than f,
// 0 if receiver is equal to f,
// and 1 if receiver is greater than f.
-(int) compare: (Fraction *) f
{
// convert fractions to floats for comparison
float f1 = (float) self.numerator / self.denominator;
float f2 = (float) f.numerator / f.denominator;

if (f1 < f2)
return -1;
else if (f1 > f2)
return 1;
else
return 0;
}

@end

Fraction.h
Code: (Objective-C)
@interface Fraction : NSObject

@property int numerator, denominator;

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

@end

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

// The Fraction class

@implementation Fraction

@synthesize numerator, denominator;

-(id) init
{
return [self initWith: 0 over: 0];
}

-(id) initWith: (int) n over: (int) d
{
self = [super init];

if (self)
[self setTo: n over: d];

return self;
}

-(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;
}

-(void) reduce
{
int u = numerator;
int v = denominator;
int temp;

while (v != 0) {
temp = u % v;
u = v;
v = temp;
}

numerator /= u;
denominator /= u;

if (denominator < 0)
{
denominator = -denominator;
numerator = - numerator;
}
}

@end

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

NSString * printResults(int r)
{
NSString *returnString = [[NSString alloc] init];

switch (r) {
case -1:
returnString = @"is less than";
break;
case 0:
returnString = @"is equal to";
break;
case 1:
returnString = @"is greater than";
break;
default:
break;
}

return returnString;
}

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

@autoreleasepool {
   
Fraction *a = [[Fraction alloc] init];
Fraction *b = [[Fraction alloc] init];

[a setTo: 1 over: 3];
[b setTo: 2 over: 5];

// test less than comparison
int result = [a compare: b];
NSLog(@"%i/%i %@ %i/%i", a.numerator, a.denominator,
  printResults(result), b.numerator, b.denominator);
   
// test greater than comparison
result = [b compare: a];
NSLog(@"%i/%i %@ %i/%i", b.numerator, b.denominator,
  printResults(result), a.numerator, a.denominator);

// test equality comparison
result = [a compare: a];
NSLog(@"%i/%i %@ %i/%i", a.numerator, a.denominator,
  printResults(result), a.numerator, a.denominator);

if ([a isEqualTo: a])
NSLog(@"They're equal");
else
NSLog(@"They're not equal");

if ([a isEqualTo: b])
NSLog(@"They're equal");
else
NSLog(@"They're not equal");
}

    return 0;
}
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.