第一题 定义分数(Fraction)类:
1、成员变量:分子、分母
2、方法:
(1)自定义初始化方法(初始分子和分母)
(2)分子的赋值、取值方法
(3)分母的赋值取值方法
(4)打印分数信息
(5)约分
(6)加、减、乘、除运算方法,返回分数对象。
3、mian.m文件中创建分数对象,测试加、减、乘、除。
提示:加减乘除运算有一个Fraction类型的参数(参与加法运算的另外一个分数对象),有一个Fraction类型的返回值(结果)。
Fraction.h
#import <Foundation/Foundation.h> @interface Fraction : NSObject { int _numerator; // 分子 int _denominator; // 分母 } /// 分子赋值 - (void)setNumerator:(int)numerator; /// 分母赋值 - (void)setDenominator:(int)denominator; /// 分子取值 - (int)numerator; /// 分母取值 - (int)denominator; /// 打印 - (void)print; /// 初始化 - (id)initWithNumerator :(int)numerator denominator:(int)denominator; /// 约分 - (void)reduce; /// 加法 - (Fraction *)add:(Fraction *)f; /// 减法 - (Fraction *)sub:(Fraction *)f; /// 乘法 - (Fraction *)mul:(Fraction *)f; /// 除法 - (Fraction *)div:(Fraction *)f; @end
#import "Fraction.h" @implementation Fraction /// 初始化 - (id)initWithNumerator:(int)numerator denominator:(int)denominator { _numerator = numerator; _denominator = denominator; return self; } /// 分子赋值 - (void)setNumerator:(int)numerator { _numerator = numerator; } /// 分母赋值 - (void)setDenominator:(int)denominator { _denominator = denominator; } /// 分子取值 - (int)numerator { return _numerator; } /// 分母取值 - (int)denominator { return _denominator; } /// 打印 - (void)print { NSLog(@"分数是%d / %d", _numerator, _denominator); } /// 约分 - (void)reduce { int a = self->_numerator , b = self->_denominator; int temp; while (a % b != 0) { temp = a % b; a = b; b = temp; } _denominator /= b; _numerator /= b; } /// 加法 - (Fraction *)add:(Fraction *)f { Fraction *result = [[Fraction alloc]init]; result->_numerator = self->_numerator * f->_denominator + self->_denominator * f->_numerator; result->_denominator = self->_denominator * f->_denominator; return result; } ///减法 - (Fraction *)sub:(Fraction *)f { Fraction *result = [[Fraction alloc]init]; result->_numerator = self->_numerator * f->_denominator - self->_denominator * f->_numerator; result->_denominator = self->_denominator * f->_denominator; return result; } /// 乘法 - (Fraction *)mul:(Fraction *)f { Fraction *result = [[Fraction alloc]init]; result->_numerator = self->_numerator * f->_numerator; result->_denominator = self->_denominator * f->_denominator; return result; } /// 除法 - (Fraction *)div:(Fraction *)f { Fraction *result = [[Fraction alloc]init]; result->_numerator = self->_numerator * f->_denominator ; result->_denominator = self->_denominator * f->_numerator; return result; } @end
#import <Foundation/Foundation.h> #import "Fraction.h" int main(int argc, const char * argv[]) { @autoreleasepool { Fraction *f1 = [[Fraction alloc]initWithNumerator:2 denominator:4]; [f1 reduce]; [f1 print]; Fraction *f2 = [[Fraction alloc]initWithNumerator:3 denominator:8]; [f1 reduce]; [f2 print]; /// 加法 Fraction *result = [f1 add:f2]; [result reduce]; [result print]; // 减法 result = [f1 sub:f2]; [result reduce]; [result print]; // 乘法 result = [f1 mul:f2]; [result reduce]; [result print]; // 除法 result = [f1 div:f2]; [result reduce]; [result print]; } return 0; }