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

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         
时间: 2024-08-29 15:22:46

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)保存为脚本,运行后没有输出.和自己预期不一样.

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

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

C++ Primer Plus 第六版 第16章 string类和标准模板库

1.string实际上是模板具体化basic_string<char> 的一个typedef,有默认参数,所以省略了初始化参数 2.size_type是一个依赖于实现的整形 string将string::npos定义为字符串的最大长度 3.string类的构造函数P656 4.对于c-风格字符串,3种输入方法:cin>>   cin.getline(),cin.get 对于string   ,2种输入方法:cin>>,getline(cin,string对象) 5.st

Python核心编程(第二版) 第六章习题答案

6–1.字符串.string 模块中是否有一种字符串方法或者函数可以帮我鉴定一下一个字符串是否是另一个大字符串的一部分? 答:有,string.find(str,beg,end) 6–2.字符串标识符.修改例 6-1 的 idcheck.py 脚本,使之可以检测长度为一的标识符,并且可以识别 Python 关键字,对后一个要求,你可以使用 keyword 模块(特别是 keyword.kelist)来帮你. 1 #!/usr/bin/python 2 3 import string 4 impo

Python核心编程(第二版) 第四章习题答案

4-1.Python对象.与所有Python对象有关的三个属性是什么?请简单的描述一下.答:与所有Python对象有关的三个属性是身份.类型.值.身份:每一个对象都有一个唯一的身份标识自己,任何对象的身份可以使用内建函数id()来得到.这个值可以被认为是该对象的内存地址.类型:对象的类型决定了该对象可以保存什么类型的值,可以进行什么样的操作,以及遵循什么规则.可以用内建函数type()来查看Python的类型.值:对象表示的数据项.4-2.类型.不可更改(immutable)指的是什么?Pyth

Python核心编程(第二版) 第五章习题答案

5-1.整型.讲讲Python普通整型和长整型的区别. 答:Python 的标准整数类型是最通用的数字类型.在大多数 32 位机器上,标准整数类型的取值范围是-2**31到 2**31-1,也就是-2,147,483,648 到 2,147,483,647.如果在 64 位机器上使用 64 位编译器编译 Python,那么在这个系统上的整数将是 64 位. Python 的长整数类型能表达的数值仅仅与你的机器支持的(虚拟)内存大小有关. 5-2.操作符.(a)写一个函数,计算并返回两个数的乘积.