好文档 - 专业文书写作范文服务资料分享网站

c语言程序设计教程第二版课后答案

天下 分享 时间: 加入收藏 我要投稿 点赞

c 语言程序设计教程第二版课后答案

【篇一: c 语言程序设计 (第 2 版)-- 课后题答案】

p> 参考答案

第 1 章 进入 c 语言程序世界 1.i love china! printf(we are students.\\n) 2.6 项目实训题参考答案

1 .编写一个 c 程序,输出以下信息: i ******************** main() am a student!

{ printf(* ***************** **\\n); printf( i am a student!\\n);

printf( ******************** \\n);

2.已知立方体的长、宽、高分别是 10cm 序,求立方体体积。 解:

main()

{

int a,b,c,v; a=10; b=20; c=15; v=a*b*c;

printf(v=%d,v);

}

本程序运行结果为

v=3000

第 2 章 编制 c 程序的基础知识 一 选择题 c b a b a c c

二 操作题 ,2,-8,2

3.000000 ,2.500000 , -8.000000 2. abc de fgh why is21+35equal 52 3. 3 4 2

20cm 15cm 、、,编写程

1 4. aa a

项目实训题

1 .定义一个符号常量 m 为 5 和一个变量 n 值为 2,把它们的乘积 输出。

#define m 5 main() { int n,c;

n=2; c=m*n; printf(%d\\n,c);}

2 .编程求下面算术表达式的值。

(1) x+a%3*(int)(x+y)%2/4 ,设 x=2.5,a=7,y=4.7;

(2) (float)(a+b)/2+(int)x%(int)y ,设 a=2,b=3,x=3.5,y=2.5 。 (1) main() { int a=7;

float x=2.5,y=4.7;

printf(%f\\n,x+a%3*(int)(x+y)%2/4);} (2) main() { int a=2,b=3; float x=3.5,y=2.5;

printf(%f\\n,(float)(a+b)/2+(int)x%(int)y);} 第三章 顺序结构程序设计

一 选择题 a c d c c 二 操作题 1. x=3,a=2,b=3 2. z=12.700000

2 1 3 3 2 bb cc abc n 3. 1 2 1 a

2 1 2 三.编程题 编程题 解: #include stdio.h main() {

float sj,gz,yfgz; printf(time,salary:); scanf(%f,%f,sj,gz); yfgz=sj*gz*0.9;

printf(total salary:%f\\n,yfgz);

} 本程序运行结果为 : time,salary:4,3cr total salary:10.800000 2 .编写一个程序求出任意一个输入字符的 ascii 码 解:

#include stdio.h main() {

char c; printf(input a string:);

scanf(%c,c);

printf(%c ascii is %d\\n,c,c); }

本程序运行结果为 : input a string:acr a ascii is 97 3 、编写一个程序用于水果店售货员算帐:已知苹果每斤 鸭2.50 元, 梨每斤 1.80 元,香蕉每斤 2 元,橘子每斤 1.6 元, 水果要求输入各类 的重量,打印出应付

3 解: main() { float p,y,x,j,ys,g,fk;

printf(apple,pear,banana,orange(weight)=);

scanf(%f,%f,%f,%f,p,y,x,j); ys=2.5*p+1.8*y+2*x+1.6*j; printf(fu kuan=); scanf(%f,g); fk=g-ys; printf(result:\\n);

printf(fukuan=%6.2fyuan\\nshoukuan=%6.2fyuan\\nzhaohui=%6. 2fyuan\\n,g,ys,fk);

} 本程序运行结果为 : apple,pear,banana,orange(weight)=1,2,3,4 fu kuan=100

result: fukuan=100.00yuan shoukuan= 18.50yuan zhaohui= 81.50yuan 项目实训

1 .假设银行定期存款的年利率 rate 为 2.25% ,并已知存款期为 n 年,存款本金为 capital 元,试编程计算 n 年后可得到本利之和 deposit 。 #includemath.h main() { int n;

float rate=0.0225,capital,deposit; scanf(%d,%f,n,capital); deposit=capital*pow(1+rate,n); printf(deposit=%f\\n,deposit); }

2 .将一个三位数整数,正确分离出它的个位、十位和百位数字,并 分别在屏幕上输出。

