首页 > 代码库 > Objective-C 程序设计(第六版)第十一章习题答案
Objective-C 程序设计(第六版)第十一章习题答案
1.
1 #import "Fraction.h" 2 3 @interface Fraction (MathOps) 4 5 - (Fraction *) add: (Fraction *) f; 6 7 - (Fraction *) mul: (Fraction *) f; 8 9 - (Fraction *) sub: (Fraction *) f;10 11 - (Fraction *) div: (Fraction *) f;12 13 - (Fraction *) invert; //倒数14 15 @end
1 #import "Fraction+MathOps.h" 2 3 @implementation Fraction (MathOps) 4 5 - (Fraction *) add: (Fraction *) f 6 { 7 Fraction *result = [[Fraction alloc] init]; 8 9 result.numerator = self.numerator * f.denominator + self.denominator * f.numerator;10 result.denominator = self.denominator * f.denominator;11 12 [result reduce];13 return result;14 15 }16 17 - (Fraction *) mul: (Fraction *) f18 {19 Fraction *result = [[Fraction alloc] init];20 21 result.numerator = self.numerator * f.denominator - self.denominator * f.numerator;22 result.denominator = self.denominator * f.denominator;23 24 [result reduce];25 return result;26 27 }28 29 - (Fraction *) sub: (Fraction *) f30 {31 Fraction *result = [[Fraction alloc] init];32 33 result.numerator = self.numerator *f.numerator;34 result.denominator = self.denominator *f.denominator;35 36 [result reduce];37 return result;38 39 }40 41 - (Fraction *) div: (Fraction *) f42 {43 Fraction *result = [[Fraction alloc] init];44 45 result.numerator = self.numerator *f.denominator;46 result.denominator = self.denominator *f.numerator;47 48 [result reduce];49 return result;50 }51 52 - (Fraction *) invert53 {54 Fraction *result = [[Fraction alloc] init];55 56 result.numerator = self.denominator;57 result.denominator = self.numerator;58 59 [result reduce];60 return result;61 }62 63 64 @end
1 Fraction *f1 = [[Fraction alloc] init];2 Fraction *f2 = [[Fraction alloc] init];3 Fraction *result;4 5 [f1 setTo:1 over:5];6 [f2 setTo:1 over:6];7 result = [f1 add: f2];8 [[result invert] print];9
2.
1 #import "Fraction.h"2 3 @interface Fraction (Comparison)4 5 - (BOOL) isEqualTo: (Fraction *) f;6 7 - (int) compare: (Fraction *) f;8 9 @end
#import "Fraction+Comparison.h"@implementation Fraction (Comparison)- (BOOL) isEqualTo: (Fraction *) f{
if (self.numerator * f.denominator == f.numerator * self.denominator) { return YES; }else return NO;}- (int) compare: (Fraction *) f{
if (self.numerator * f.denominator == f.numerator * self.denominator) { return 0; }else if (self.numerator * f.denominator > f.numerator * self.denominator) { return 1; }else return -1; }@end
1 Fraction *f1 = [[Fraction alloc] init]; 2 Fraction *f2 = [[Fraction alloc] init]; 3 Fraction *result; 4 5 [f1 setTo: 3 over: 7]; 6 [f2 setTo: 1 over: 2]; 7 result = [f1 add: f2]; 8 [[result invert] print]; 9 10 if ([f1 isEqualTo: f2] == YES) {11 NSLog(@"两个分数相等");12 }else13 NSLog(@"两个分数不相等");14 15 if ([f1 compare: f2] == 0) {16 NSLog(@"%d/%d 等于 %d/%d", f1.numerator, f1.denominator,f2.numerator,f2.denominator);17 }else if ([f1 compare:f2] == 1){18 NSLog(@"%d/%d 大于 %d/%d", f1.numerator, f1.denominator,f2.numerator,f2.denominator);19 }else20 NSLog(@"%d/%d 小于 %d/%d", f1.numerator, f1.denominator,f2.numerator,f2.denominator);21 22
3.
1 @implementation Fraction (NSComparisonMethods) 2 3 - (BOOL) isEqualTo:(id)object 4 { 5 if ( (self.numerator * [object denominator] ) == ([object numerator] * self.denominator )) { 6 return YES; 7 }else 8 return NO; 9 }10 11 - (BOOL) isLessThanOrEqualTo:(id)object12 {13 if ( (self.numerator * [object denominator]) <= ([object numerator] * self.denominator )) {14 return YES;15 }else16 return NO;17 }18 19 - (BOOL) isLessThan:(id)object20 {21 if ( (self.numerator * [object denominator]) < ([object numerator] * self.denominator )) {22 return YES;23 }else24 return NO;25 }26 27 - (BOOL) isGreaterThanOrEqualTo:(id)object28 {29 if ( (self.numerator * [object denominator]) >= ([object numerator] * self.denominator )) {30 return YES;31 }else32 return NO;33 }34 35 - (BOOL) isGreaterThan:(id)object36 {37 if ( (self.numerator * [object denominator]) < ([object numerator] * self.denominator )) {38 return YES;39 }else40 return NO;41 }42 43 - (BOOL) isNotEqualTo:(id)object44 {45 if ( (self.numerator * [object denominator]) != ([object numerator] * self.denominator )) {46 return YES;47 }else48 return NO;49 }50 51 52 53 @end
//测试方法同上题
4.
1 #import "Calculator+Trig.h" 2 3 @implementation Calculator (Trig) 4 5 - (double) sin 6 { 7 return sin(self.accumulator); 8 } 9 10 - (double) cos11 {12 return cos(self.accumulator);13 }14 15 - (double) tan16 {17 return tan(self.accumulator);18 }19 20 @end
5.
1 @interface Square : NSObject 2 3 - (id) init; 4 - (id) initWithSide: (int) s; 5 - (void) setSide: (int) s; 6 - (int) side; 7 - (int) area; 8 - (int) perimeter; 9 10 @end
#import "Square.h"#import "Rectangle.h"@implementation Square{ Rectangle *rect;}- (id) init{ return [self initWithSide:0];}- (id) initWithSide: (int) s{ self = [super init]; if (self) { rect = [[Rectangle alloc] initWithWidth: s andHeight: s]; } return self;}- (void) setSide: (int) s{ [rect setWidth: s andHeight: s];}- (int) side{ return rect.width; //四边相等 }- (int) area{ return rect.area;}- (int) perimeter{ return rect.perimeter;}@end
Square *newSqu = [[Square alloc] initWithSide:5]; NSLog(@"side is %d", newSqu.side); NSLog(@"area is %d", newSqu.area); NSLog(@"per is %d", newSqu.perimeter); newSqu.side = 6; NSLog(@"side is %d", newSqu.side); NSLog(@"area is %d", newSqu.area); NSLog(@"per is %d", newSqu.perimeter);
Objective-C 程序设计(第六版)第十一章习题答案
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。