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

1.   非法常量

      0x10.5    0X0G1    98.7U    17777s    0996    1.2Fe-7    15,000    .

2.

     //只需要列出表达式就可以
        
        int F = 27;       //设置F为华氏温度,赋值 27
        float C ;           //设置C为摄氏温度,类型为浮点类型
        
        C = (F-32)/1.8;
        NSLog(@"%.f",C);     // %.f 保留一位小数就可以

     //输出结果
       2014-09-17 14:58:48.259 prog1[6058:303] -2.8
       Program ended with exit code: 0

3.    输出结果为 d .

4.    

        float x = 2.55 ,  result ;
        
        result = 3 * (x * x * x) - 5 * (x * x) + 6 ;
        
        NSLog(@"result is %f",result);

       //结果如下
      2014-09-17 15:11:11.064 prog1[6196:303] result is 23.231621

5.

        //不知道这道题考的是什么 列出表达式过了得了

        double result;
        
        result = (3.31e-8 + 2.01e-7) / (7.16e-6 + 2.01e-8);
        
        NSLog(@"result is %e",result);
        
        //输出结果
        2014-09-17 15:29:31.547 prog1[6274:303] result is 3.260400e-02
        Program ended with exit code: 0

6.

//-------------interface部分-----------

@interface Complex : NSObject

- (void) setReal: (double) a;            //设置实数
- (void) setImaginary: (double) b;    //设置虚数
- (void) print;                                 //打印结果
- (double) real;
- (double) imaginary;

@end

//--------------implementation部分----------------

@implementation Complex
{
    double real;
    double imaginary;
}

- (void) setReal: (double) a
{
    real = a;
}

- (void) setImaginary: (double) b
{
    imaginary = b;
}

- (void) print
{
    NSLog(@"The complex is %.f + %.fi", real, imaginary);
}

- (double) real
{
    return real;
}

- (double) imaginary
{
    return imaginary;
}


@end


//----------------program部分-------------

int main(int argc, const char * argv[])
{

    @autoreleasepool {

        Complex * newComplex = [[Complex alloc]init];
        
        [newComplex setReal:11];
        [newComplex setImaginary:22];

//下面两个方法都可以 ,分别测试 print方法和 real,imaginary方法;(应题目要求) [newComplex print];

  //      NSLog(@"The complex is %.f + %.fi",[newComplex real],[newComplex imaginary]);

       
    }
    return 0;
}

//结果如下
   2014-09-17 17:27:59.830 prog1[6678:303] The complex is 11 + 22i
   Program ended with exit code: 0

7.

//-----------------interface部分------------------ 

@interface Rectangle : NSObject

- (void) setWidth: (int) w;       //设置矩形宽度
- (void) setHeight: (int) h;      //设置矩形高度
- (int) width;
- (int) height;
- (int) area;                            //面积
- (int) perimeter;                    //周长

@end


//----------------implementation部分---------------

@implementation Rectangle

{
    int width;
    int height;
}

- (void) setWidth: (int) w
{
    width = w;
}

- (void) setHeight: (int) h
{
    height = h;
}

- (int) width
{
    return  width;
}

- (int) height
{
    return height;
}

- (int) area
{
    return (width * height);
}

- (int) perimeter
{
    return 2*(width + height);
}


@end


//-----------------program部分--------------

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        
        Rectangle *newRectangle = [[Rectangle alloc]init];
        
        [newRectangle setWidth:12];
        [newRectangle setHeight:21];
        
        //测试新类的各个方法--------------
        NSLog(@"Width of newRectangle is %d",      [newRectangle width]);
        NSLog(@"Height of newRectangle is %d",     [newRectangle height]);
        NSLog(@"Area of newRectangle is %d",        [newRectangle area]);
        NSLog(@"Perimeter of newRectangle is %d", [newRectangle perimeter]);    
        
    }
    return 0;
}
//结果如下
2014-09-17 22:55:03.825 prog1[7272:303] Width of newRectangle is 12

2014-09-17 22:55:03.827 prog1[7272:303] Height of newRectangle is 21

2014-09-17 22:55:03.827 prog1[7272:303] Area of newRectangle is 252

2014-09-17 22:55:03.827 prog1[7272:303] Perimeter of newRectangle is 66

Program ended with exit code: 0

 

8. 

//----------------interface部分---------------

@interface Calculator : NSObject

//设置累加器
- (void) setAccumulator: (double) value;
- (void) clear;
- (double) accumulator;

