TA的每日心情 | 汗 2024-10-15 10:05 |
---|
签到天数: 372 天 [LV.9]以坛为家II
|
Fraction.h - #import <Foundation/NSObject.h>
-
- @interface Fraction: NSObject
- {
- int numerator;
- int denominator;
- }
-
- -(void) print;
- -(void) setNumerator: (int) n;
- -(void) setDenominator: (int) d;
- -(int) numerator;
- -(int) denominator;
- -(void) setNumerator: (int) n andDenominator: (int) d;
- //****************************************************
- -(Fraction*) initWithNumerator:(int)n denominator: (int) d;
- //****************************************************
- @end
复制代码
Fraction.m- #import "Fraction.h"
- #import <stdio.h>
- @implementation Fraction
- -(void) print
- {
- printf("%i/%i",numerator,denominator);
- }
-
- -(void) setNumerator: (int) n
- {
- numerator = n;
- }
- -(void) setDenominator: (int) d
- {
- denominator = d;
- }
-
- -(int) denominator
- {
- return denominator;
- }
- -(int) numerator
- {
- return numerator;
- }
-
- -(void) setNumerator: (int) n andDenominator: (int) d
- {
- numerator = n;
- denominator = d;
- }
- //**********************************************************
- -(Fraction*) initWithNumerator: (int) n denominator: (int) d
- {
- self = [super init];
- if ( self ) {
- [self setNumerator: n andDenominator: d];
- }
- return self;
- }
- //**********************************************************
- @end
复制代码
main.m- #import <stdio.h>
- #import "Fraction.h"
- int main( int argc, const char *argv[] )
- {
- Fraction *frac = [[Fraction alloc] init];
- [frac setNumerator: 1];
- [frac setDenominator: 3];
- printf( "The fraction is: " );
- [frac print];
- [frac release];
- Fraction *frac2 = [[Fraction alloc] init];
- [frac2 setNumerator: 1 andDenominator: 5];
- printf( "Fraction 2 is: " );
- [frac2 print];
- [frac2 release];
- //***************************************************
- Fraction *frac3 = [[Fraction alloc] initWithNumerator: 3 denominator: 10];
- printf( "Fraction 3 is: " );
- [frac3 print];
- [frac3 release];
- //***************************************************
- return 0;
- }
复制代码
|
|