Amazon.com Widgets Exercise 7.1 written by me
Welcome, Guest. Please login or register.
Did you miss your activation email?
July 28, 2014, 05:33:25 PM
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 7
| | | |-+ Exercise 7.1 written by me
Pages: [1] Go Down
Print
Author Topic: Exercise 7.1 written by me (Read 887 times)
Chuckz
Newbie
*
Posts: 35



Email




on: December 20, 2012, 12:40:22 AM

Not verify still, do not use this code.
I will update the code later soon.

Code: (Objective-C)
//Interface File: Fraction.h

#import <Foundation/Foundation.h>

// Define the Fraction class

@interface Fraction: NSObject

@property int numerator, denominator;

-(void) print;
-(void) setTo: (int) n over: (int) d;
-(double) convertToNum;
-(Fraction *) add: (Fraction *) f;
-(Fraction *) subtract: (Fraction *) f;
-(Fraction *) multiply: (Fraction *) f;
-(Fraction *) divide: (Fraction *) f;
-(void) reduce;
@end

//Implementation File: Fraction.m
#import "Fraction.h"

@implementation Fraction

@synthesize numerator, denominator;

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

// Divide argument from receiver
-(Fraction *) divide: (Fraction *) f;
{
  // To multiply two fractions:
// (a/b) / (c/d) = (a*d) / (b*c)

// result will store the result of the addition
Fraction *result = [[Fraction alloc] init];

result.numerator = numerator * f.denominator;
result.denominator = denominator * f.numerator;

[self reduce];

return result;
}

// Mulitiply argument from receiver
-(Fraction *) multiply: (Fraction *) f;
{
  // To multiply two fractions:
// a/b * c/d = (a*c) / (b*d)

// result will store the result of the addition
Fraction *result = [[Fraction alloc] init];

result.numerator = numerator * f.numerator;
result.denominator = denominator * f.denominator;

[self reduce];

return result;
}

// Subtract argument from receiver
-(Fraction *) subtract: (Fraction *) f;
{
  // To subtract two fractions:
// a/b - c/d = ((a*d) - (b*c)) / (b*d)

// result will store the result of the addition
Fraction *result = [[Fraction alloc] init];

result.numerator = numerator * f.denominator - denominator * f.numerator;
result.denominator = denominator * f.denominator;

[self reduce];

return result;
}

//add a Fraction to the receiver
-(Fraction *) add: (Fraction *) f
{
// To add two fractions:
// a/b + c/d = ((a*d) + (b*c)) / (b*d)

// result will store the result of the addition
Fraction *result = [[Fraction alloc] init];

result.numerator = numerator * f.denominator + denominator * f.numerator;
result.denominator = denominator * f.denominator;

[self reduce];

return result;
}

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

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

numerator /= u;
denominator /= u;
}

@end


// Test File main.m
#import "Fraction.h"

int main (int argc, char * argv[])
{
@autoreleasepool{
Fraction *aFraction = [[Fraction alloc] init];
Fraction *bFraction = [[Fraction alloc] init];

Fraction *resultFractionAdd, *resultFractionSub, *resultFractionMul, *resultFractionDiv;

[aFraction setTo: 1 over: 4];// set 1st fraction to 1/4
[bFraction setTo: 1 over: 2];// set 2nd fraction to 1/2

// Add
[aFraction print];
NSLog (@"+");
[bFraction print];
NSLog (@"=");

resultFractionAdd = [aFraction add: bFraction];
[resultFractionAdd print];

// Subtract
[aFraction print];
NSLog (@"-");
[bFraction print];
NSLog (@"=");

resultFractionSub = [aFraction subtract: bFraction];
[resultFractionSub print];

// Multiply
[aFraction print];
NSLog (@"*");
[bFraction print];
NSLog (@"=");
resultFractionMul = [aFraction multiply: bFraction];
[resultFractionMul print];

// Div
[aFraction print];
NSLog (@"/");
[bFraction print];
NSLog (@"=");
resultFractionMul = [aFraction Div: bFraction];
[resultFractionDiv print];

}

return 0;
}







Logged

I want my life back!
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.