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

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

时间:2014-11-07 16:48:05      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   io   color   ar   sp   for   div   

1.

1         int  value1,value2;
2         printf("请输入两个整数,用逗号隔开:");
3         scanf("%d,%d",&value1,&value2);
4         
5         if (value1 % value2 == 0) {
6             printf("可以整除\n");
7         }
8         printf("不能整除\n");
9         

2.  main函数部分

 1         double   value1, value2;
 2         char      operator;
 3         Calculator *deskCalc = [[Calculator alloc] init];
 4         
 5         NSLog(@"Type in your expression.");
 6         scanf("%lf  %c  %lf", &value1, &operator, &value2);
 7         
 8         [deskCalc setAccumulator:value1];
 9         
10         if ( operator == + ) {
11             [deskCalc add: value2];
12         }else if ( operator == -)
13             [deskCalc subtract: value2];
14         else if ( operator == * )
15             [deskCalc multiply: value2];
16         else if ( operator == /)
17             if ( value2 == 0) {
18                 NSLog(@"Division by zero.");
19 //                NSLog(@"%.2f",[deskCalc accumulator]);
20             }else
21                 [deskCalc divide: value2];
22         else
23             NSLog(@"Unknown operator.");
24         NSLog(@"%.2f",[deskCalc accumulator]);

3.修改print方法

- (void) print
{
    if (numerator % denominator == 0 && denominator != 0) {
        NSLog(@"%d",numerator/denominator);
    }else if (numerator == 0 )
        NSLog(@"分数为 0 ");
    else
        NSLog(@"%d/%d", numerator,denominator);

}

4.有内存泄露的情况 懒得改了

        double   number;
        char      operator;
        Calculator *deskCalc = [[Calculator alloc] init];
        
        NSLog(@"Type in your expression.");
//        scanf("%lf  ", &number);

        while ( operator != E ) {
            scanf("%lf %c",&number,&operator);
            if ( operator == S) {
                [deskCalc setAccumulator:number];
                NSLog(@"%lf",[deskCalc accumulator]);
                
            }else if ( operator == + ){
                [deskCalc add:number];
                NSLog(@"加法 %lf",[deskCalc accumulator]);
            }else if ( operator == - ){
                [deskCalc subtract:number];
                NSLog(@"减法%lf",[deskCalc accumulator]);
            }else if ( operator == * ){
                [deskCalc multiply:number];
                NSLog(@"乘法%lf",[deskCalc accumulator]);
            }else if ( operator == / ){
                if (number != 0) {
                    [deskCalc divide:number];
                    NSLog(@"除法%lf",[deskCalc accumulator]);
                }else
                    NSLog(@"除数为0 不合法");
            }else
                NSLog(@"您输入的符号不合法");
    }

5.

        int number, right_digit;
        BOOL isTure = \0;
        
        NSLog(@"Enter your number.");
        scanf("%d", &number);
        
        if ( number < 0 ) {
            number = -number;
            isTure = YES;
        }
        do {
            right_digit = number % 10;
            NSLog(@"%d", right_digit);
            number /= 10;
        } while ( number != 0 );
        if ( isTure ) {
            NSLog(@"-");
            
        }

6.很难 做出一部分  当尾数有一个零或者几个零的时候显示不完全。头疼 

 1         int number, right_digit, reverseNum = 0;
 2         NSLog(@"请输入一个整数");
 3         scanf("%d", &number);
 4         
 5         do {
 6             right_digit = number % 10;
 7             reverseNum *= 10;
 8             reverseNum += right_digit;
 9 //            NSLog(@"%d", reverseNum);
10             number /= 10;
11         } while (number != 0);
12         
13         do {
14             right_digit = reverseNum % 10;
15             switch (right_digit) {
16                 case 0:
17                     NSLog(@"zero");
18                     break;
19                 case 1:
20                     NSLog(@"one");
21                     break;
22                 case 2:
23                     NSLog(@"two");
24                     break;
25                 case 3:
26                     NSLog(@"three");
27                     break;
28                 case 4:
29                     NSLog(@"four");
30                     break;
31                 case 5:
32                     NSLog(@"five");
33                     break;
34                 case 6:
35                     NSLog(@"six");
36                     break;
37                 case 7:
38                     NSLog(@"seven");
39                     break;
40                 case 8:
41                     NSLog(@"eight");
42                     break;
43                 case 9:
44                     NSLog(@"nine");
45                     break;
46                 default:
47                     break;
48             }
49             reverseNum /= 10;
50         } while (reverseNum != 0);
51         
52         

7.这道题理解不是很充分。有机会在做吧

 1          int p, d, isPrime;
 2         
 3         for ( p = 2; p <= 50; ++p ){
 4             if ( p % 2 == 0 ){
 5                 isPrime = 0;
 6             }
 7             isPrime = 1;
 8             
 9             for ( d = 2; d < p; ++d ){
10                 if ( p % d == 0 )
11                     isPrime = 0;
12             }
13             
14             if ( isPrime != 0 )
15                 NSLog (@"%i", p);
16         }
17         

 

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

标签:des   style   blog   io   color   ar   sp   for   div   

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

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