ImageVerifierCode 换一换
格式:DOCX , 页数:70 ,大小:41.71KB ,
资源ID:11410288      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/11410288.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(C程序设计课后答案.docx)为本站会员(b****7)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

C程序设计课后答案.docx

1、C程序设计课后答案第一章一、选择题1.B; (typedef ,typeid ,typename,都为保留字); 2.C; (标识符,应该以字母或,下划线开头); 3.C; (标识符中有的特殊符号,只能有下划线); 二、填空题1. cin,cout 2. new,delete 3. int a=55; 三、改错题1.没有定义变量num; 2.*p不能当作“左值”。 3.p为常量指针,不能吧p作为“左值”,p=&y,错误。 四、编程题1. 分别用字符和ASCII码形式输出整数值65和66. #include using namespace std;void main() char a=A,b=B

2、; int ascii_1=53,ascii_2=54;/ASCII码中的,5和6 cout字符输出:(int)a,(int)b endl; coutASCII码输出:(char)ascii_2(char)ascii_1,; cout(char)ascii_1(char)ascii_1 endl;2.编写一个int型变量分配100个整形空间的程序。#include using namespace std;void main() int *p; p = new int100; for(int i = 0;i 100;i+) *(p+i)=i; for(i = 0;i 100;i+) cout*(

3、p+i),; delete p;3.编写完整的程序,它读入15个float值,用指针把它们存放在一个存储快里,然后输出这些值的和以及最小值。#include using namespace std;void main() float min,sum=0,*p; p = new float15; cout输入15个float类型的值: *p;/输入第一个数, min=*p;/ 先认为是最小sum+=*p;/加入累加器 for(int i = 1;i *(p+i); sum+=*(p+i); /加入累加器 if(*(p+i)min) min=*(p+i); coutn这些数的和是:sum n其中最

4、小的是:min endl; delete p;4.声明如下数组: int a = 1 ,2 ,3, 4, 5, 6, 7, 8;先查找4的位置,将数组a复制给数组b,然后将数组a的内容反转,再查找4的位置,最后分别输出数组a和b的内容。#include #include /数组处理的头文件#include using namespace std;void main() int a=1,2,3,4,5,6,7,8,b8; cout数组a中4的位置是: find(a,a+8,4) endl;/查找4的位置 copy(a,a+8,b);/将数组a复制给数组b reverse_copy(b,b+8,a

5、);/把数组b,逆向复制给a,完成a的逆转 cout数组a反转后,4的位置是: find(a,a+8,4) endl;/在查找4的位置 cout数字a的内容: endl; for(int i=0;i8;i+) cout ai ,; coutn数组b中的内容: endl; for(i=0;i8;i+) cout bi ,;第二章一、单项选择1.D; 2.D;二、画图题三、编程题1.使用多种方法编写将两个字符串连接在一起的程序。#include #include using namespace std;void main() /使用string类定义字符串,完成字符串连接 string str1(

6、C+),str2(程序设计); string str3; str3 = str1+str2;/连接方式1 cout str3 endl; /使用char数组定义字符串,完成连接 char c1 = c+,c2 = program; char c320; int i=0,k=0; for(i=0;i20;i+)/初始化c3 c3i=0; i=0; while(c1i!=0) c3k=c1i; i+; k+; i=0; while(c2i!=0) c3k=c2i; i+; k+; cout c3 endl;2.已知一个string的对象str的内容为“We are here!”,使用多种方法输出“

7、h”。#include #include #include #include using namespace std;void main() string str1(We are here!); cout str17 endl;/通过数组 string str2=str1.substr(7,1);/通过得到子字符串 cout str2 endl; char *p=find(str1.begin(),str1.end(),h);/通过find函数 if(p) cout*p endl;第三章一、填空题1.函数原型声明; 2.inline 3.对象、对象指针、引用 4.函数func返回引用 5.in