main() { int n,a,b,c; scanf(=,n); a=n/100;

4 b=n0/10; c=n0/1;

printf(a=%d,b=%d,c=%d\\n,a,b,c); } 第四章 选择结构程序设计 一、略 二、 b b a b c b a 三、 1. 1 0 2. 2 3 2 2 3. 10 20 0

4. ch= ' a' ch='z'||ch= 'a'ch='z' ch='0'ch='9' ch==' '

5. -1

四、上机操作

1. 从键盘输入一个英文字母,如果是大写字母,则将它变为小写字 母输出;如果是小写字母,则将其变为大写字母输出。 #includestdio.h main() {char ch; ch=getchar(); if(ch=ach=z) ch+=32;

else if(ch=ach=z) ch-=32; putchar(ch); putchar(\\n); }

2. 根据输入的 x 值依据下列表达式,计算 y 的值。 4+x (x-1) 解: main() {

float x,y; scanf(%f,x); 5

【篇二:《 c 语言程序设计教程 (第二版 )》习题答案】

txt> 一、单项选择题 (第 23 页)

1-4.cbbc 5-8.daca 二、填空题 (第 24 页 )

1. 判断条件 2.面向过程编程 3. 结构化 4. 程序 5. 面向对象的程序设计 语言 7.有穷性 8.直到型循环 9.算法 10.可读性 11.模块化 12. 对问题 的分析和模块的划分

三、应用题 (第 24 页 ) 2. 源程序: main()

{int i,j,k; /* i: 公鸡数 ,j: 母鸡数 ,k: 小鸡数的 1/3 */ printf(cock hen chick\\n);

for(i=1;i=20;i++) for(j=1;j=33;j++) for(k=1;k=33;k++)

if (i+j+k*3==100i*5+j*3+k==100) printf( %d %d %d\\n,i,j,k*3);} 执行结果: cock hen chick 4 18 78 8 11 81 12 4 84

3. 现计算斐波那契数列的前 20 项。 递推法 源程序: main() {long a,b;int i; a=b=1;

for(i=1;i=10;i++) /* 要计算前 30 项,把 10 改为 15 。*/ {printf(%8ld%8ld,a,b);

a=a+b;b=b+a;}} 递归法 源程序: main()

{int i; for(i=0;i=19;i++) printf(?,fib(i));} fib(int i) {return(i=1?1:fib(i-1)+fib(i-2));} 执行结果: 1 1 2 3 5 8 13 21 34 55

89 144 233 377 610 987 1597 2584 4181 6765 4. 源程序: #include math.h;

main() {double x,x0,deltax; do {x0=pow(x+1,1./3); deltax=fabs(x0-x); x=x0; }while(deltax1e-12); printf(%.10f\\n,x);} 执行结果: 1.3247179572

5. 源程序略。 (分子、分母均构成斐波那契数列 ) 结果是 32.66026079864 6. 源程序: main()

{int a,b,c,m; printf(please input a,b and c:);

scanf(%d %d %d,a,b,c); if(ab){m=a;a=b;b=m;} if(ac){m=a;a=c;c=m;} if(bc){m=b;b=c;c=m;} printf(%d %d %d\\n,a,b,c);} 执行结果: please input a,b and c:123 456 789 789 456 123

7. 源程序: main() {int a;

scanf(%d,a); printf(a!==0?yes:no);} 执行结果: 42 yes

3 第 2 章 c 语言概述

一、单项选择题 (第 34 页 ) 1-4.bdcb 5-8.aabc (第 35 页)

1. 主 2.c 编译系统 3.函数 函数 4.输入输出 5.头 6. .obj 7. 库函数 8. 文本

三、应用题 (第 36 页 )

5.sizeof 是关键字, stru 、 _aoto 、 file 、 m_i_n 、hello 、 abc 、 sin90 、 x1234 、 until 、cos2x 、 s_3 是标识符。 8. 源程序: {int a,b,c; scanf(%d %d,a,b); c=a;a=b;b=c; printf(%d %d,a,b);} 执行结果: 12 34 34 12

4 第 3章 数据类型与运算规则 一、单项选择题 (第 75 页 )

1-5.dbacc 6-10.dbdbc 11-15.adccc 16-20.cbccd 21-25.addbc 26-27.ab

二、填空题 (第 77 页 ) 8.65,89

三、应用题 (第 78 页 ) 1.10 9 2. 执行结果: 11 12 1

5 第 4章 顺序结构程序设计 一、单项选择题 (第 90 页 ) 1-5.dcdad 6-10.bacbb

二、填空题 (第 91 页 )

1.一 ;2. 5.169000 3.(1)-2002500 (2)i=-200,j=2500 (3)i=-200 j=2500 4.a=98,b=765.000000,c=4321.000000 5. 略 6.0,0,3 7.3 8.scanf(%lf%lf%lf,a,b,c); 9. 13 13.000000,13.000000 10.a=aAc;c=cAa;a=aAc;( 这种算法不破坏 b的值,也不用定义中间 变量。 )

三、编程题 (第 92 页 )

1 .仿照教材第 27 页例 2-1。 2. 源程序: main()

scanf(%d:%d,h,m); printf(%d\\n,h*60+m);} 执行结果: 9:23 563

