Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
February 22, 2018, 12:01:24 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: 15-8 Simple way  (Read 1261 times)
tadej5553
Full Member
***
Posts: 145


Email
« on: January 06, 2010, 11:52:38 AM »

main.m
Code: (Objective-C)
#import <Foundation/Foundation.h>  
#import "Fraction.h"  
#define LIMIT 10  

@interface Fraction (Random)  
+(Fraction *) randomFraction;  
@end  

@implementation Fraction (Random)  
+(Fraction *) randomFraction  
{  
    Fraction *f = [[Fraction alloc] init];  
    
    [f setTo: arc4random() % LIMIT + 1  
        over: arc4random() % LIMIT + 1];  
    
    return [f reduce];  
}  
@end  

@interface Fraction (Comparison)  
-(NSComparisonResult) compare: (Fraction *) f;  
@end  

@implementation Fraction (Comparison)  
-(NSComparisonResult) compare: (Fraction *) f  
{  
    if ([self convertToNum] > [f convertToNum])  
        return NSOrderedDescending;  
    
    else if ([self convertToNum] < [f convertToNum])  
        return NSOrderedAscending;  
    
    return NSOrderedSame;  
}      
@end    

int main (int argc, char *argv[])  
{  
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];  
    
    NSMutableArray *myArry = [NSMutableArray array];  
    Fraction *temp;  
    int i = 10;  
    
    NSLog(@"First, they are ordered like this:");  
    while (--i) {        
        temp = [Fraction randomFraction];      
        [myArry addObject: temp];        
        [temp print];        
        [temp release];                            
    }  
    
    [myArry sortUsingSelector: @selector (compare:)];  
    
    NSLog(@"\n");  
    NSLog(@"After sorting they are ordered like this:");  
    for (temp in myArry)  
        [temp print];  
    
    [pool drain];  
    return 0;  
}  

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

@interface Fraction : NSObject
{
    int  numerator;
    int  denominator;
}

@property int numerator, denominator;

-(Fraction *) init; //To set the fraction to 0/1 (0, but to work with add:)
-(void) print;
-(double)  convertToNum;
-(void) setTo: (int) num over: (int) denum;
-(Fraction *) reduce;

-(Fraction *) add: (Fraction *) f;

@end  

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

@implementation Fraction

@synthesize numerator, denominator;

-(Fraction *) init
{       
    self = [super init];
   
    if(self)
        [self setTo:0 over:1];
   
    return self;
}

-(void) print
{
    if(denominator < 0)
    {   
        denominator = -denominator;
        numerator = -numerator;
    }
   
    if(!denominator || !numerator)
        NSLog(@"0");
       
    else if(denominator == 1)
        NSLog(@"%i", numerator);
       
    else
    {
        if(numerator < 0)
        {
            if (-numerator > denominator)
                NSLog(@"%i %i/%i", numerator/denominator, -numerator%denominator, denominator);
            else
                NSLog(@"%i/%i", numerator, denominator);
        }
       
        else if (numerator > denominator)
            NSLog(@"%i %i/%i", numerator/denominator, numerator%denominator, denominator);
        else
            NSLog(@"%i/%i", numerator, denominator);
    }
}

-(double) convertToNum
{
    if(denominator)
        return (double) numerator/denominator;

    return 0.0;
}

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

-(Fraction *) reduce
{
    int u = numerator, v = denominator;
    int temp;
   
    while (v) {
        temp = u % v;
        u = v;
        v = temp;
    }
   
    numerator /= u;
    denominator /= u;
    return self;
}

-(Fraction *) add: (Fraction *) f
{
    Fraction *new = [[Fraction alloc] init];
   
    new.numerator = numerator * f.denominator + denominator * f.numerator;
    new.denominator = denominator * f.denominator;
   
    return [new reduce];
}

@end

I hope there are no memory leaks this time Smiley

« Last Edit: May 23, 2010, 12:38:06 PM by tadej5553 » 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.