Not verify still, do not use this code.
I will update the code later soon.
//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;
}