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

   2023-02-09 学习力0
核心提示:1. 1 #import "Fraction.h" 23 @interface Fraction (MathOps) 45 - (Fraction *) add: (Fraction *) f; 67 - (Fraction *) mul: (Fraction *) f; 89 - (Fraction *) sub: (Fraction *) f;10 11 - (Fraction *) div: (Fraction *) f;12 13 - (Fraction *) inv

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);
    

 

 
反对 0举报 0 评论 0
 

免责声明:本文仅代表作者个人观点,与乐学笔记(本网)无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
    本网站有部分内容均转载自其它媒体,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责,若因作品内容、知识产权、版权和其他问题,请及时提供相关证明等材料并与我们留言联系,本网站将在规定时间内给予删除等相关处理.

  • 《黑马程序员》 category分类的使用(Objective
    分类的作用:在不改变原来类的基础上,可以给类增加一些方法。使用注意 : ①  分类只能增加方法,不可以增加成员变量                ②  分类的方法在实现中可以访问成员变量,不过成员变量必须手动实现。               
    03-16
  • Objective-C学习—UIScrollView控件使用
    Objective-C学习—UIScrollView控件使用
    一、知识点简单介绍1.UIScrollView控件是什么?(1)移动设备的屏幕⼤大⼩小是极其有限的,因此直接展⽰示在⽤用户眼前的内容也相当有限(2)当展⽰示的内容较多,超出⼀一个屏幕时,⽤用户可通过滚动⼿手势来查看屏幕以外的内容(3)普通的UIView不具备滚动功能
    03-16
  • Objective-C Runtime(转)
    博主地址: http://yulingtianxia.com/blog/2014/11/05/objective-c-runtime/曾经觉得Objc特别方便上手,面对着 Cocoa 中大量 API,只知道简单的查文档和调用。还记得初学 Objective-C 时把[receiver message]当成简单的方法调用,而无视了“发送消息”这句话
    03-08
  • iOS扩展——Objective-C开发编程规范 简单io扩展实验代码
    iOS扩展——Objective-C开发编程规范 简单io扩
      最近准备开始系统学习一个完整项目的开发流程和思路,在此之前,我们需要对iOS的开发变成规范进行更系统和详尽的学习,随意对编程规范进行了整理和学习。本文内容主要转载自:Objective-C-Coding-Guidelines-In-Chinese  此外,这篇文章所说的一些常见
    03-08
  • Objective-C利用协议实现回调函数
    实现一个显示文字为测试的视图,然后经过3秒钟测试文字变为回调函数文字。相应的截图如下:  实现的代码如下:定义协议:#import UIKit/UIKit.h @protocol NoteDelegate //回调函数 -(void)messageCallBack:(NSString *)string; @end 调用协议:#impor
    02-10
  • 刨根问底Objective-C Runtime(1)- Self & S
      刨根问底Objective-C Runtime(1)- SelfSuper - Chun Tips专注iOS开发 刨根问底Objective-C Runtime(1)- SelfSuper前言关于Objective-C Runtime一篇好的文档 : Understanding the Objective-C Runtime译文地址为: http://blog.cocoabit.com/blog/2
    02-10
  • Objective-C利用协议实现回调函数
    实现一个显示文字为测试的视图,然后经过3秒钟测试文字变为回调函数文字。相应的截图如下: 实现的代码如下:定义协议:#import UIKit/UIKit.h @protocol NoteDelegate//回调函数-(void)messageCallBack:(NSString *)string;@end  调用协议:#import Founda
    02-10
  • Objective-c开发中混合使用ARC
    首选“Compile Sources”的位置:选中工程-TARGETS-相应的target然后选中右侧的“Build Phases”,向下就找到“Compile Sources”了。如何在未使用arc的工程中引入一个使用了arc特性的文件:对相应的文件添加:-fobjc-arc参数如何在arc工程中引用未使用arc的文件
    02-10
  • Objective C运行时(runtime)技术总结,好强大
    前言:         Objective C的runtime技术功能非常强大,能够在运行时获取并修改类的各种信息,包括获取方法列表、属性列表、变量列表,修改方法、属性,增加方法,属性等等,本文对相关的几个要点做了一个小结。目录:(1)使用class_replaceMethod/cla
    02-09
  • Programming With Objective-C---- Introductio
    a:link { color: rgba(88, 114, 210, 1); text-decoration: none }a:visited { color: rgba(88, 114, 210, 1); text-decoration: none }a:hover { color: rgba(173, 189, 248, 1); text-decoration: none }a:active { color: rgba(0, 0, 255, 1); text-decora
    02-09
点击排行