码迷,mamicode.com
首页 > 其他好文 > 详细

Objective-C 程序设计(第六版)第十一章习题答案

时间:2014-11-21 01:22:09      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   ar   color   os   sp   on   div   

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 *) f
18 {
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 *) f
30 {
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 *) f
42 {
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 *) invert
53 {
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         }else
13             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         }else
20             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)object
12 {
13     if ( (self.numerator * [object denominator]) <= ([object numerator] * self.denominator )) {
14         return YES;
15     }else
16         return NO;
17 }
18 
19 - (BOOL) isLessThan:(id)object
20 {
21     if ( (self.numerator * [object denominator]) < ([object numerator] * self.denominator )) {
22         return YES;
23     }else
24         return NO;
25 }
26 
27 - (BOOL) isGreaterThanOrEqualTo:(id)object
28 {
29     if ( (self.numerator * [object denominator]) >= ([object numerator] * self.denominator )) {
30         return YES;
31     }else
32         return NO;
33 }
34 
35 - (BOOL) isGreaterThan:(id)object
36 {
37     if ( (self.numerator * [object denominator]) < ([object numerator] * self.denominator )) {
38         return YES;
39     }else
40         return NO;
41 }
42 
43 - (BOOL) isNotEqualTo:(id)object
44 {
45     if ( (self.numerator * [object denominator]) != ([object numerator] * self.denominator )) {
46         return YES;
47     }else
48         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) cos
11 {
12     return cos(self.accumulator);
13 }
14 
15 - (double) tan
16 {
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 程序设计(第六版)第十一章习题答案

标签:style   blog   io   ar   color   os   sp   on   div   

原文地址:http://www.cnblogs.com/MingMing-King/p/4109590.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!