8、t *fun(char,int&); 二、单项选择题1.A; 2.C; 3.C; 三、改错题1.y = x * x - T; 错误,T是类型,不是变量,不能参加运算; 2.T max(T x, y) y没有类型 3.void change (string & s)中函数change 的参数定义成了常量,只能使用参数,而无权修改他。 所以应去掉。四、编程题1.编写一个求方程ax2 + bx + c = 0的根 的程序,用3个函数分别求当b2-4ac大于零、等于零、和小于零时的方程的根。要求从主函数输入a,b,c的值并输出结果。 #include #include void equation_1

9、(int a, int b, int c) double x1, x2, temp; temp = b*b - 4 * a * c; x1 = (-b + sqrt(temp) ) / (2 * a * 1.0); x2 = (-b - sqrt(temp) ) / (2 * a * 1.0); cout两个不相等的实根 endl; coutx1 = x1, x2 = x2 endl;void equation_2 (int a, int b, int c) double x1, x2, temp; temp = b*b - 4 * a * c; x1 = (-b + sqrt(temp) )

10、 / (2 * a * 1.0); x2 = x1; cout两个相等的实根 endl; coutx1 = x1, x2 = x2 endl;void equation_3 (int a, int b, int c) double temp, real1, real2, image1, image2; temp = - (b*b - 4 * a * c); real1 = -b / (2 * a *1.0); real2 = real1; image1 = sqrt(temp)/(2*a*1.0); image2 = - image1; cout两个虚根 endl; coutx1 = real

11、1 + image1j endl; coutx2 = real2 + image2j endl;void main() int a, b, c; double temp; cout输入a,b,c的值abc; cout方程为: ax*x+ bx+ c = 0 0) equation_1 (a, b, c); if(temp = 0) equation_2 (a, b, c); if(temp 0) equation_3 (a, b, c);2.定义函数up(ch),如字符变量ch是小写字母就转换成大写字母并通过up返回,否则字符ch不改变。要求在短小而完全的程序中显示这个程序是怎样被调用的。#i

12、nclude using namespace std;char up (char c) if(c = 97 & c = 123) return (c - 32) ; else return c;void main() int i; char c15 = A,v,e,t,E,T,%,&,4,Y,e,i,9,; for(i = 0 ; i 15 ; i+) cout up(ci), ; cout endl;3.编写主程序调用带实数r和整数n两个参数的函数并输出r的n次幂。#include #include double power(double r, int n) int i; double re

13、sult = 1.0; for(i=0;i n;i+) result = result * r; return result;void main() double r; int n; coutr; coutn; cout r的 n次幂是: power(r,n) endl;4.编写有字符型参数C和整形参数N的函数,让他们显示出由字符C组成的三角形。其方式为第1行有1个字符C,第2行有2个字符C ,等等。#include using namespace std;void print_triangle(char c, int n) int i, j; for(i=0; i n; i+) for(j=

14、0; j=i; j+) cout c; cout endl; void main() print_triangle(a,10);5.编写一个求字符串长度的函数strlen(),再用strlen()函数编写一个函数revers(s)的倒序递归程序,使字符串s逆序。#include #include using namespace std;int strlen(char *str) int len = 0; while(strlen != 0) len+; return len;void revers(char *b) char c; int j, len; len=strlen(b); j=le

15、n/2-1; while(j=0) c=*(b+j); /交换对称元素 *(b+j)=*(b+len-j-1); *(b+len-j-1)=c; j-; blen=0;void main() char str=1234567890; cout str-的长度: strlen(str) endl; cout str endl;/倒序前 revers(str);/ cout str endl;/倒序后6.用函数模板实现3个数值中按最小值到最大值排序的程序。#include using namespace std;template void sort(T a, T b, T c) T array3,

16、temp; int i,j; array0 = a; array1 = b; array2 = c; for(i=0;i3;i+)/冒泡法排序 for(j=i;jarrayj+1) temp = arrayj; arrayj = arrayj+1; arrayj+1 = temp; cout array0 array1 array2 endl;void main() sort(5,1,9);7.利用函数模板设计一个求数组元素中和的函数,并检验之。#include using namespace std;template T sum (T a,int n) int i; T s=0; for(i