3. 源程序 : main()

{int a[]={-10,0,15,34},i; for(i=0;i=3;i++)

printf(%d\\370c=%g\\370f\\t,a[i],a[i]*1.8+32);}

SSH9.9215674165 456 e-seprinff(SSH%if宀 main

p「inff(dafa」(a+bca+cbb+ca)doub-e。

a

b

peiroro-f\\psqrt(s*(s宀

一)—a)*(sdoub-escanf(%」

雪—m

>br(s—>-

c)))SH(a+b+c)/2-f%-f%-fQ)bc)八 」

=ttinc-ude5?亍雪a宀5main雪3mafh.m 昭 mH 爺>%doubCD。 >弔>?>一-

p-

aH31.4 10-f 」\59265359

3SHW? 一14

」SH49.3480220054 o-f\\nr

2*p^prpis59265358979 「p」inff( 「H%-g

main宀10? 。爺?

7mainscanf(mH宀inf「雪567 675 8main?。爺m宀infpscanf(and r>7r—s^。inf %d%d is

%d

%.2f\\n0)bc

% d Q)

?b

(a+b+c)/3) c =

p

「 inff(ave 「 age

of

%d ? %

d

a昭>弔>b-

-

p

inff(a%a

dH b%d-

b%dvlQ)bc)HCoH」m -

%d

%d abcm %dQ)bc= 昭4弔 3-4. f

5*

6

,n八0)

b

c

CL

e

prinff(aH:bH%

—f}」 4doH$%2ndH%9neH%6.2f\\n7r%—

^

a^Hi

3

b

H

4

o

H

5

i

o

a

f dA2eH2.23

>-

6 7 9

average of 6,7 and 9 is 7.33

9. 不能。修改后的源程序如下: main()

{int a,b,c,x,y; scanf(%d %d %d,a,b,c); x=a*b;y=x*c;

printf(a=%d,b=%d,c=%d\\n,a,b,c); printf(x=%d,y=%d\\n,x,y);}

6 第 5章 选择结构程序设计 一、单项选择题 (第 113 页 ) 1-4.dcbb 5-8.dabd

二、填空题 (第 115 页) 1. 非 0 0 2.k==0

3.if (abs(x)4) printf(%d,x);else printf(error!); 4.if((x=1x=10||x=200x=210)x1)printf(%d,x);

5. k=1 (原题最后一行漏了个 d,如果认为原题正确,则输出k=%。) 6. 8! right!11 7.$$$a=0 8.a=2,b=1 三、编程题 (第 116 页)

1. 有错。正确的程序如下: main() {int a,b,c; scanf(%d,%d,%d,a,b,c); printf(min=%d\\n,ab?bc?c:b:ac?c:a);}

2. 源程序: main() {unsigned long a; scanf(%ld,a); for(;a;printf(%d,a),a/=10);} 执行结果: 12345 54321

3. ( 1 )源程序:

【篇三: c 语言程序设计现代方法 (第二版 ) 习题答案】

answers to selected exercises

