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

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

时间:2014-11-08 23:30:13      阅读:462      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   color   ar   使用   sp   文件   div   

1.各个方法的实现

 1 - (Fraction *) subtract: (Fraction *) f
 2 {
 3     Fraction *result = [[Fraction alloc] init];
 4     
 5     result.numerator = numerator *f.denominator - denominator *f.numerator;
 6     result.denominator = denominator *f.denominator;
 7     
 8     [result reduce];
 9     return  result;
10 }
11 
12 - (Fraction *) multiply: (Fraction *) f
13 {
14     Fraction *result = [[Fraction alloc] init];
15     
16     result.numerator = numerator *f.numerator;
17     result.denominator = denominator *f.denominator;
18     
19     [result reduce];
20     return result;
21 }
22 
23 - (Fraction *) divide: (Fraction *) f
24 {
25     Fraction *result = [[Fraction alloc] init];
26     
27     result.numerator = numerator *f.denominator;
28     result.denominator = denominator *f.numerator;
29     
30     [result reduce];
31     return result;
32 }

2.print方法

 1 - (void) print: (BOOL) isReduce
 2 {
 3     if (numerator % denominator == 0) {
 4         NSLog(@"%d",numerator/denominator);
 5     }else if (isReduce) {
 6         [self reduce];
 7         NSLog(@"%d / %d",numerator,denominator);
 8     }else
 9         NSLog(@"%d / %d",numerator,denominator);
10 
11 }

3.对负分数一样使用

 1 int main(int argc, const char * argv[]) {
 2     @autoreleasepool {
 3         // insert code here...
 4         
 5         Fraction *aFraction = [[Fraction alloc] init];
 6         Fraction *bFraction = [[Fraction alloc] init];
 7         Fraction *resultFraction;
 8         
 9         [aFraction setTo:-1 over:4];
10         [bFraction setTo:-1 over:2];
11         
12         resultFraction = [aFraction add:bFraction];
13         
14         
15         [resultFraction print:YES];
16     }
17     return 0;
18 }

4.

 1 - (void) print: (BOOL) isReduce 
 2 {
 3     if (numerator % denominator == 0) {
 4         NSLog(@"%d",numerator/denominator);
 5     }else if (numerator > denominator){
 6         [self reduce];
 7         NSLog(@"%d %d/%d",numerator/denominator,numerator % denominator,denominator);
 8     }else if (isReduce) {
 9         [self reduce];
10         NSLog(@"%d/%d",numerator,denominator);
11     }else
12         NSLog(@"%d/%d",numerator,denominator);
13 
14 }

5.去掉synthesize合成方法后 修改变量名 _numerator, _denominator  替换程序中的numerator,denominator;增加setter方法 用于点语法。

6..m文件

1 - (Complex *) add: (Complex *) complexNum
2 {
3     Complex *result = [[Complex alloc] init];
4     
5     result.real = real + complexNum.real;
6     result.imaginary = imaginary + complexNum.imaginary;
7     
8     return result;
9 }

main

 1         Complex * aComplex = [[Complex alloc]init];
 2         Complex * bComplex = [[Complex alloc]init];
 3         Complex * resultComplex;
 4 
 5         
 6         [aComplex setReal:5.3];
 7         [aComplex setImaginary:7];
 8         [bComplex setReal:2.7];
 9         [bComplex setImaginary:4];
10         resultComplex = [aComplex add:bComplex];
11         
12         NSLog(@"result is %.f + %.fi",[resultComplex real],[resultComplex imaginary]);
13         

7.略吧

 

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

标签:style   blog   io   color   ar   使用   sp   文件   div   

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

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