



版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、c 语言程序设计教程第二版课后答案【篇一: c 语言程序设计 (第 2 版)- 课后题答案】p> 参考答案第 1 章 进入 c 语言程序世界二、1.i love china!printf(we are students.n)2.6项目实训题参考答案1编写一个 c 程序,输出以下信息:* *i am a student!*main() printf(*n); printf( i am a student!n);printf(*n);2已知立方体的长、宽、高分别是 10cm 、 20cm 、15cm ,编写程序,求立方体体积。 解:main()int a,b,c,v;a=10;b=20;c=
2、15;v=a*b*c;printf(v=%d,v);本程序运行结果为:v=3000第 2 章 编制 c 程序的基础知识一 选择题c b a b a c c二 操作题, 2,-8,23.000000 ,2.500000 , -8.0000002. abc de fghwhy is21+35equal 523.34214. aaa项目实训题1 定义一个符号常量 m 为 5 和一个变量 n 值为 2,把它们的乘积输出。#define m 5main() int n,c; n=2; c=m*n; printf(%dn,c);2编程求下面算术表达式的值。(1) x+a%3*(int)(x+y)%2/4,
3、设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(%fn,x+a%3*(int)(x+y)%2/4);( 2) main() int a=2,b=3; float x=3.5,y=2.5; printf(%fn,(float)(a+b)/2+(int)x%(int)y);第三章 顺序结构程序设计一 选择题a c d c c二 操作题1. x=3,a=2,b=32. z=12.7000002 1 3 3 2 bb cc
4、 abc n3. 121a2 1 2三编程题编程题解:#include stdio.hmain()float sj,gz,yfgz;printf(time,salary:);scanf(%f,%f,sj,gz);yfgz=sj*gz*0.9;printf(total salary:%fn,yfgz);本程序运行结果为:time,salary:4,3crtotal salary:10.8000002 编写一个程序求出任意一个输入字符的ascii码解:#include stdio.hmain()char c;printf(input a string:);scanf(%c,c);printf(%c
5、 ascii is %dn,c,c);本程序运行结果为:input a string:acra ascii is 973 、编写一个程序用于水果店售货员算帐:已知苹果每斤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
6、;printf(result:n);printf(fukuan=%6.2fyuannshoukuan=%6.2fyuannzhaohui=%6.2fyuann,g,ys,fk);本程序运行结果为:apple,pear,banana,orange(weight)=1,2,3,4fu kuan=100result:fukuan=100.00yuanshoukuan= 18.50yuanzhaohui= 81.50yuan项目实训1假设银行定期存款的年利率 rate 为 2.25% ,并已知存款期为 n 年,存款本金为 capital 元,试编程计算 n 年后可得到本利之和deposit 。#inc
7、ludemath.hmain() int n;float rate=0.0225,capital,deposit;scanf(%d,%f,n,capital);deposit=capital*pow(1+rate,n);printf(deposit=%fn,deposit); 2将一个三位数整数,正确分离出它的个位、十位和百位数字,并分别在屏幕上输出。main() int n,a,b,c; scanf(%3d,n); a=n/100;4b=n%100/10;c=n%100%10/1;printf(a=%d,b=%d,c=%dn,a,b,c); 第四章 选择结构程序设计一、略二、 b b a b
8、 c b a三、1. 1 02. 23223. 1020 04. ch= ach=z|ch= ach=z ch=0ch=9ch= 5. -1四、上机操作1. 从键盘输入一个英文字母,如果是大写字母,则将它变为小写字母输出;如果是小写字母,则将其变为大写字母输出。#includestdio.hmain()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;scan
9、f(%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 chickn);for(i=1;i=20;i
10、+)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 %dn,i,j,k*3);执行结果:cock hen chick4187881181124843. 现计算斐波那契数列的前 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(
11、%8d,fib(i);fib(int i)return(i=1?1:fib(i-1)+fib(i-2);执行结果:89 144 233 377 610 987 1597 2584 4181 67654. 源程序:#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(%.10fn,x);执行结果:1.32471795725. 源程序略。 (分子、分母均构成斐波那契数列 )结果是 32.660260798646. 源程序:main()
12、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 %dn,a,b,c);执行结果:please input a,b and c:123 456 789789 456 1237. 源程序:main()int a;scanf(%d,a);printf(a%21=0?yes:no);执行结果:42yes3 第 2 章 c 语言概述一、单项选择题 (第 34 页 )1-4.bdcb 5-8
13、.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 3434 124 第 3 章 数据类型与运算规则一、单项选择题 (第 75 页 )1-5.dbacc 6-
14、10.dbdbc 11-15.adccc 16-20.cbccd 21-25.addbc26-27.ab二、填空题 (第 77 页 )8.65,89三、应用题 (第 78 页 )1.10 92. 执行结果:111215 第 4 章 顺序结构程序设计一、单项选择题 (第 90 页 )1-5.dcdad 6-10.bacbb二、填空题 (第 91 页 )1. 一 ;2. 5.169000 3.(1)-2002500 (2)i=-200,j=2500 (3)i=-200j=2500 4.a=98,b=765.000000,c=4321.000000 5.略 6.0,0,3 7.38.scanf(%l
15、f%lf%lf,a,b,c); 9. 13 13.000000,13.00000010.a=ac;c=ca;a=ac;(这种算法不破坏b 的值,也不用定义中间变量。 )三、编程题 (第 92 页 )1. 仿照教材第 27 页例 2-1。2. 源程序:main()scanf(%d:%d,h,m);printf(%dn,h*60+m);执行结果:9:235633. 源程序 : main()int a=-10,0,15,34,i; for(i=0;i=3;i+) printf(%d370c=%g370ft,ai,ai*1.8+32);执行结果:4. 源程序: main()printf(r=%lg a
16、=%.10lf s=%.10lfn,r,2*pi*r,pi*pi*r);执行结果:r=5 a=31.4159265359 s=49.34802200545. 源程序:#include math.h;main()double a,b,c;scanf(%lf%lf%lf,a,b,c);if (a+bca+cbb+ca)double s=(a+b+c)/2;printf(ss=%.10lfn,sqrt(s*(s-a)*(s-b)*(s-c);else printf(data error!);执行结果:4 5 6ss=9.92156741656. 源程序:main()int a=3,b=4,c=5;f
17、loat d=1.2,e=2.23,f=-43.56; printf(a=%3d,b=%-4d,c=*%dnd=%gne=%6.2fnf=%-10.4f*n,a,b,c,d,e,f);7. 源程序: main()int a,b,c,m;scanf(%d %d %d,a,b,c); m=a;a=b;b=c;c=m; printf(%d %d %dn,a,b,c);执行结果:5 6 76 7 58.源程序:int a,b,c;scanf(%d %d %d,a,b,c);printf(average of %d,%d and %d is %.2fn,a,b,c,(a+b+c)/3.);执行结果:6
18、7 9average of 6,7 and 9 is 7.339. 不能。修改后的源程序如下: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=%dn,a,b,c);printf(x=%d,y=%dn,x,y);6 第 5 章 选择结构程序设计一、单项选择题 (第 113 页)1-4.dcbb 5-8.dabd二、填空题 (第 115 页)1. 非 0 0 2.k=03.if (abs(x)4) printf(%d,x);else printf(error!);4.if(x=1x=10|x=200x
19、=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=%dn,ab?bc?c:b:ac?c:a);2. 源程序:main()unsigned long a;scanf(%ld,a);for(;a;printf(%d,a%10),a/=10);执行结果:12345543213.(1) 源程序:【篇三: c 语
20、言程序设计现代方法(第二版 )习题答案】answers to selected exercises2. 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 timeavailable for its completion.3. was #4 #include stdio.h int main(void)int heigh
21、t = 8, length = 12, width = 10, volume; volume = height * length * width; printf(dimensions: %dx%dx%dn, length, width, height); printf(volume (cubic inches): %dn, volume); printf(dimensional weight (pounds): %dn, (volume + 165) /166);return 0;4. was #6 heres one possible program: #include stdio.hint
22、 main(void)int i, j, k; float x, y, z;printf(value of i: %dn, i); printf(value of j: %dn, j); printf(value of k: %dn, k); printf(value of x: %gn, x); printf(value of y: %gn, y);printf(value of z: %gn, z);return 0;when compiled using gcc and then executed, this program produced the following output:
23、value of i: 5618848value of j: 0value of k: 6844404value of x: 3.98979e-34value of y: 9.59105e-39value of z: 9.59105e-39the 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 #1
24、2 there are 14 tokens: a, =, (, 3, *, q, -, p, *, p, ), /, 3, and ;.answers to selected programming projects4. was #8; modified #include stdio.hint main(void)float original_amount, amount_with_tax; printf(enter an amount: );scanf(%f, original_amount);amount_with_tax = original_amount * 1.05f; printf
25、(with tax added: $%.2fn, 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: $%.2fn, original_amount
26、 * 1.05f); return 0;chapter 3answers to selected exercises2. was #2(a) printf(%-8.1e, x);(b) printf(%10.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 projects1. was #4; modified #include s
27、tdio.hint 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%.2dn, year, month, day);return 0;3. was #6; modified #include stdio.hint main(void)int prefix, group, publisher, item, check_digit; printf(enter isbn: );s
28、canf(%d-%d-%d-%d-%d, prefix, group, publisher, item, check_digit);printf(gs1 prefix: %dn, prefix); printf(group identifier: %dn, group); printf(publisher code: %dn, publisher); printf(item number: %dn, item); printf(check digit: %dn, check_digit);/* the five printf calls can be combined as follows: printf(gs1 prefix: %dngroup identifier: %dnpublishercode: %dnitem number: %dncheck digit: %dn, prefix, group, publisher, item, check_digit);*/return 0;chapter 4answers to selected exercises2. was #2 not in c89. suppose that i is 9 and j is 7.
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 二零二五年度办事处环境保护责任协议
- 知识产在办公自动化中的运用及挑战分析报告
- 2025-2030年中国玻璃包装行业竞争格局及前景趋势预测报告
- 2025-2030年中国烯虫酯市场发展趋势规划研究报告
- 2025-2030年中国火车站独立货场仓储产业市场运行状况及发展前景分析报告
- 2025-2030年中国涡轮增压行业十三五规划及发展潜力分析报告
- 2025-2030年中国活塞销行业运行状况及前景趋势分析报告
- 社交网络在商业决策中的价值与影响
- 2025-2030年中国氨基酸输液医院用药行业运行状况与发展趋势分析报告
- 2025-2030年中国橡胶制品行业供需规模规划分析报告
- 故障检测报告表
- 日常零星项目维修项目清单
- 心脏解剖演示文稿
- GB∕T 28575-2020 YE3系列(IP55)三相异步电动机技术条件(机座号63~355)
- 2022医院设备科工作制度
- 【23精品】苏少小学美术三下教案全册
- 房屋租赁(出租)家私清单
- 仓储货架ppt课件
- 《保健按摩师》(五级)理论知识鉴定要素细目表
- 陈日新腧穴热敏化艾灸新疗法上篇
- PID烙铁恒温控制器设计与制作_图文
评论
0/150
提交评论