17、=0;i n;i+) s = s + ai; return s;void main () int a5=1,2,3,4,5; int s = sum(a,5); cout s endl;8.重载上题中的函数模板,使他能够进行两个数组的求和。#include using namespace std;template T sum (T a, int n) int i; T s=0; for(i=0;i n;i+) s = s + ai; return s;template /重载上面的模板T sum (T a, int n, T b, int m) return sum(a,n)+sum(b,m)

18、;void main () int a5=1,2,3,4,5; int b10=1,2,3,4,5,6,7,8,9,10; int s1 = sum(a, 5); int s2 = sum(b, 10); int s3= sum(a, 5, b, 10); cout s1 endl; cout s2 endl; cout s3 endl;第四章一、填空题1.数据成员、成员函数; 2.类、返回类型、1;3.fun:fun(fun &)、fun:fun(const fun &); 二、单项选择题1.C。2.C。3.没有答案,应该是A:A(void)、或A:A()。4.B。 5.C。 6.C。 7.

19、D三、改错题1.return m;-错误,没又定义变量m; base()delete p p后应有一个;2.A.init(24,56);-错误,应该先定义A对象:Point A; 同时参数个数不同;Setx(a);无类型,也无返回值。四、完成程序题1.#include using namespace std;class base private : /私有数据成员 int a, b; public : void init(int x, int y)/公有函数 a = x; b = y; void print() cout2 * a - b = (2*a-b) endl; ;void main(

20、) base a; a.init(68,55); a.print();2.#include using namespace std;class Point private : int m, n; public : Point(int, int);/整型变量,为参数的构造函数 Point(Point&);/复制构造函数的原型 print() coutm = m, n = n endl; ;Point:Point(int a, int b) m = a; n = b;Point:Point(Point & t)/复制构造函数的定义 m = t.m; n = t.n;void main() Poin

21、t a(10,89); Point b(a); a.print(); b.print();五、程序分析题1.没有结果,因为没有main函数如果加main函数void main() base b(10, 20);输出:初始化.10,20Destory.10,202.输出:55六、编程题1.设计一个点类Point,再设计一个矩形类,矩形类使用Point类的两个坐标点作为矩形的对角顶点。并可以输出4个坐标值和面积。使用测试程序验证程序。#include using namespace std;class Point /点类 private: int x, y;/私有成员变量,坐标 public :

22、Point()/无参数的构造方法,对xy初始化 x = 0; y = 0; Point(int a, int b)/又参数的构造方法,对xy赋值 x = a; y = b; void setXY(int a, int b)/设置坐标的函数 x = a; y = b; int getX()/得到x的方法 return x; int getY()/得到有的函数 return y; ;class Rectangle /矩形类 private: Point point1, point2, point3, point4;/私有成员变量,4个点的对象 public : Rectangle();/类Poin

23、t的无参构造函数已经对每个对象做初始化啦,这里不用对每个点多初始化了 Rectangle(Point one, Point two)/用点对象做初始化的,构造函数,1和4为对角顶点 point1 = one; point4 = two; init(); Rectangle(int x1, int y1, int x2, int y2)/用两对坐标做初始化,构造函数,1和4为对角顶点 point1.setXY(x1, y1); point4.setXY(x2, y2); init(); void init()/给另外两个点做初始化的函数 point2.setXY(point4.getX(), p

24、oint1.getY() ); point3.setXY(point1.getX(), point4.getY() ); void printPoint()/打印四个点的函数 coutA:( point1.getX() , point1.getY() ) endl; coutB:( point2.getX() , point2.getY() ) endl; coutC:( point3.getX() , point3.getY() ) endl; coutD:( point4.getX() , point4.getY() ) 0) return area; else return -area; ;void main() Point p1(-15, 56), p2(89, -10);/定义两个点 Rectangle r1(p1, p2);/用两个点做参数,声明一个矩形对象r1 Rectangle r2(1,

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1