




已阅读5页,还剩32页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
C+语言程序设计习题参考答案教材名称 :C+语言程序设计主 编 :李宁出 版 社 :中央广播电视大学出版社版 本 :2000年7月第1版第一章 第一章 C+语言概述自测题 P2 1.1-1 C P8 1.2-1 形式参数表 1.2-2 main 1.2-3 ; 1.2-4 1.2-5 main 1.2-6 显示器 输出流 1.2-7 键盘 输入流 1.2-8 头文件 1.2-9 int f( );1.2-10 124 1.2-11 1.2-12 1.2-13 1.2-14 1.2-15 1.2-16 1.2-17 ABCFG 1.2-18 D1.2-19 D P11 1.3-1 OBJ 1.3-2 source.cpp习题一 (P12)1-1 1C 2类型修饰符 3main 4语句以分号(;)结束 5 6 8.h 9source.obj 10Main 11char * Hi(const char *); 12331-2 1 2 3 4 5 6 7 81-3 1ABFI 2D 3ABCD 4B1-4 # include void main( ) cout ”C+语言程序设计”endl; 1-5 # include void main() int i,j,k;cout ijk;cout endli”+”j”+”k”=”i+j+ky+3 2.8-2 x!=3 & x=-3 2.8-3 B2.8-4 C 2.8-5 BC 2.8-6 AD2.8-7 D 2.8-8 C 2.8-9 A 2.8-10 CD 2.8-11 D 2.8-12 B 2.8-13 !isMale Age=55 Age=50 & isMale Age100?100:0); 2. 7 3. 17 4. D:MyDirMyFile.Dat 5. C F 6. 48+n U 或 char(48+n) U 或 n+0 U 7. Jun thisMonth+1 ( 说明:VC+不支持枚举数据参加算术运算,此例无法编译)8. ( 说明:题目有错,cin m; 编译出错,C+不支持枚举数据输入)9. 12.3456 9.87653e3 10. 3.66667 4.0 411. 57 45.0 G 12. 92 13. Whats your name? 14. 7715. 2 44 3 16. 7 4 317. 3 1 18. 6 1019. 3 7 20. 5 221. f=+p+5; 22. f=p-5;23. x+y=3 24. x=-525. a*a*a+3*a*a*a*b+3*a*b*b+b*b*b 26. 1.0/2*a*sqrt(b+2*c)/5) 27. IsMale Age30 Age=50|IsMale 2-3 2-4 1. B 2. A 3. AB 4. B 5. A 6. ABCD 7. CEFHI 8. CEFGHJ 9. C 10. C 11. A 12. AB 13. C2-5 / P50 习题2-5 # include # include void main() double a,r; cout endla; r=1.0/2*a*sqrt(10+2*sqrt(5)/5); cout endl正五边形外接圆的半径:rendl;2-6 / P50 习题2-6# include # include void main()double a,b,c,area,s;cout endlabc;s=(a+b+c)/2;area=sqrt(s*(s-a)*(s-b)*(s-c); cout endl三角形的面积:areaendl; 2-7 / P50 习题2-7# include # include void main()double x1,y1,z1,x2,y2,z2,d;cout endlx1y1z1;cout endlx2y2z2;d=sqrt(x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)+(z2-z1)*(z2-z1); cout endl两点间的距离:dendl;第三章 第三章 C+程序的流程控制自测题 P52 3.1-1 顺序 条件分支 循环 只有一个入口和一个出口 P65 3.2-1 false 3.2-2 AB 3.2-3 B 3.2-4 t=x; x=y; y=t; 3.2-5 if (!OK) y=1; else if (READY) y=-1; else y=0;3.2-6 1 2 3 n-1 3.2-7 XYZ 3.2-8 if (k=1 | k=2 | k=3) cout ”A”;else if (k=4 | k=5) cout ”B”; else cout =1 & k=3) cout =4 & k=5) cout ”B”; else cout ”C”;3.2-9 switch (x) case 0: case 1: case 2: cout ”X”; break; case 20: case 21: case 22: cout ”Y”; break; default : cout ”C”; break; ;3.2-10 ABD 3.2-11 AD 3.2-12 BC 3.2-13 BCD P78 3.3-1 28 3.3-2 21 3.3-3 for ( int i=10; i100; i+ ) cout ”P”; 3.3-4 117 3.3-5 i=0; while (i10) cout 0; i- ) cout i; 习题三 (P82)3-1 1. y z或 z y 2. true 3. A 4. A 5. x+=y; y=x-y; x-=y; 6. y=OK?(READY?-1:0):1; 7. ABC 8. XYZ 9. ABC 10. 22 11. 23 12. for (int i=100; i; i-) cout P; 13. 4614. 14. if (k=1 | k=2 ) cout A; else if ( k=3 | k=4 | k=5 ) cout B;else cout =1 & k=2 ) cout =3 & k=5 ) cout B;else cout C; 15. switch (x) case 1: case 2: case 3: cout X; break; case 19: case 20: case 21: case 22: cout Y; break; default : cout Z; break; 16. 133-2 1. ABC 2. ABD 3. C 4. ACD3-3 / P84 习题3-3# include void main()int weekday;cout endlweekday;if (weekday=0)cout Sundayendl;else if (weekday=1)cout Mondayendl;else if (weekday=2)cout Tuesdayendl;else if (weekday=3)cout Wednesdayendl;else if (weekday=4)cout Thursdayendl;else if (weekday=5)cout Fridayendl;else if (weekday=6)cout Saturdayendl;else cout 表示星期几的数必须在0-6之间!endl;3-4 / P84 习题3-4# include void main()int a,b,c,d,max;cout abcd;max=a;if (maxb) max=b;if (maxc) max=c;if (maxd) max=d;cout endl;cout 最大的数是:maxendl;3-5 / P84 习题3-5# include void main()int a,b,c,d,temp;cout abcd;cout 排序前:a b c db) temp=a; a=b; b=temp;if (ac) temp=a; a=c; c=temp;if (ad) temp=a; a=d; d=temp;/ 求第二小数存入bif (bc) temp=b; b=c; c=temp;if (bd) temp=b; b=d; d=temp;/ 求第三小的数存入cif (cd) temp=c; c=d; d=temp;cout 排序后:a b c dendl;3-6 / P84 习题3-6# include void main()cout endl |; / 以下3行显示顶部表栏 int i=1;while (i10)cout setw(3)i;i+;cout endl-+-;i=1;while (i10) / 乘数从1 循环到9cout endlsetw(2)i|;int j=1;while (j10)cout setw(3)i*j; / 被乘数从1 循环到9j+;i+; coutendlendl;3-7 / P84 习题3-7# include void main()int a=1,b=2; double s=0,p;for (int i=1; i=20; i+)p=(double)b/a;s+=p;b+=a; / 产生分子a=b-a; / 产生分母cout 2/1+3/2+5/3+8/5+.=sendl;3-8 / P84 习题3-8# include # define N 15void main()int n; long double s=0.0L,fact=1.0L;for (n=1; n=N; n+) fact*=n; / 计算n! s+=fact;cout 1!+2!+.+N!=sendl;3-9 / P84 习题3-9 / 方法一# include void main()int n1=1,n2=1,n=n1+n2,couter=2; / n1,n2相邻二项的分母,cout统计项数 double s=2.0,s0;while (s0=1.0/n)=0.000001)s+=s0;n1=n2;n2=n;n=n1+n2;couter+; coutendlendl; cout 累加和:sendl; cout 累加项数:couterendl;/ 方法二# include void main()int n=2,d=1,k=2; / k统计项数 double s=2.0,s0;while (s0=1.0/n)=0.000001)s+=s0;n+=d;d=n-d;k+; coutendlendl; cout 累加和:sendl; cout 累加项数:kendl;3-10 / P84 习题3-10# include void main()for (int i=1; i=6; i+)cout endl;int j;for (j=1; ji; j+) cout ;for (j=1; j=13-i-i; j+) cout *;cout endlendl;3-11 / P81 习题3-11# include void main() cout setw(6)*; / 输出顶* for (int i=0; i4; i+)cout endl;int j;for (j=0; j4-i; j+) / 输出载边空格cout ;cout *; / 输出左*for (j=0; ji+i+1; j+) / 输出中间空格cout ;cout *; / 输出右*cout endl*;cout endlendl;3-12 / P81 习题3-12# include void main() int i;cout endl |; for (i=0; i=9; i+)cout setw(6)i;cout endl-+;for (i=0; i=10; i+) cout -;for (i=0; i=9; i+)cout endlsetw(5)i|;for (int j=0; j=9; j+)cout setw(6)(i*10+j)*(i*10+j);cout endlendl;3-13 / P81 习题3-13# include # include void main() double x;cout x;int i=1;double n=-x*x,d=1.0*2.0,s=1.0,s0;while (fabs(s0=n/d)=0.000001)s+=s0;i+;n*=(-x*x);d*=(i+i)*(i+i-1);cout 累加求值 COS(x)=sendl;cout 标准函数 COS(x)=cos(x)endl;cout endlendl;第四章 第四章 数 组自测题 P93 4.1-1 (3,6,9,0,0) (0) (4) 4.1-2 (9,6,3) (0) (2)4.1-3 (100) (0) (99) 4.1-4 (ACD)4.1-5 int j=9 , j=0 ,-Tj 4.1-6 0 ,j10 ,j+ ,s+=TjP99 4.2-1 (5) (3) (15) (4) (2) 4.2-2 (2) (4) (8) (1) (3) (1,2,3,4)4.2-3 (3) (3) (9) (1,2,0,5,0,0,6,7,8) 4.2-4 int g5=1,1,2,1,2,3,1,2,3,4,1,2,3,4,5;4.2-5 0 , 6 , i+ , hii 4.2-6 0 , 5 , i+ , int j=0 , j3 , mij4.2-7 BCDP105 4.3-1 XABCD 4.3-2 net , internet4.3-3 strcpy(s3,s1); strcpy(s1,s2); strcpy(s2,s3);4.3-4 strcat(s3,s1); strcat(s3,s2) ; 或 strcat(strcat(s3,s1),s2)4.3-5 if ( !strcmp(s1,s2) ) cout s1endls20) cout s1endl;else cout s20 ) cout s1endl; else if (strcmp(s1,s2)0) cout s2endl; else cout s1endls2endl;或 if ( strcmp(s1,s2)0 ) cout s20) cout s1endl; else cout s1endls2endl;4.3-6 D 4.3-7 BCD习题四 (P107)4-1 1. ( 3,6,9,12 ) ( 0 ) ( 3 ) 2. ( 9,6,3,0,0,0 ) ( 0 ) ( 5 ) 3. ( 50 ) ( 0 ) ( 49 ) 4. ( int j=0 ) ( j+=2 ) ( aj )5. 5. ( 0 ) ( j=0 ) ( j18 ) ( s+=s0j ) ( /= ) (说明:题目的“并用s保存”改为“并用s0保存”) 6. ( 4 ) ( 11 ) ( 44 ) ( 3 ) ( 10 ) 7. ( 3 ) ( 5 ) ( 15 ) ( 2 ) ( 4 ) ( 1,2,3,0,0 , 5,6,0,0,0 , 8,0,0,0,0 ) 8. ( 4 ) ( 3 ) ( 12 ) ( 1,2,0,5,0,0,6,7,8,0,0,0 ) 9. ( int g55=1,2,3,4,5,1,2,3,4,1,2,3; 10. ( int i=0 ) ( i8 ) ( hii ) 11. ( 0 ) ( 5 ) ( i+ ) ( int j=0 ) ( j3 ) ( m4-ij ) 12. ( “ABCD” ) 13. ( net ) ( in ) 14. ( aaabbbbccccc , bbbbccccc , ccccc ) 15. ( aaa , cccccaaa , ccccc )16. 16. ( abc , abf , abcdabf )4-2 1. BCD 2. AB 3. CD 4. AC4-3 / P109 习题4-3 # include # define SIZE 10void main()int aSIZE; int temp,i,max,min;cout 输入SIZEa0;max=min=0; / 存放最大数和最小数的下标for (i=1; iai;if (aiamax) max=i; / 保存当前最大数的下标if (aiamin) min=i; / 保存当前最小数的下标cout endl交换前:;for (i=0; iSIZE; i+)cout ai ;temp=a0; a0=amax; amax=temp; / 最大数存首元素temp=aSIZE-1; aSIZE-1=amin; amin=temp; / 最小数存末元素 cout endl交换后:;for (i=0; iSIZE; i+)cout ai ;cout endlendl;4-4 / P109 习题4-4 方法一:# include void main()int a=23,25,13,68,3,44,11,7,55,36;const int SIZE=sizeof(a)/sizeof(a0); / 数组a的元素个数 int temp,i;cout endl处理前:;for (i=0; iSIZE; i+)cout ai ;for (i=0; i5; i+) / 找前5大数之一int k=i,j; / k保存大数的下标for (j=5; jak) k=j; temp=ai; ai=ak; ak=temp; / 交换cout endl处理后:;for (i=0; iSIZE; i+)cout ai ;cout endlendl;方法二:/ P109 习题4-4 # include void main()int a=23,25,13,68,3,44,11,7,55,36;const int SIZE=sizeof(a)/sizeof(a0); / 数组a的元素个数 int temp,i;cout endl处理前:;for (i=0; iSIZE; i+)cout ai ;for (i=0; i5; i+) / 用选择排序法排前5个数为大数int k=i,j; / k保存当前最大数的下标for (j=i+1; jak) k=j; temp=ai; ai=ak; ak=temp; / 交换cout endl处理后:;for (i=0; iSIZE; i+)cout ai ;cout endlendl;4-5 / P109 习题4-5# include # define SIZE 20void main()int aSIZE,i; cout endl请输入SIZE个正整数: ;for ( i=0; iai;while (ai=0);cout endl处理前: ;for (i=0; iSIZE; i+) cout ai ;cout endl;int j=0; / j为奇数存放下标 for (i=0; iSIZE; i+)if ( ai%2 ) / 条件成立,表示ai为奇数int temp=ai; ai=aj; aj=temp;j+; / 下一个奇数存放位置cout endl处理后: ;for (i=0; iSIZE; i+) cout ai ;cout endlendl;4-6 / P109 习题4-6# include void main() int a=23,25,13,68,3,44,11,7,55,36;const int SIZE=sizeof(a)/sizeof(a0); / 数组a的元素个数 int i,number; cout endlnumber;cout endlendl整数序列: ;for (i=0; iSIZE; i+) / 显示数组元素 cout ai ;cout endlendl输入数: number的序号: ; for (i=0; iSIZE & ai!=number; i+) ; / 查找 cout (iSIZE)?i+1:0)endl; / 表达式中条件成立,表示找到cout endl;4-7 / P109 习题4-7# include # define SIZE 5void main() int aSIZESIZE;int i,j,sum=0; cout endl请输入SIZE行SIZE列整型数组: endl;for (i=0; iSIZE; i+)for (j=0; jaij;cout endlendlSIZE行SIZE列整型矩阵: ;for (i=0; iSIZE; i+) / 显示二维数组元素cout endl;for (j=0; jSIZE; j+) cout setw(4)aij;for (i=0; iSIZE; i+)for (j=0; jSIZE; j+)if (i=0 | j=0 | i=SIZE-1 | j=SIZE-1)sum+=aij;cout endlendl矩阵四周元素的合计值: sumendlendl;4-8 / P109 习题 4-8# include # include # define SIZE 10void main()double dataSIZESIZE;int i,j,size;cout size;if (size10)cout 行列式的阶数要求在 1-10 之间.endl;return;/ 输入行列式cout 请输入size行size列的行列式:endl;for (i=0; isize; i+)for (j=0; jdataij;/ 输出行列式cout endlendlsize行size列的行列式:endl; for (i=0; isize; i+)cout endl;for (j=0; jsize; j+)cout setw(10)dataij; cout endl;/ 调用函数grmdet()计算行列式的值double grmdet(double aSIZE,int size); cout endlsize行size列的行列式的值为:;cout grmdet(data,size)endl;/ 计算行列式的值函数。以下函数定义参阅1982年12月第1版上海教育出版社出版/ 上海计算技术研究所编电子计算机算法手册第295页算法double grmdet(double aSIZE,int size)int i,j,k,n;double p,t,u;double mSIZE;for (i=0; isize; i+) u=0.0;for (j
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 学校办公室公文处理职责
- 护理分级护理标准执行问题整改措施
- 环保设施设计安全保证措施
- 促销策略效果-洞察及研究
- 2025年护士条例试题库及答案
- 药店新员工培训试卷及答案
- 2025年教科版科学四年级上册教学方法计划
- 2025年主管护师考试模拟试卷及答案
- 2025年红十字初级急救员证考试题及答案
- 数字印刷员协同作业考核试卷及答案
- 光纤通信施工难点措施
- 资质备案管理办法
- 设备维护保养基础知识
- GB/T 45760-2025精细陶瓷粉体堆积密度测定松装密度
- 职业技能鉴定机构备案表(空表)
- 补肾养血膏方联合PRP治疗肝肾亏虚型膝骨关节炎的临床疗效观察
- 医疗机构依法执业自查
- 专项复习:相似三角形折叠问题(分层练习)(综合练)
- 角色设计课程说课模板
- DB13T 5098-2019 无人值守起重机控制系统检验规则
- 被动解除劳动合同范本
评论
0/150
提交评论