objective-c第七章答案

1, - (Fraction *) subtract: (Fraction *) f
解释:
{ Fraction * result = [[Fraction alloc] init];
        result.numerator = numerator * f.denominator - denominator * f.numerator;
        result.denominator = denominator * f.denominator;
        [result reduce];
        return result;
}
2,- (Fraction *) multiply: (Fraction *) f
解释;
{  Fraction * result = [[Fraction alloc] init];
      result.numerator =numerator * f.denominator;
      result.denominator = denominator *f.denominator;
      [result reduce];
    return result;
}

   
   
- (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         


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值