Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
October 19, 2017, 09:40:09 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 7.2  (Read 4462 times)
catonkeyboardjsjsjsj
Newbie
*
Posts: 14


« on: January 13, 2014, 06:57:42 PM »

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

@interface Fraction : NSObject

@property int numerator, denominator;

// Convert to a real number
-(double) convertToNum;

// Print
-(void) print: (BOOL) toReduceOrNotToReduce;

// Set numerator and denominator
-(void) setTo: (int) n over: (int) d;

// Arithmetic operations methods
-(Fraction *) add: (Fraction *) f;
-(Fraction *) subtract: (Fraction *) f;
-(Fraction *) multiple: (Fraction *) f;
-(Fraction *) divide: (Fraction *) f;

// Reduce the fraction
-(void) reduce;

@end

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

@implementation Fraction

@synthesize numerator, denominator;

-(void) print: (BOOL) toReduceOrNotToReduce {
    if (numerator % denominator == 0) {
        numerator = numerator / denominator;
        NSLog(@"%i", numerator);
    } else if (toReduceOrNotToReduce) {
        Fraction *reducedFraction = [[Fraction alloc] init];
        [reducedFraction setTo:numerator over:denominator];
        [reducedFraction reduce];
        NSLog(@"%i/%i", reducedFraction.numerator, reducedFraction.denominator);
    } else
        NSLog(@"%i/%i", numerator, denominator);
}



-(double) convertToNum {
    if (denominator != 0) {
        return (double) numerator / denominator;
    } else
        return NAN;
}



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



// Add a fraction to the receiver
-(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;
}



// Subtract a fraction from the receiver
-(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;
}



// Multiple receiver by a fraction
-(Fraction *) multiple:(Fraction *)f {
    
    Fraction *result = [[Fraction alloc] init];
    result.numerator = (numerator * f.numerator);
    result.denominator = (denominator * f.denominator);
    [result reduce];
    
    return result;
}



// Divide receiver by a fraction
-(Fraction *) divide:(Fraction *)f {
    
    Fraction *result = [[Fraction alloc] init];
    result.numerator = (numerator * f.denominator);
    result.denominator = (denominator * f.numerator);
    [result reduce];
    
    return result;
}



// Reduce the fraction
-(void) reduce {
    int u = numerator, v = denominator, temp;
    
    while (v !=0) {
        temp = u % v;
//        NSLog(@"%i", temp);
        u = v;
        v = temp;
    }
    
    numerator /= u;
    denominator /= u;
}

@end

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

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

    @autoreleasepool {
        
        Fraction *aFraction = [[Fraction alloc] init];
        Fraction *bFraction = [[Fraction alloc] init];
        Fraction *resultFraction = [[Fraction alloc] init];
        
        [aFraction setTo:1 over:2];
        [bFraction setTo:1 over:4];
        
        NSLog(@"Add Fractions:");
        [aFraction print:NO];
        NSLog(@"+");
        [bFraction print:NO];
        NSLog(@"=");
        
        resultFraction = [aFraction add:bFraction];
        [resultFraction print:YES];
        
        
        NSLog(@"---------------------");
        
        
        NSLog(@"Subtract Fractions:");
        [aFraction print:NO];
        NSLog(@"-");
        [bFraction print:NO];
        NSLog(@"=");
        
        resultFraction = [aFraction subtract:bFraction];
        [resultFraction print:YES];
        
        
        NSLog(@"---------------------");
        
        NSLog(@"Multiply Fractions:");
        [aFraction print:NO];
        NSLog(@"x");
        [bFraction print:NO];
        NSLog(@"=");
        
        resultFraction = [aFraction multiple:bFraction];
        [resultFraction print:YES];
        
        
        NSLog(@"---------------------");
        
        
        NSLog(@"Divide Fractions:");
        [aFraction print:NO];
        NSLog(@"/");
        [bFraction print:NO];
        NSLog(@"=");
        
        resultFraction = [aFraction divide:bFraction];
        [resultFraction print:YES];
        NSLog(@"---------------------");
        
        
    }
    return 0;
}
Logged
Jackiepie
Newbie
*
Posts: 8


Email
« Reply #1 on: May 21, 2015, 12:30:37 AM »

This is my code modified from your  code. I think it would be better thinking of every possibility.

Code: (Objective-C)
-(void) print: (BOOL) isRedu;
{
    if (denominator == 0)                                  // when denominator = 0,  it's not a number
        NSLog(@"NAN");
    else if (numerator % denominator == 0)                 // somehow it  means denominator = 1
        NSLog(@"%i", numerator = numerator / denominator);
    else if (numerator == 0)                               // when numerator = 0, the fraction =  0
        NSLog(@"0");
    else if (isRedu)
    {
        Fraction *reduFrac = [[Fraction alloc] init];
        [reduFrac setTo: numerator over: denominator];
        [reduFrac reduce];
        NSLog(@"%i/%i", reduFrac.numerator, reduFrac.denominator);
       
    }
    else
        NSLog(@"%i/%i", numerator, denominator);
}
Logged
cary_W
Newbie
*
Posts: 10


Email
« Reply #2 on: May 06, 2016, 06:31:01 PM »

You can use the [self reduce] in the print: method, that would be more concise and easy to read. Smiley
Logged
cary_W
Newbie
*
Posts: 10


Email
« Reply #3 on: May 06, 2016, 06:34:48 PM »

Code: (Objective-C)
-(void) print: (BOOL) whether_to_reduce
{
    if (whether_to_reduce)
    {
        [self reduce];
    }
    NSLog (@"%i/%i", numerator, denominator);
}
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.