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 *) 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);
    
时间: 2024-10-30 11:48:23

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

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

1.略 2. #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { // insert code here... NSLog(@"In Obiective-c, lowercase letters are significance .\n main is where program execution begins.\n Open and close

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

1.检测不到reduce方法,因为Complex类中没有定义: 2.合法.因为id类型可以用来存储属于任何类的对象(不能为id变量使用点运算符) 3. 1 //XYPoint类print方法 2 3 4 - (void) print 5 { 6 NSLog(@" (%g , %g) ", x, y); 7 } 8 9 10 //main函数部分 11 12 id dataValue; 13 14 XYPoint *x1 = [[XYPoint alloc] init]; 15 16 [

Python核心编程(第二版) 第二章习题答案 未完待续

2-2.程序输出.阅读下面的Python脚本.#!/usr/bin/env python1 + 2 * 4(a)你认为这段脚本是用来做什么的?(b)你认为这段脚本会输出什么?(c)输入以上代码,并保存为脚本,然后运行它,它所做的与你的预期一样吗?为什么一样/不一样?(d)这段代码单独执行和在交互解释器中执行有何不同?试一下,然后写出结果.(e)如何改进这个脚本,以便它能和你想象的一样工作?答:(a)这段脚本是用来计算表达式的值(b)脚本会输出9(c)保存为脚本,运行后没有输出.和自己预期不一样.

第十一章习题答案

第十一章练习题答案?1. 如何把 /etc/passwd 中用户uid 大于500 的行给打印出来?awk -f ':' '$3 > 500' /etc/passwd? 2. awk中 nr,nf两个变量表示什么含义awk -f ':' '{print $nr}' /etc/passwd 会打印出什么结果出来?nr表示行数,nf表示一共有多少段?awk -f ':' '{print $nr}' /etc/passwd 会依次打印对应的行数的段,第一行打印第一段,第二行打印第二段... 到了最后就

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

1. 1 - (id) init 2 { 3 return [self initWithWidth: 0 andHeight: 0]; 4 } 5 6 - (id) initWithWidth: (int) w andHeight: (int) h 7 { 8 self = [super init]; 9 if (self) { 10 [self setWidth: w andHeight: h]; 11 } 12 return self; 13 } 2. 1 - (id)init 2 { 3

C++程序设计原理与实践 第十一章部分答案

1 #include "../../st.h" 2 3 int main() 4 try{ 5 string s1="a.txt"; 6 string s2="z.txt"; 7 ifstream ifs(s1.c_str()); 8 if(!ifs) 9 error("can not open input file",s1); 10 ofstream ofs(s2.c_str()); 11 if(!ofs) 12 error

C++程序设计原理与实践 第二十一章部分答案

1 #include <iostream> 2 #include <vector> 3 #include <string> 4 #include <list> 5 #include<fstream> 6 #include<algorithm> 7 #include<stdexcept> 8 using namespace std; 9 10 struct Item 11 { 12 string name; 13 int i

Java语言程序设计基础篇第10版第5章习题答案

1 public class Demo { 2 public static void main(String[] args) { 3 java.util.Scanner input = new java.util.Scanner(System.in); 4 int num = input.nextInt(); 5 int count=0; 6 int a=0,b=0; 7 float sum=0; 8 while(num!=0){ 9 if(num>0) 10 a++; 11 else 12 b

概率论与数理统计严继高版第七章习题答案(含过程)

无7.3(不考)总习题我只有草稿,忘记带了,想起来就更 原文地址:https://www.cnblogs.com/cs-learn/p/9611237.html