1、C+徐孝凯习题解答清华大学出版社出版普通高等院校计算机专业(本科)实用教程系列之一C+语言基础教程全部练习题参考解答第一章 C+语言概述 1.2 填空题 1# 2. ; 3. 空格 制表 回车换行 4. 系统 用户 5. 程序 6. 函数头 函数体 7. main 8. 函数原型 9. 原型 10. 复合语句 11. .h .cpp 12. 严重错误 警告错误 13. void 14. void 15. int 0 16. n 17. 下一行 18. 空白符 1.3 写出下列程序运行结果,此题又作为上机实验题 1. x+y=11,x*y=30 2. cube(3)=27 cube(5)=125
2、 cube(8)=512 3. averageValue:3 averageValue:4 4. 请输入三个整数:10 5 9 (假定输入的三个整数为10,5,9) 最大值: 10 最小值: 5第二章 数据类型和表达式 2.2 填空题 1. 4,1,1,4,8 2. short, int, long 3. 4, 2, 2 4. 46, 123, 98 5. 107, 10, 92, 42 102 6. 15 7. 符号常量,整数,int 8. 3.4E2, 5.27E6 9. int, int, double, double, float 10. x, 15 11. 6, 60 12. 26,
3、 25 13. 4, 1 14. 256, 22 15. x, x 16. 0, 1 17. 20 18. 5 19. 9, 216 20. 0, 19 21. (1+x)*sin(48*3.14159/180), a*pow(x,b)*exp(x+1) 2.7 写出下列每个程序运行后的输出结果并自行上机验证 1. 0,1,2,3 2. p=62.8318 s=314.159 3. 1 1 1 2 2 2 3 1 8 4. 1 1 2 4 4 4 8 8 4 4 5. 30 120 7 30 10 30 30 20 10 6. 1 1 0 0 0 1 7. 5 10 15 15 10 15 1
4、50 10 150 151 11 10 152 12 151 8. 15.72 16 15 15.72 16 15.7 15.72 16 15.7 第三章 流程控制语句 3.1 填空题 1. 选择,循环,跳转 2. goto, continue, break, return 3. 结构性 4. 常量表达式 5. if 6. switch 7. 不停止 8. switch 9. 1, n+1, n 10. 循环条件,循环体 11. 循环, switch, 循环 12. 循环体,循环体 13. do, 右花括号 14. do, for, while 15. 10 16. 11 17. 10 18.
5、 break 19. continue 20. return 3.2 写出下列每个程序运行后的输出结果并上机验证 1. 121 a,b,c=7,12,30 2. 11 14 switch end. 3. 36 -5 73 192 8 44 36 139 -5 1 73 8 192 13 8 67 44 171 4. 36 25 20 43 12 70 66 34 28 15 32 55 129 255 52 5. 1 1 1 2 2 3 3 6 9 4 24 33 6. 10 6 4 7. * * * * * 8. 1 5 7 11 13 17 19 23 25 29 1509. +*+ k=
6、11 10. 1 4 2 6 2 3 3 5 5 6 c=37 11. s=63 12. 请输入两个正整数x和y:24 88 x和y的最小公倍数:264第四章 数组和字符串 4.1 填空题 1. i+1 2. 40 a+20 3. 192 a+136 4. 300 a+94 5. 9 7 6. 3 8 0 7. 1 2 8. 0 1 9. 8 10. 11 11. n+1 n+1 12. 字符串 字符串 13. 10 19 14. 1 4 15. ”456” ” (或空串) 16. 5 1 17. ”abcdef” 18. strcpy(a,”aaa”) 19. 3 20. 2 21. Int
7、eger int int 22. AA 10 int 23. BB 10 50 500 char 24. 4 6 24 int 4.2 写出下列程序运行后的输出结果 1. 6 4 2. 66 55 3. 14 25 4. 4 3 14 5. 4 3 2 2 3 6. 2 1 5 2 7. 1 3 12 8. worker cadre 9. 4 1 8 -1 4.4 编写下列程序并上机运行 1. #include void main() int i,a20; a0=0; a1=1; for(i=2;i=0;i-) coutai ; coutendl; 2. #include void main(
8、) char s31; int c=0; couts; for(int i=0;si;i+) if(si=0 & si=9) c+; coutcendl; 3. #include void main() double a44; int i,j; cout输入一个4*4的数值矩阵:; for(i=0;i4;i+) for(j=0;jaij; double p=1; for(i=0;i4;i+) p*=aii; coutpendl; 4. #include void main() int a34=3,8,2,9,4,7,3,6,5,2,8,4; int b43; int i,j; for(i=0;
9、i3;i+) for(j=0;j4;j+) bji=aij; for(i=0;i4;i+) for(j=0;j3;j+) coutbij ; coutendl; 5. #include void main() int a34=3,0,4,5,6,2,1,7,4,1,5,8; int b44=1,4,0,3,2,5,1,6,0,7,4,4,9,3,6,0; int c34=0; int i,j,k; for(i=0;i3;i+) for(j=0;j4;j+) for(k=0;k4;k+) cij+=aik*bkj; for(i=0;i3;i+) for(j=0;j4;j+) coutcij ;
10、coutendl; 6. #include #include void SelectSort(int b, int n) int i,j,k; for(i=1;in;i+) k=i-1; for(j=i;jn;j+) if(bjbk) k=j; int x=bi-1; bi-1=bk; bk=x; void main() int a10; int i; for(i=0;i10;i+) ai=rand()%90+10; SelectSort(a,10); for(i=0;i10;i+) coutai ; coutendl; 7. #include void main() char a80; in
11、t b26=0; couta; int i,x; for(i=0;ai;i+) if(ai=A & ai=a & ai=z) x=ai-a; bx+; for(i=0;i26;i+) coutchar(a+i):bi ; if(i+1)%4=0) coutendl; 8. #include #include void SelectSort(char a124, double b, int n) int i,j,k; for(i=1;in;i+) k=i-1; for(j=i;jn;j+) if(bjbk) k=j; char x4; double y; strcpy(x,ai-1); strc
12、py(ai-1,ak); strcpy(ak,x); y=bi-1; bi-1=bk; bk=y; void main() char a124=001,002,010,011,023,025, 031,036,037,102,325,438; double b12=13.6,14.8,12.0,12.7,15.6,13.4,14.9, 12.6,13.4,12.5,15.3,12.7; SelectSort(a,b,12); for(int i=0;i12;i+) couti+1 ai biendl; 第五章 指针 5.1 填空题 1. 4 2. 地址 DataType* 3. (char*)
13、p 4. int* 5. *p &p 6. *p p 7. *p *p 8. 25 9. 42 10. 26 11. 42 12. 4*i 13. *(a+i) 14. a+i*sizeof(ai) 15. 第一个 修改 16. b7 b2 17. int *p=&x; 18. *p 19. intn int * 20. c30 c32 21. int(*)6 22. char20 char* fi0 23. *(ai+j) 或*(*(a+i)+j)或*(a+i)j 24. int &y=x; 25. x 26. 相等 x 27. *p 28. 0 p0 *P 29. a 30. char(*
14、)n 31. delete p 32. delete p 5.2 写出下列每个程序运行后的输出结果并上机验证 1. 1 2 3 2. 3 5 7 9 11 13 15 17 3. 229 45 45.8 4. 72 24 5. 3 6 9 12 15 15 12 9 6 3 6. 4 12 20 28 7. 20 40 30 60 8. 17 17 52 52 9. computer typewriter telephone 10. aremac camera 11. 7 4 5 10 3 12. 24 24 49 49 13. 5 15 15 5 14. 0 1 1 2 3 5 8 13 2
15、1 34 55 89第六章 函数 6.1 填空题 1. 引用 2. char* a 3. int (*w)N 4. 不能够 5. 不能够 能够 6. 实参 7. 函数声明 8. 实参的值 地址 9. 实参 10. return return 11. 数组 指针 12. 全局 文件 函数 局部 13. 能够 14. 无关 15. 有关 不同 16. 不同 相同 17. 不能够 相同 18. 可以 19. 内层 外层 20. 原型语句 21. 自己 22. 递归 递归 23. 相同 不同 24. 不是 不可以 25. 函数调用表达式 26. 普通函数 27. 模板函数 28. int& (*ff)
16、(int, int) 6.2 给出下列程序运行后的输出结果 1. 10 20 30 65 30 20 2. 10 20 15 35 5 10 20 3. x=13, y=21 x=5, y=8, z=34 4. x,y=10, 26 x,y=26, 10 x,y=10, 26 x,y=25, 11 5. 66 6. 25 37 8 7. motion telephone 8. 12 4 9. 6 5 4 3 2 1 0 91 10.v1=4 v2=5.55 11.操作成功! 操作成功! 操作成功! 25 48 50 82 66 43 75 s t u d e n t w File Edit I
17、nsert Project Build 12.输入矩阵的行数和列数:3 4 1 2 3 4 2 4 6 8 3 6 9 12第七章 结构与联合 7.4 编写下列程序或函数。 1. void FindOut(Person a, int n, int x) for(int i=0; i=x) coutsetw(12)ai.name; if(ai.sex=true) coutsetw(8)male; else coutsetw(8)female; coutsetw(4)ai.age; coutsetw(8)ai.payendl; 2. Franction& FranCut(Franction& a,
18、 const Franction& b) a.nume=a.nume*b.deno-b.nume*a.deno; a.deno=a.deno*b.deno; FranSimp(a); return a; Franction& operator-(Franction& a, const Franction& b) a.nume=a.nume*b.deno-b.nume*a.deno; a.deno=a.deno*b.deno; FranSimp(a); return a; 3. template Type Delete(Type a, int& n, int i) if(i=n | n1) ce
19、rr参数值无效!endl; exit(1); Type x=ai; for(int j=i+1; j=n-1; j+) aj-1=aj; n-; return x; 4. int Find(Workers a, int n, int k, char* x) for(int i=k; ks2 release dynamic memory space1 release dynamic memory space1 release dynamic memory space1 8.4 按题目要求编写出程序、函数或类 1. 各成员函数的体外定义如下 Quadratic:Quadratic(double a
20、a, double bb, double cc) a=aa; b=bb; c=cc; Quadratic Quadratic:operator+(Quadratic& x) Quadratic q; q.a=a+x.a; q.b=b+x.b; q.c=c+x.c; return q; Quadratic Quadratic:operator-(Quadratic& x) Quadratic q; q.a=a-x.a; q.b=b-x.b; q.c=c-x.c; return q; double Quadratic:Compute(double x) return a*x*x+b*x+c; in
21、t Quadratic:Root(double& r1, double& r2) if(a=0) return -1; double d=b*b-4*a*c; if(d=0) r1=(-b+sqrt(d)/(2*a); r2=(-b-sqrt(d)/(2*a); return 1; else return 0; void Quadratic:Print() if(a) couta0) cout+bx; else coutb0) cout+c; else coutc; coutendl; 2. 定义的矩形类如下: class Rectangle /矩形类 double len,wid; publ
22、ic: Rectangle() len=wid=0; Rectangle(double length, double width) len=length; wid=width; double Perimeter() return 2*(len+wid); double Area() return len*wid; double GetLength() return len; double GetWidth() return wid; void Modify(double length, double width) len=length; wid=width; void Output() cou
23、tlength: len, width: widendl; ;第九章 类的继承与多态性 9.1 填空题 1. 初始化表 2. 基类成员 类对象成员 非类对象成员 3. 派生类 类对象所属类 基类 4. int AB:bb=0; 5. AB:bb 9.2 写出下列程序运行后的输出结果。 1. Constructor A!2 Constructor B!3 Constructor A!4 Constructor B!5 2. Destructor B!7 Destructor A!6 Destructor B!0 Destructor A!5 3. AX constructor. AX constructor. BX constructor. AX constructor. AX constr
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1