Last active
December 24, 2015 22:49
-
-
Save jchudzynski/6875220 to your computer and use it in GitHub Desktop.
Example of Objective-C Class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import <Foundation/Foundation.h> | |
| #import "MFFraction.h" | |
| @interface MFUtilities : NSObject | |
| { | |
| int count; | |
| } | |
| //main intro | |
| -(double)getValueOfFraction:(MFFraction *)fraction; | |
| -(BOOL)isEqual:(MFFraction *)fractionOne and:(MFFraction *)object; | |
| -(NSComparisonResult)compare:(MFFraction *)fractionOne and:(MFFraction *)otherObject; | |
| @end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // MFUtilities.m | |
| /* | |
| Author: Janusz Chudzynski | |
| */ | |
| #import "MFUtilities.h" | |
| //MFUtilities Extension | |
| @interface MFUtilities() | |
| //private methods and objects | |
| @end | |
| @implementation MFUtilities | |
| -(double)getValueOfFraction:(MFFraction *)fraction;{ | |
| return fraction.numerator.intValue*1.0/fraction.denominator.intValue*1.0; | |
| } | |
| - (NSComparisonResult)compare:(MFFraction *)fractionOne and:(MFFraction *)otherObject { | |
| float a = fractionOne.numerator.intValue/fractionOne.denominator.intValue; | |
| float b = otherObject.numerator.intValue/otherObject.denominator.intValue; | |
| if(a==b) | |
| { | |
| return NSOrderedSame; | |
| } | |
| if(a<b){ | |
| return NSOrderedAscending; | |
| } | |
| return NSOrderedDescending; | |
| } | |
| - (BOOL)isEqual:(MFFraction *)fractionOne and:(MFFraction *)object | |
| { | |
| MFFraction * other = (MFFraction *)object; | |
| fractionOne = [self simplify:fractionOne]; | |
| other = [self simplify:other]; | |
| return fractionOne.numerator.intValue == other.numerator.intValue && fractionOne.denominator.intValue == other.denominator.intValue; | |
| } | |
| -(MFFraction *)simplify:(MFFraction *)_fraction{ | |
| int a = _fraction.numerator.intValue; | |
| int b = _fraction.denominator.intValue; | |
| long gcm = GCD(a, b); | |
| a = a/gcm; | |
| b= b/gcm; | |
| _fraction.numerator = [NSNumber numberWithInt:a]; | |
| _fraction.denominator = [NSNumber numberWithInt:b]; | |
| return _fraction; | |
| } | |
| int GCD(int a, int b){ | |
| if (b==0) return a; | |
| return GCD(b,a%b); | |
| } | |
| @end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment