I am very new to Objective-C as well as objected oriented programming, and in a book I am studying from there is an exercise in which I was supposed to create a class called Rational
, that has hidden data members called numerator and denominator, and methods to add, multiply, subtract and divide the Rational objects (the objects are just fractions) together. For some reason when I run the program it becomes extremely slow when calculating. I am using ARC on X-Code, and I am wondering if it has to do with memory management issues.
Here is the .m file of the class:
#import "Rational.h"
@interface Rational (privateMethods)
-(int) gcd:(int) a: (int) b;
-(Rational*) simplifyFraction:(Rational*)fraction;
@end
@implementation Rational
@synthesize numerator, denominator;
-(Rational*) multiplyFraction:(Rational *)fraction1 :(Rational *)fraction2{
fraction1.numerator = fraction1.numerator * fraction2.numerator;
fraction1.denominator = fraction1.denominator * fraction2.denominator;
fraction1 = [self simplifyFraction:fraction1];
return fraction1;
}
-(Rational*) addFraction:(Rational *)fraction1 :(Rational *)fraction2{
Rational * returnFraction = [[Rational alloc] init];
fraction1.numerator = fraction1.numerator*fraction2.denominator;
fraction2.numerator = fraction2.numerator *fraction1.denominator;
fraction1.denominator = fraction1.denominator*fraction2.denominator;
fraction2.denominator = fraction1.denominator;
returnFraction.numerator = fraction1.numerator + fraction2.numerator;
returnFraction.denominator = fraction1.denominator;
returnFraction = [self simplifyFraction:returnFraction];
return returnFraction;
}
-(Rational*) subtractFraction:(Rational *)fraction1 :(Rational *)fraction2{
Rational * returnFraction = [[Rational alloc] init];
fraction1.numerator = fraction1.numerator*fraction2.denominator;
fraction2.numerator = fraction2.numerator *fraction1.denominator;
fraction1.denominator = fraction1.denominator*fraction2.denominator;
fraction2.denominator = fraction1.denominator;
returnFraction.numerator = fraction1.numerator - fraction2.numerator;
returnFraction.denominator = fraction1.denominator;
returnFraction = [self simplifyFraction:returnFraction];
return returnFraction;
}
-(Rational*) divideFraction:(Rational *)fraction1 :(Rational *)fraction2{
Rational * returnFraction = [[Rational alloc] init];
const int temp = fraction2.denominator;
fraction2.denominator = fraction2.numerator;
fraction2.numerator = temp;
returnFraction.numerator = fraction1.numerator * fraction2.numerator;
returnFraction.denominator = fraction2.denominator * fraction1.denominator;
returnFraction = [self simplifyFraction:returnFraction];
return returnFraction;
}
-(void) printObject:(Rational *)fraction{
printf("%i/%i",fraction.numerator, fraction.denominator);
printf("\n");
}
-(void) printRoundedFloat:(Rational *)fraction{
float number = (float)fraction.numerator/fraction.denominator;
printf("%f", number);
printf("\n");
}
-(int)gcd:(int)a :(int)b{
if (b==0) {
return a;
}
else
return [self gcd:b :a%b];
}
-(Rational*) simplifyFraction:(Rational *)fraction{
if (fraction.denominator == 0) {
NSLog(@"ERROR: YOU CAN NOT HAVE ZERO IN THE DENOMINATOR");
}
else{
int i = fraction.numerator > fraction.denominator ? fraction.numerator:fraction.denominator;
while (i>1) {
if (fraction.numerator % i == 0 && fraction.denominator%i==0) {
fraction.numerator/=i;
fraction.denominator/=i;
}
--i;
}
}
return fraction;
}
-(void) dealloc{
}
@end
Here is the Main.m file of the program:
import <Foundation/Foundation.h>
import "Rational.h"
int main (int argc, const char * argv[])
{
@autoreleasepool {
Rational * newFraction = [[Rational alloc] init];
Rational * otherFraction = [[Rational alloc] init];
newFraction.numerator = 1;
newFraction.denominator = 25;
printf("Fraction 1 is: ");
[newFraction printObject:newFraction];
otherFraction.numerator = 1;
otherFraction.denominator = 5;
printf("Fraction 2 is: ");
[otherFraction printObject:otherFraction];
printf("\nThe Fractions Added togeher are: ");
id number = [newFraction addFraction:newFraction :otherFraction];
[number printObject:number];
printf("Rounded: ");
[number printRoundedFloat:number];
printf("\nThe Fractions subtracted are: ");
number = [number subtractFraction:newFraction :otherFraction];
[number printObject:number];
printf("Rounded: ");
[number printRoundedFloat:number];
printf("\nThe Fractions multiplied are: ");
number = [number multiplyFraction:newFraction :otherFraction];
[number printObject:number];
printf("Rounded: ");
[number printRoundedFloat:number];
printf("\nThe Fractions divided are: ");
number = [number divideFraction:newFraction :otherFraction];
[number printObject:number];
printf("Rounded: ");
[number printRoundedFloat:number];
}
return 0;