//加减乘除
- (double) add: (double) value;
- (double) subtract: (double) value;
- (double) multiply: (double) value;
- (double) divide: (double) value;

@end


//------------------implementation部分------------------

@implementation Calculator

{
    double accumulator;
}

- (void) setAccumulator: (double) value
{
    accumulator = value;
}

- (void) clear
{
    accumulator = 0;
}

- (double) accumulator
{
    return accumulator;
}

- (double) add: (double) value
{
    accumulator += value;
    return accumulator;
}

- (double) subtract: (double) value
{
    accumulator -= value;
    return accumulator;
}

- (double) multiply: (double) value
{
    accumulator *= value;
    return accumulator;
}

- (double) divide: (double) value
{
    accumulator /= value;
    return accumulator;
}

@end


//---------------------program部分------------------

int main(int argc, const char * argv[])
{

    @autoreleasepool {

         Calculator *deskCalc = [[Calculator alloc]init];
        
        [deskCalc setAccumulator:100.0];

        NSLog(@"%g",[deskCalc add:10]);
        NSLog(@"%g",[deskCalc subtract:10]);
        NSLog(@"%g",[deskCalc multiply:10]);
        NSLog(@"%g",[deskCalc divide:10]);
      
    }
    return 0;
}

 

 9.

//----------------interface部分---------------

@interface Calculator : NSObject

//设置累加器
- (void) setAccumulator: (double) value;
- (void) clear;
- (double) accumulator;

//加减乘除
- (double) add: (double) value;
- (double) subtract: (double) value;
- (double) multiply: (double) value;
- (double) divide: (double) value;
//改变累加器正负号,累加器的倒数,累加器的平方
- (double) changeSign;
- (double) reciprocal;
- (double) xSquared;

 @end



//------------------implementation部分------------------

@implementation Calculator

{
    double accumulator;
}

- (void) setAccumulator: (double) value
{
    accumulator = value;
}

- (void) clear
{
    accumulator = 0;
}

- (double) accumulator
{
    return accumulator;
}

- (double) add: (double) value
{
    accumulator += value;
    return accumulator;
}

- (double) subtract: (double) value
{
    accumulator -= value;
    return accumulator;
}

- (double) multiply: (double) value
{
    accumulator *= value;
    return accumulator;
}

- (double) divide: (double) value
{
    accumulator /= value;
    return accumulator;
}
- (double) changeSign
{
    accumulator = -accumulator;
    return accumulator;
}
 
- (double) reciprocal
{
    accumulator = 1/accumulator;
    return accumulator;
}
 
- (double) xSquared
{
    accumulator = accumulator * accumulator;
    return accumulator;
}
 
@end


//---------------------program部分------------------

int main(int argc, const char * argv[])
{

    @autoreleasepool {

         Calculator *deskCalc = [[Calculator alloc]init];
        
        [deskCalc setAccumulator:100.0];

//        NSLog(@"%g", [deskCalc add:10]);
//        NSLog(@"%g", [deskCalc subtract:10]);
//        NSLog(@"%g", [deskCalc multiply:10]);
//        NSLog(@"%g", [deskCalc divide:10]);
        NSLog(@"%g", [deskCalc changeSign]);
        NSLog(@"%g", [deskCalc reciprocal]);
        NSLog(@"%g", [deskCalc xSquared]);
      
    }
    return 0;
}

 10.

//inerface

//内存相关
- (double) memoryClear;                    //清理内存
- (double) memoryStore;                   //设置内存为累加器
- (double) memoryRecall;                   //设置累加器到内存
- (double) memoryAdd: (double) value;      //添加值到内存
- (double) memorySubtract: (double) value;    //与内存的值相减

//@implementation

- (double) memoryClear
{
    memory = 0.0;
    return accumulator;
}

- (double) memoryStore
{
    memory = accumulator;
    return accumulator;
}

- (double) memoryRecall
{
    accumulator = memory;
    return accumulator;
}

//应题目要求,所有方法返回值都是累加器。但是全是无用功 
//正确的方法是 accumulator = memory
- (double) memoryAdd: (double) value
{
    memory += value;
    return accumulator;
}
- (double) memorySubtract: (double) value
{
    memory -= value;
    return accumulator;
}

 

转载于:https://www.cnblogs.com/MingMing-King/p/3978113.html

你可能感兴趣的:(Objective-C 程序设计(第六版)第四章习题答案)