《c程序设计语言第二版答案.pdf》由会员分享,可在线阅读,更多相关《c程序设计语言第二版答案.pdf(24页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、c 程序设计语言第二版答案【篇一:c 语言程序设计现代方法(第二版)习题答案】answers to selected exercisesanswers to selected exercises 2. was #2 (a) the program contains one directive (#include) 2. was #2 (a) the program contains one directive (#include)and four statements (three calls of printf and one return).and four statements (thr
2、ee calls of printf and one return). (b) (b) parkinsons law: parkinsons law: work expands so as to fill the time work expands so as to fill the time available for its completion. available for its completion. 3. was #4 3. was #4 #include stdio.h #include stdio.h int main(void) int main(void) int heig
3、ht = 8, length = 12, width = 10, volume; int height = 8, length = 12, width = 10, volume; volume = height * length * width; volume = height * length * width; printf(dimensions: %dx%dx%dn, length, width, height); printf(dimensions: %dx%dx%dn, length, width, height); printf(volume (cubic inches): %dn,
4、 volume); printf(volume (cubic inches): %dn, volume); printf(dimensional weight (pounds): %dn, (volume + 165) / printf(dimensional weight (pounds): %dn, (volume + 165) /166);166); return 0; return 0; 4. was #6 heres one possible program: 4. was #6 heres one possible program: #include stdio.h #includ
5、e stdio.h int main(void) int main(void) int i, j, k; int i, j, k; float x, y, z; float x, y, z; printf(value of i: %dn, i); printf(value of i: %dn, i); printf(value of j: %dn, j); printf(value of j: %dn, j); printf(value of k: %dn, k); printf(value of k: %dn, k); printf(value of x: %gn, x); printf(v
6、alue of x: %gn, x); printf(value of y: %gn, y); printf(value of y: %gn, y);printf(value of z: %gn, z);printf(value of z: %gn, z); return 0; return 0; when compiled using gcc and then executed, this program when compiled using gcc and then executed, this programproduced the following output:produced
7、the following output: value of i: 5618848 value of i: 5618848 value of j: 0 value of j: 0 value of k: 6844404 value of k: 6844404 value of x: 3.98979e-34 value of x: 3.98979e-34 value of y: 9.59105e-39 value of y: 9.59105e-39 value of z: 9.59105e-39 value of z: 9.59105e-39 the values printed depend
8、on many factors, so the chance that the values printed depend on many factors, so the chance thatyoull get exactly these numbers is small.youll get exactly these numbers is small. 5. was #10 (a) is not legal because 100_bottles begins with a 5. was #10 (a) is not legal because 100_bottles begins wit
9、h adigit.digit. 8. was #12 there are 14 tokens: a, =, (, 3, *, q, -, p, *, p, ), /, 3, 8. was #12 there are 14 tokens: a, =, (, 3, *, q, -, p, *, p, ), /, 3,and ;.and ;. answers to selected programming projects answers to selected programming projects 4. was #8; modified 4. was #8; modified #include
10、 stdio.h #include stdio.h int main(void) int main(void) float original_amount, amount_with_tax; float original_amount, amount_with_tax; printf(enter an amount: ); printf(enter an amount: ); scanf(%f, original_amount); scanf(%f, original_amount); amount_with_tax = original_amount * 1.05f; amount_with
11、_tax = original_amount * 1.05f; printf(with tax added: $%.2fn, amount_with_tax); printf(with tax added: $%.2fn, amount_with_tax); return 0; return 0; the amount_with_tax variable is unnecessary. if we remove it, the amount_with_tax variable is unnecessary. if we remove it,the program is slightly sho
12、rter:the program is slightly shorter: #include stdio.h #include stdio.h int main(void) int main(void) float original_amount; float original_amount; printf(enter an amount: ); printf(enter an amount: ); scanf(%f, original_amount); scanf(%f, original_amount); printf(with tax added: $%.2fn, original_am
13、ount * 1.05f); printf(with tax added: $%.2fn, original_amount * 1.05f); return 0; return 0; chapter 3 chapter 3 answers to selected exercises answers to selected exercises 2. was #2 2. was #2 (a) printf(%-8.1e, x); (a) printf(%-8.1e, x); (b) printf(%10.6e, x); (b) printf(%10.6e, x); (c) printf(%-8.3
14、f, x); (c) printf(%-8.3f, x); (d) printf(%6.0f, x); (d) printf(%6.0f, x); 5. was #8 the values of x, i, and y will be 12.3, 45, and .6, 5. was #8 the values of x, i, and y will be 12.3, 45, and .6,respectively. answers to selected programming projectsrespectively. answers to selected programming pro
15、jects 1. was #4; modified 1. was #4; modified #include stdio.h #include stdio.h int main(void) int main(void) int month, day, year; int month, day, year; printf(enter a date (mm/dd/yyyy): ); printf(enter a date (mm/dd/yyyy): ); scanf(%d/%d/%d, month, day, year); scanf(%d/%d/%d, month, day, year); pr
16、intf(you entered the date %d%.2d%.2dn, year, month, day); printf(you entered the date %d%.2d%.2dn, year, month, day); return 0; return 0; 3. was #6; modified 3. was #6; modified#include stdio.h#include stdio.h int main(void) int main(void) int prefix, group, publisher, item, check_digit; int prefix,
17、 group, publisher, item, check_digit; printf(enter isbn: ); printf(enter isbn: ); scanf(%d-%d-%d-%d-%d, prefix, group, publisher, item, scanf(%d-%d-%d-%d-%d, prefix, group, publisher, item,check_digit);check_digit); printf(gs1 prefix: %dn, prefix); printf(gs1 prefix: %dn, prefix); printf(group ident
18、ifier: %dn, group); printf(group identifier: %dn, group); printf(publisher code: %dn, publisher); printf(publisher code: %dn, publisher); printf(item number: %dn, item); printf(item number: %dn, item); printf(check digit: %dn, check_digit); printf(check digit: %dn, check_digit); /* the five printf c
19、alls can be combined as follows: /* the five printf calls can be combined as follows: printf(gs1 prefix: %dngroup identifier: %dnpublisher printf(gs1 prefix: %dngroup identifier: %dnpublishercode: %dnitem number: %dncheck digit: %dn,code: %dnitem number: %dncheck digit: %dn, prefix, group, publisher
20、, item, check_digit); prefix, group, publisher, item, check_digit); */ */ return 0; return 0; chapter 4 chapter 4 answers to selected exercises answers to selected exercises 2. was #2 not in c89. suppose that i is 9 and j is 7. the value 2. was #2 not in c89. suppose that i is 9 and j is 7. the valu
21、eof (-i)/j could be eitherof (-i)/j could be either 1 or1 or 2, depending on the2, depending on theimplementation. on the other hand, the value of -(i/j) is alwaysimplementation. on the other hand, the value of -(i/j) is always 1, regardless of the implementation. in c99, on the other hand,1, regard
22、less of the implementation. in c99, on the other hand,the value of (-i)/j must be equal to the value of -(i/j).the value of (-i)/j must be equal to the value of -(i/j). 9. was #6 9. was #6 (a) 63 8 (a) 63 8 (b) 3 2 1 (b) 3 2 1 (c) 2 -1 3 (c) 2 -1 3 (d) 0 0 0 (d) 0 0 013. was #8 the expression +i is
23、equivalent to (i += 1). the13. was #8 the expression +i is equivalent to (i += 1). thevalue of both expressions is i after the increment has beenvalue of both expressions is i after the increment has beenperformed. answers to selected programming projectsperformed. answers to selected programming pr
24、ojects 2. was 2. was #4 #4 #include stdio.h #include stdio.h int main(void) int main(void) int n; int n; printf(enter a three-digit number: ); printf(enter a three-digit number: ); scanf(%d, n); scanf(%d, n); printf(the reversal is: %d%d%dn, n % 10, (n / 10) % 10, n / 100); printf(the reversal is: %
25、d%d%dn, n % 10, (n / 10) % 10, n / 100); return 0; return 0; chapter 5 chapter 5 answers to selected exercises answers to selected exercises 2. was #2 2. was #2 (a) 1 (a) 1 (b) 1 (b) 1 (c) 1 (c) 1 (d) 1 (d) 1 4. was #4 (i j) - (i j) 4. was #4 (i j) - (i j) 6. was #12 yes, the statement is legal. whe
26、n n is equal to 5, it 6. was #12 yes, the statement is legal. when n is equal to 5, itdoes nothing, since 5 is not equal todoes nothing, since 5 is not equal to 9.9. 10. was #16 the output is 10. was #16 the output is onetwo onetwo since there are no break statements after the cases. since there are
27、 no break statements after the cases. answers to selected programming projects answers to selected programming projects 2. was #6 2. was #6【篇二:c 语言与程序设计-第 2 章课后习题参考答案】txttxt关键字是关键字是注释注释空白符空白符八进制常量是八进制常量是三字符序列三字符序列字符串常量是字符串常量是括号是括号是 2.2 c 2.2 c 编译器可将以下每一个源字符串分解为哪些记号?不必考编译器可将以下每一个源字符串分解为哪些记号?不必考虑记号组合是
28、否合法虑记号组合是否合法 (1) x+y x, +, +, y (1) x+y x, +, +, y (2) -0 xabl -, 0 xabl (2) -0 xabl -, 0 xabl (3) 2.89e+12l 2.89e+12l (3) 2.89e+12l 2.89e+12l (4) string+foo string+ foo (4) string+foo string+ foo (5) x*2 x, *, *, 2 (5) x*2 x, *, *, 2 (6) x?/ x?/ (6) x?/ x?/ (7) a?ba, ?, b (7) a?ba, ?, b (8) x-+=y x
29、, -, +=, y (8) x-+=y x, -, +=, y (9) intx=+10 intx, =, +, 10 (9) intx=+10 intx, =, +, 10 (10) stringfoo string, foo (10) stringfoo string, foo这道题当时改的时候有几个小题改得有错误,注意!这道题当时改的时候有几个小题改得有错误,注意! 2.3 2.3 以下哪些不是标识符,为什么?以下哪些不是标识符,为什么?标识符由字母、数字和下划线组成,但首字符必须是字母或下划线。标识符由字母、数字和下划线组成,但首字符必须是字母或下划线。 4th 4th 不是,以数字
30、开头;不是,以数字开头; sizeof sizeof 不是不是( (标准标准 c c 的关键字的关键字) ) _limit _limit 是是 _is2 _is2 是是 xyshould xyshould 是是 x*y x*y 不是,不是,* * 非法非法 o_no_o_no o_no_o_no 是是 temp-2 temp-2 不是,不是,- - 非法非法 isnt isnt 不是,不是, 非法非法 enum enum 不是不是( (标准标准 c c 的关键字。注:关键字也称为保留字,是被系统的关键字。注:关键字也称为保留字,是被系统赋予特定含义并有专门用途的标识符。关键字不能作为普通标识符
31、,赋予特定含义并有专门用途的标识符。关键字不能作为普通标识符,但可以作为宏名。所有预处理均发生在识别这些关键字之前。但可以作为宏名。所有预处理均发生在识别这些关键字之前。) ) 2.4 2.4 在以下表示中,哪些是合法常数,哪些是非法常数?对于合法在以下表示中,哪些是合法常数,哪些是非法常数?对于合法常数,指出其类型;对于非法常数,说明其错误原因。常数,指出其类型;对于非法常数,说明其错误原因。 2l 2l 合法,合法,longlong 长整型长整型不合法,单引号组中的单引号前需要转义字符不合法,单引号组中的单引号前需要转义字符.12.12 合法,合法,doubledouble 双精度浮点型双
32、精度浮点型 0 x1ag 0 x1ag 不合法,不合法,g g 不是不是 1616 进制数中的符号,也不表示任何类型进制数中的符号,也不表示任何类型 33333 33333 合法,合法,intint 整形整形 a a 合法,字符串常量合法,字符串常量合法,字符串常量合法,字符串常量 0.l 0.l 合法,合法,long doublelong double 长双精度浮点型长双精度浮点型 e20 e20 不合法,缺少尾数部分不合法,缺少尾数部分 0377ul 0377ul 合法,合法,unsigned longunsigned long 无符号长整型无符号长整型 18 18 不合法,存在非不合法,
33、存在非 8 8 进制位进制位 0 xa 0 xa 不合法,不符合十六进制字符码表示规则不合法,不符合十六进制字符码表示规则xhhxhh 0 x9cfu 0 x9cfu 合法,合法,unsigned intunsigned int 无符号整形无符号整形 45 45 合法,合法,charchar 字符型字符型 1.e-5 1.e-5 合法,合法,doubledouble 双精度浮点型双精度浮点型 0 0 合法,合法,charchar 字符型字符型 3.f 3.f 合法,合法,floatfloat 浮点型浮点型 34 34 不合法,缺少转义符不合法,缺少转义符合法,合法,charchar 字符型字符
34、型p35p35,双引号作为字符常量时既可用图形符号,双引号作为字符常量时既可用图形符号也可用转义序列表示也可用转义序列表示 a a 合法,合法,charchar 字符型字符型 2.6 2.6 以下的变量声明语句中有什么错误?以下的变量声明语句中有什么错误? (1) int a; b = 5; (1) int a; b = 5; 第一个分号改为逗号第一个分号改为逗号 int a, b=5; int a, b=5; (2) doubel h; (2) doubel h; 关键字错误关键字错误 double h;double h; (3) int x = 2.3; (3) int x = 2.3;
35、类型错误类型错误 float x = 2.3;float x = 2.3; (4) const long y; (4) const long y; 需要赋初值需要赋初值 const long y = 0;const long y = 0; (5) float a = 2.5*g; g (5) float a = 2.5*g; g 未定义变量未定义变量 int g = 1; float a = 2.5*g; int g = 1; float a = 2.5*g; (6) int a = b = 2; b (6) int a = b = 2; b 未定义变量未定义变量 int a = 2, b =
36、 2;int a = 2, b = 2; 2.7 2.7 设变量说明为:设变量说明为: int a = 1, b = 2, c = 3, d; int a = 1, b = 2, c = 3, d; double x = 2.0; y = 7.7; double x = 2.0; y = 7.7;请给出以下表达式的值。请给出以下表达式的值。 (1) +a*b- 4 (1) +a*b- 4 (2) !a+b/c 0 (2) !a+b/c 0 (3) a=-b+c true (3) a=-b+c true (4) d=a+,a*=b+1d (4) d=a+,a*=b+1d 为为 1, a1, a
37、为为 6 6 (5) d=y+=1/x y (5) d=y+=1/x y 为为 8.2, d8.2, d 为为 8 8 (6) abx=yfalse (6) abx=yfalse (7) x=(int)y/b+x (7) x=(int)y/b+x 为为 3.03.0 (8) a-?+a:+aa (8) a-?+a:+aa 为为 1 1 (9) a+xa+a 108 (9) a+xa+a 108 (10) a=0,-a,a+=(a+)-a (10) a=0,-a,a+=(a+)-a 表达式结果为表达式结果为-1-1,a a 的值为的值为 0 0 2.8 2.8 设设 i i 和和 j j 是是
38、intint 类型,类型,a a 和和 b b 是是 doubledouble 类型,以下表达式哪些是类型,以下表达式哪些是错误的,为什么?错误的,为什么? (1) a=b=c (1) a=b=c 错误,错误,c c 未定义且逻辑错误未定义且逻辑错误 (2) a045 (2) a045 正确正确 (3) 7+i*-j/3 (3) 7+i*-j/3 正确正确 (4) 39/-+i-+29%j (4) 39/-+i-+29%j 正确正确 (5) a*+-b (5) a*+-b 错误,错误,+需要左值需要左值 (6) a|bi (6) a|bi 错误,错误, 号左侧类型为号左侧类型为 doubled
39、ouble (7) i*j%a (7) i*j%a 错误,错误,%右侧类型为右侧类型为 doubledouble (8) i/j2 (8) i/j2 正确正确 (9) a+=i+=1+2 (9) a+=i+=1+2 正确正确 (10) int(a+b) (10) int(a+b) 正确,正确,vc+vc+下可运行下可运行 2.9 2.9 下面代码的执行结果是什么?下面代码的执行结果是什么? char a = 1, b = 2, c = 3; char a = 1, b = 2, c = 3; printf(%d,%d,%d,%dn,sizeof(c), sizeof(a), sizeof(c=
40、a), printf(%d,%d,%d,%dn,sizeof(c), sizeof(a), sizeof(c=a),sizeof(a+b+7.7);sizeof(a+b+7.7);结果:结果:1,4,1,81,4,1,8 2.10 2.10 设变量说明为:设变量说明为: unsigned short x = 1, y = 2, z = 4, mask = 0 xc3, w; unsigned short x = 1, y = 2, z = 4, mask = 0 xc3, w; short v; short v;请给出以下表达式的值。请给出以下表达式的值。 (1) xx 0 (1) xx 0
41、(2) v=x -2 (2) v=x -2 (3) w=xx 65535 (3) w=xx 65535 (4) x|yx|z 5 (4) x|yx|z 5 (5) w=y|z,(w3)+(w1) 60 (5) w=y|z,(w3)+(w1) 60 (6) w=x|yx|zymaskx 113 (6) w=x|yx|zymaskx 113 (7) v=-1,v=1-2 (7) v=-1,v=1-2 (8) v=x|x -1 (8) v=x|x -1 (9) w=xy 65532 (9) w=xy 65532 (10) x|y|z2 3 (10) x|y|z2 3 2.11 2.11 写一个表达式
42、,将整数写一个表达式,将整数 k k 的高字节作为结果的低字节,整数的高字节作为结果的低字节,整数 p p的低字节作为结果的高字节,拼成一个新的整数。的低字节作为结果的高字节,拼成一个新的整数。表达式为:表达式为:3232 位位 k 24 | (k 2558)8 | (p 25516)8 | p 24 k 24 | (k 2558)8 | (p 25516)8 | p 24 16 16 位位 (k 8) | (p 8) (k 8) | (p 8) 2.12 2.12 写一个表达式,将整数写一个表达式,将整数 x x 向右循环移位向右循环移位 n n 位。位。表达式为:表达式为:3232 位位
43、x(32-(n%32) | x(n%32) x(32-(n%32) | x(n%32) 16 16 位位 x(16-(n%16) | x(n%16) x(16-(n%16) | x(n%16)2.132.13 写一个表达式,将整数写一个表达式,将整数 x x 从第从第 p p 位开始的向右位开始的向右 n n 位位p p 从右至从右至左编号为左编号为 015015翻转即翻转即 1 1 变变 0,00,0 变变 1 1,其余各位保持不变。,其余各位保持不变。表达式为:表达式为: x(0)(p+1-n) (unsigned short)0)(16-p-1) x(0)(p+1-n) (unsigne
44、d short)0)(16-p-1)或:或:x(0(16-n)(p+1-n)x(0(16-n)(p+1-n) 2.15 2.15 表达式表达式 v = (v-1)v = (v-1)能实现将能实现将 v v 最低位的最低位的 1 1 翻转。比方翻转。比方 v=108v=108,其,其二进制表示为二进制表示为 0110110001101100,则,则 v = (v-1)v = (v-1)的结果是的结果是 0110100001101000。用这一。用这一方法,可以实现快速统计方法,可以实现快速统计 v v 的二进制中的二进制中 1 1 的位数,只要不停地翻转的位数,只要不停地翻转 v v的二进制数的
45、最低位的的二进制数的最低位的 1 1,直到,直到 v v 等于等于 0 0 即可。请用该方法重写例即可。请用该方法重写例2-182-18。 #includestdio.h #includestdio.h int main(void) int main(void) unsigned char data, backup, t = 0; unsigned char data, backup, t = 0; int parity = 0; int parity = 0; data = getchar(); data = getchar(); backup = data; backup = data;
46、while(data) while(data) t+; t+; data = (data -1); data = (data -1); data = backup | (parity7)(t7); data = backup | (parity7)(t7); printf(the data is %#xn, backup); printf(the data is %#xn, backup); printf(parity-check code is %#xn, data); printf(parity-check code is %#xn, data); return 0; return 0;
47、2.16 2.16 写一个表达式,其结果是写一个表达式,其结果是 a a、b b 和和 c c 这这 3 3 个数中最大的一个。个数中最大的一个。表达式为:表达式为: ab?ac?a:c:bc?b:c ab?ac?a:c:bc?b:c 或或 (ab)?(ac?a:c):(bc?b:c) (ab)?(ac?a:c):(bc?b:c) 或或 (ab)?(ac)?a:c):(bc)?b:c) (ab)?(ac)?a:c):(bc)?b:c) 2.18 2.18 写一个表达式,如果整数写一个表达式,如果整数 a a 能被能被 3 3 整除且个位数字是整除且个位数字是 5 5,则结,则结果为非果为非 0
48、 0,否则为,否则为 0 0。 表达式为:表达式为: a%3 ? 0 : (a%10=5 ? 1 : 0) a%3 ? 0 : (a%10=5 ? 1 : 0) 2.19 2.19 定义一个枚举类型定义一个枚举类型 enum monthenum month,用来描述一年,用来描述一年 1212 个月:一个月:一月月janjan、二月、二月febfeb、十二月、十二月decdec,并编写一个程序,并编写一个程序,根据用户输入的年份,输出该年各月的英文名及天数。根据用户输入的年份,输出该年各月的英文名及天数。#includestdio.h#includestdio.h enum year jan,
49、 feb, mar, apr, may, jun, jul, aug, sep, oct, nov, enum year jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov,dec;dec; int main(void) int main(void) 2.20 2.20 设变量说明为:设变量说明为:float a; double b; char c; int x;float a; double b; char c; int x; 试将以下表试将以下表达式中隐含的类型转换改为用强制类型转换运算符转换的表达式。达式中隐含的类型转换改为用强制类
50、型转换运算符转换的表达式。 (1) x = (int)(a - (float)(int)c + a) (1) x = (int)(a - (float)(int)c + a) (2) b * (double)x + (double)( (int)c - (int)0 ) (2) b * (double)x + (double)( (int)c - (int)0 ) (3) ( x 0 ) ? (double)a : b (3) ( x 0 ) ? (double)a : b enum yaer month; int year_num, year_days=365; char enum yaer