2. [was #2] (a) the program contains one directive (#include) and four statements (three calls of printf and one return). (b)

parkinsons law: work expands so as to fill the time available for its completion.

3. [was #4] #include stdio.h int main(void) { int height = 8, length = 12, width = 10, volume; volume = height * length * width;

printf(dimensions: %dx%dx%d\\n, length, width, height); printf(volume (cubic inches): %d\\n, volume);

printf(dimensional weight (pounds): %d\\n, (volume + 165) / 166); return 0; }

4. [was #6] heres one possible program:

#include stdio.h int main(void) { int i, j, k;

float x, y, z; printf(value of i: %d\\n, i); printf(value of j: %d\\n, j); printf(value of k: %d\\n, k); printf(value of x: %g\\n, x); printf(value of y: %g\\n, y); printf(value of z: %g\\n, z); return 0; }

when compiled using gcc and then executed, this program produced the following output:

value of i: 5618848 value of j: 0 value of k: 6844404 value of x:

3.98979e-34 value of y: 9.59105e-39 value of z: 9.59105e-39 the values printed depend on many factors, so the chance that youll get exactly these numbers is small.

5. [was #10] (a) is not legal because 100_bottles begins with a digit. 8. [was #12] there are 14 tokens: a, =, (, 3, *, q, -, p, *, p, ), /, 3, and ;. answers to selected programming projects 4. [was #8; modified] #include stdio.h int main(void)

{

float original_amount, amount_with_tax; printf(enter an amount: ); scanf(%f, original_amount);

amount_with_tax = original_amount * 1.05f; printf(with tax added: $%.2f\\n, amount_with_tax); return 0;

the amount_with_tax variable is unnecessary. if we remove it, the program is slightly shorter: #include stdio.h int main(void)

{

float original_amount; printf(enter an amount: ); scanf(%f, original_amount);

printf(with tax added: $%.2f\\n, original_amount * 1.05f); return 0;

}

chapter 3 answers to selected exercises 2. [was #2] (a) printf(%-8.1e, x); (b) printf(.6e, x); (c) printf(%-8.3f, x); (d) printf(%6.0f, x);

5. [was #8] the values of x, i, and y will be 12.3, 45, and .6, respectively. answers to selected programming projects 1. [was #4; modified] #include stdio.h int main(void)

{

int month, day, year; printf(enter a date (mm/dd/yyyy): ); scanf(%d/%d/%d, month, day, year);

printf(you entered the date %d%.2d%.2d\\n, year, month, day); return 0; }

3. [was #6; modified] #include stdio.h int main(void) {

int prefix, group, publisher, item, check_digit; printf(enter isbn: );

scanf(%d-%d-%d-%d-%d, prefix, group, publisher, item, check_digit); printf(gs1 prefix: %d\\n, prefix); printf(group identifier: %d\\n, group); printf(publisher code: %d\\n, publisher); printf(item number: %d\\n, item); printf(check digit: %d\\n, check_digit);

/* the five printf calls can be combined as follows: printf(gs1 prefix: %d\\ngroup identifier: %d\\npublisher code: %d\\nitem number: %d\\ncheck digit: %d\\n, prefix, group, publisher, item, check_digit); */

return 0;

} chapter 4 answers to selected exercises

2. [was #2] not in c89. suppose that i is 9 and j is 7. the value of (-i)/j could be either — or —., depending on the implementation. on the other hand, the value of -(i/j) is always —,regardless of the impleme ntati on. in c99, on the other hand, the value of (-i)/j must be equal to the value of -(i/j).

9. [was #6] (a) 63 8 (b) 3 2 1 (c) 2 -1 3 (d) 0 0 0

13. [was #8] the expression ++i is equivalent to (i += 1). the value of both expressions is i after the increment has been performed. answers to selected programming projects 2. [was #4] #include stdio.h int main(void)

{ int n; printf(enter a three-digit number: ); scanf(%d, n);

printf(the reversal is: %d%d%d\\n, n % 10, (n / 10) % 10, n / 100); return 0;

} chapter 5 answers to selected exercises 2. [was #2] (a) 1 (b) 1

(c) 1 (d) 1

4. [was #4] (i j) - (i j)

6. [was #12] yes, the statement is legal. when n is equal to 5, it does nothing, since 5 is not equal to -9. 10. [was #16] the output is onetwo

since there are no break statements after the cases. answers to selected programming projects 2. [was #6]

c语言程序设计教程第二版课后答案

c语言程序设计教程第二版课后答案【篇一:c语言程序设计(第2版)--课后题答案】p>参考答案第1章进入c语言程序世界1.ilovechina!printf(wearestudents.\\n)2.6项目实训题参考答案1.编写一个c程序,输出以下信息:i*************
推荐度:
点击下载文档文档为doc格式
0hz7b1l1kh2mdyx423a46cyp27lzc201brh
领取福利

微信扫码领取福利

微信扫码分享