Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
August 18, 2018, 09:26:48 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: Exercises 1 and 2  (Read 1288 times)
kotyara
Newbie
*
Posts: 15


« on: December 14, 2014, 08:24:27 AM »

//  Fraction.h

#import <Foundation/Foundation.h>

@interface Fraction : NSObject
@property int numerator, denominator;
-(void) print;
-(void) setTo: (int) n over: (int) d;
-(double) convertToNum;
-(void) reduce;
@end

//  Fraction.m

#import "Fraction.h"

@implementation Fraction
@synthesize numerator, denominator;
-(void)print{
    NSLog(@"%i/%i", numerator, denominator);
}
-(void)setTo:(int)n over:(int)d{
    numerator = n;
    denominator = d;
}
-(double)convertToNum{
    if (denominator != 0) {
        return (double) numerator / denominator;
    } else {
        NSLog(@"Error - division by zero");
        return NAN;
    }
}
-(void)reduce{
    int u = numerator;
    int v = denominator;
    int temp;
    while (v != 0) {
        temp = u % v;
        u = v;
        v = temp;
    }
    numerator = numerator / u;
    denominator = denominator / u;
}
@end

//  Fraction+MathOps.h

#import "Fraction.h"

@interface Fraction (MathOps)
-(Fraction *) add: (Fraction *) f;
-(Fraction *) subtract: (Fraction *) f;
-(Fraction *) multiply: (Fraction *) f;
-(Fraction *) divide: (Fraction *) f;
-(Fraction *) invert;
@end

//  Fraction+MathOps.m
//  q01
//
//  Created by Константин Яворский on 14.12.14.
//  Copyright (c) 2014 Konstantin Yavorskiy. All rights reserved.
//

#import "Fraction+MathOps.h"

@implementation Fraction (MathOps)

-(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 *)subtract:(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 *)multiply:(Fraction *)f{
    Fraction * result = [[Fraction alloc]init];
    result.numerator = self.numerator * f.numerator;
    result.denominator = self.denominator * f.denominator;
    [result reduce];
    return  result;
}
-(Fraction *)divide:(Fraction *)f{
    Fraction * result = [[Fraction alloc]init];
    result.numerator = self.numerator * f.denominator;
    result.denominator = self.denominator * f.numerator;
    [result reduce];
    return result;
}
-(Fraction *)invert{
    Fraction * result = [[Fraction alloc]init];
    result.numerator = self.denominator;
    result.denominator = self.numerator;
    return result;
}
@end

//  Fraction+Comparison.h

#import "Fraction.h"

@interface Fraction (Comparison)
-(BOOL) isEqualTo: (Fraction *) f;
-(int) compare: (Fraction *) f;
@end

//  Fraction+Comparison.m

#import "Fraction+Comparison.h"
#import "Fraction+MathOps.h"

@implementation Fraction (Comparison)

-(BOOL)isEqualTo:(Fraction *)f{
    BOOL equality;
    Fraction * result;
    result = [self subtract:f];
    if (result.numerator == 0) {
        equality = YES;
        return equality;
    } else {
        equality = NO;
        return equality;
    }
}
-(int)compare:(Fraction *)f{
    double result;
    result = [[self subtract:f] convertToNum];
    if (result < 0.0){
        return -1;}
    else if (result == 0.0){
        return 0;}
    else{
        return 1;}
}
@end

//  main.m

#import <Foundation/Foundation.h>
#import "Fraction.h"
#import "Fraction+MathOps.h"
#import "Fraction+Comparison.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        Fraction * a = [[Fraction alloc]init];
        [a setTo:3 over:4];
        Fraction * b = [[Fraction alloc]init];
        [b setTo:6 over:8];
        if ([a isEqualTo:b]) {
            NSLog(@"Fractions a and b are equal");
        } else {
            NSLog(@"Fractions a and b are not equal");
        }
        int i;
        i = [a compare:b];
        NSLog(@"%i", i);
    }
    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.