收藏 分销(赏)

同济c++课本习题参考答案.doc

上传人:人****来 文档编号:4720324 上传时间:2024-10-11 格式:DOC 页数:34 大小:282.50KB
下载 相关 举报
同济c++课本习题参考答案.doc_第1页
第1页 / 共34页
同济c++课本习题参考答案.doc_第2页
第2页 / 共34页
同济c++课本习题参考答案.doc_第3页
第3页 / 共34页
同济c++课本习题参考答案.doc_第4页
第4页 / 共34页
同济c++课本习题参考答案.doc_第5页
第5页 / 共34页
点击查看更多>>
资源描述

1、34Error! Reference source not found.习 题 5一、选择题1. 下列叙述错误的是_A_。 A. 主函数中定义的变量在整个程序中都是有效的 B. 复合语句中定义的变量只在该复合语句中有效 C. 其它函数中定义的变量在主函数中不能使用 D. 形式参数是局部变量2. 若函数的形参为一维数组,则下列说法中错误的是_B、才_。A. 形参数组可以不指定大小B. 函数调用时对应的实参只能是数组名C. 函数调用时,系统会为形参数组分配存储单元D. 函数中对形参的修改将会影响对应的实参值3. 若函数的类型和return语句中的表达式的类型不一致,则_D_。A. 编译时出错B.

2、运行时出现不确定结果C. 不会出错,且返回值的类型以return语句中表达式的类型为准D. 不会出错,且返回值的类型以函数类型为准4. 下面的函数定义正确的是_D_。A. float f(float x;float y) B. float f(float x,y) return x*y; return x*y;C. float f(x,y) D. float f( int x, int y) int x,y ; return x*y; return x*y;5. 下面函数头的定义格式正确的是_C_。A. void sort(int an,int n) B. void sort(int a ,i

3、nt n)C. void sort(int a ,int n) D. void sort(int a ,n)(2). #include iostream.hvoid cube(int &x) x=x*x*x; void main()int x=5; cube(x); coutx;6. 下面4个程序中输出结果是125的有_C_。(1). #include iostream.hvoid cube(int x) x=x*x*x; void main()int x=5;cube(x); coutx;(4). #include iostream.hint x=5;void cube() x=x*x*x;

4、void main() cube(); coutx; (3). #include iostream.hint cube(int x) x=x*x*x; return(x); void main()int x=cube(5);coutx;A. 1 B. 2 C. 3 D. 47. 设函数m()的说明形式为void m(int,int *); 利用函数m()对数5和整数j作函数m()定义的计算,正确的调用形式为_C_。A. m(&5,&j) B. m(5,j) C. m(5,&j) D. m(&5,j)8. 设函数的说明为: void fun(int a,int m); ,若有定义:int a10

5、,n,x; 则下面调用该函数正确的是_A_。A. fun( a, n); B. x=fun( a, n); C. fun( a10, 10); D. x=fun( a, n);9. 下面函数说明正确的是_C_。A. void f1(int a=3, int b, int c); B. void f2 int a, int b=3, int c);C. void f3(int a, int b, int c=3); D. void f4(int a, int b, int 3);10. 有两个函数分别为: int f(int);和int f(int,int =100);,则下面说法正确的是_B_

6、。A. 不能在同一个程序中定义B. 可以在同一个程序中定义,但不可以重载C. 可以在同一个程序中定义并可重载D. 以上说法均错误11. 以下几种函数模板的定义正确的是_A_。B. template void fun1(T1 a,T1 b,T2 c) A. template T fun1(T a,int b) D. template T2 fun1(T1 a,T1 b) C. template void fun1(int a,int b) T i; 12. 下面程序的输出结果是_B_。#include iostream.hint m=10; void f(int m,int &n) m=m+2;

7、 n=n+2; void main() int n=5; f(m,n); coutm=m n=nendl;A. m=10 n=5 B. m=10 n=7 C. m=12 n=7 D. m=12 n=5二、阅读程序,写出运行结果1. yes 3 not 42. 43. 12 25 16 9 2 14. 212345. m=5n=36. 911三、程序填空1. 该程序功能:对x=1,2,.,10,求f(x)=x*x-5*x+sin(x)的最大值。#include iostream.h#include math.hfloat f(int x)float y; y=x*x-5*x+sin(x); _r

8、eturn y _;void main() int x; float max; _max=f(1)_; for(x=2;xmax)max=f(x)_ ; coutmaxendl;2. 函数backmove()是把字符指针x所指的字符串平移m个字符,即将最后m个字符移到串首。如“abcdefghij”, 平移3个字符,成“hijabcdefg”。#include stdio.h #include string.hvoid backmove(char *x,int m)int i,j,n;char w; n=strlen(x); for(j=0;jm;j+) w=_*(x+n-1)_; for(i

9、=0;in-1;i+) *(x+n-1-i)=_ *(x+n-2-i)_; _*x_=w; void main()char s20; gets(s); _backmove(s,3)_; /假设平移3个字符 puts(s);3. 函数index()为查找字符串sub是否是字符串st的子串。若是,返回sub在st中首次出现的下标,否则返回-1。字符串sub和st非空。如sub: cd ,st: abcdefcd ,返回2。#include iostream.h#include stdio.hvoid main()char s180,s280; _ int index(char ,char );_;

10、 gets(s1);gets(s2); if(_index(s1,s2)_) cout子串在字符串中首次出现的下标:index(s1,s2); else cout找不到;int index(char st,char sub)int i,j,k; for(i=0;sti!=0;i+) for(j=i,k=0;subk!= 0&stj=subk ;_ k+,j+_); if(subk= 0)_ return(i)_; return 0;4. 函数root为用二分法求方程f(x)=0在x1,x2的实根,精度为eps。二分法求根的基本思想为 f(x)在区间a,b上连续,f(a) 与 f(b)异号,区间

11、中点 c=(a+b)/2 的 f(c) 符号和 f(a) 符号确定 c 代替 a 或 b,使根所在区间每次减半,直到|a- b|eps或|f(c)|0.0)_ x1=x_; else if (y1*y=eps_&_fabs(x2-x1)=eps); return(x); double f(double x)return x*x*x-5*x*x+16*x-80;void main()cout_root(1,7)_endl;5随机生成10个1100之间的数放在一维数组中,求其平均值及最大的元素值。#include iostream.h#include stdlib.hconst int N=10;

12、void fun(float *p,float *p1,float *p2) float sum,max1;_max1=*p_; for(int i=1;iN;i+) if (max1*p) max1=*p; sum=sum+*p; p+; _*p1=max1_; _*p2=sum/N_;void main() float a10,aver,max,x;for(int i=0;i10;i+) x=rand() % 100+1; ai=x; for(i = 0;i10;i+) coutai ; coutendl;_fun(a,max,aver)_; cout平均值:aver 最大值:max=0;

13、-i) chk+=b _ci_ ; _chk=0_;void main()char ch10;int m,h;cinmh;convert(m,h,ch);coutchendl;四、编写程序1. 编写函数,功能为将字符串s中的字符c1用字符c2替换,并加以调用。函数形式为:void match(char s, char c1,char c2);#include void replace(char s,char c1,char c2)char *p=s; while(*p!=0) if(*p=c1) *p=c2; p+; void main()char s80,c1,c2; cins; cinc1

14、c2; replace(s,c1,c2); coutsendl;2. 编写函数,功能为求圆的周长和面积。函数分别定义为如下形式:double area(double r, double * girth ,double pi=3.14159); void fun(double r, double &girth ,double &area,double pi=3.14159); 分别编二个程序实现,半径从键盘输入。方法一、#include double area(double r,double *girth,double pi=3.14159)*girth=2*pi*r;return(pi*r*r

15、);void main()double r,len,s; cinr; s=area(r,&len); coutlen=len,s=sendl;方法二#include void fun(double r,double &girth,double &area,double pi=3.14159)girth=2*pi*r;area=pi*r*r;void main()double r,len,s; cinr; fun(r,len,s); coutlen=len,s=sendl;3. 编写函数,功能是求二维数组中最大元素所在的行号和列号,再编写主函数调用之。#define SIZE1 3#define

16、 SIZE2 4#include iostream.h#include stdlib.hfloat max_value(float x4,int &ii,int &jj)float max=x00; for(int i=0;iSIZE1;i+) for(int j=0;jmax) max=xij; ii=i; jj=j; return(max);void main()int i,j,t1,t2;float aSIZE1SIZE2; coutenter the array:n;for(i=0;iSIZE1;i+) for(j=0;jSIZE2;j+) aij=rand()%101; coutai

17、j ;coutendl;coutmax value is max_value(a,t1,t2);cout line=t1 row=t2endl;4. 编写函数,将两个字符串s和t的前n个字符拼接成新的字符串,结果存放在s中。如果s或t中字符串的长度不足n,按实际长度处理。例如,如果有ABCDEFGH和abcdefghijk,n为3,则新的字符串为”ABCabc”,并加以调用。函数形式为:void mystrcat(char s,char t,int n);#include #include void mystrcat(char s,char t,int n)int l1=strlen(s),l

18、2=strlen(t);int k1=l1n?n:l1,k2=l2n?n:l2;for(int i=0;ist;cinn;mystrcat(s,t,n);coutsendl;5. 编写函数,其功能是逐字符比较两个字符串s1和s2,并将s1中第一个与s2不相同字符的地址返回给主函数。再编写主函数调用该函数,并在主函数中输出s1从这个位置开始的子串。函数形式为:char *dif(char s1,char s2);#include #include stdio.hchar *dif(char s1,char s2)int i=0;while(s1i=s2i&s1i!=0)i+;if(s1i!=0)

19、return(&s1i);else return NULL;void main()char s30,t30;cinst;char *p;if(p=dif(s,t)!=NULL)coutpendl;elsecouts包含于t中n;6. 用递归方法求正整数m,n的最大公约数。#include int gcd(int m,int n)int r=m%n;if(r!=0)return gcd(n,r);elsereturn(n);void main()int m,n;cinmn;coutgcd(m,n)endl;7. 编写四个同名函数max,分别求两个整数、三个整数,两个双精度数、三个双精度数的最大值

20、。#include int max(int a,int b)return(ab?a:b);int max(int a,int b,int c)int t=max(a,b);return(max(t,c);double max(double a,double b)return(ab?a:b);double max(double a,double b,double c)double t=max(a,b);return(max(t,c);void main()double x,y,z; int a,b,c; cinabc; cinxyz; coutmax(a,b)endlmax(a,b,c)endl

21、; coutmax(x,y)endlmax(x,y,z)endl; 第四章习题一、1.C2.A3.C4.D5.C6.B7.A8.B二、1. 67890543212. 43. 14三、1.(1)0,1(3)xi=xi-1+xi-2(4)setw(5)xi2.(1)rand()%101(2)jai/2(3)A(i)=3.(1)bm=0(2)nm+4(3)bm=bm/44.(该题目需要加上头文件#include stdio.h)(1)gets(s1)(2)gets(s2)(3)*s1=*s2(4) r=05.(1)j=k=l=0(2)jM&kN(3)cl=aj(4)cl+=bk+(5)kN(6)jM

22、四、1.(1)#include stdlib.h#include iostream.hvoid main()int a44,b44,c44,i,j; for(i=0;i4;i+) for(j=0;j4;j+) aij=rand()%41+30; for(i=0;i4;i+) for(j=0;j4;j+) bij=rand()%35+101; cout矩阵A的内容如下:n; for(i=0;i4;i+)for(j=0;j4;j+) coutaij ; coutendl; cout矩阵B的内容如下:n; for(i=0;i4;i+)for(j=0;j4;j+) coutbij ; coutendl

23、; for(i=0;i4;i+) for(j=0;j4;j+) cij=aij+bij; cout矩阵C的内容如下:n; for(i=0;i4;i+)for(j=0;j4;j+) coutcij ; coutendl; (2)#include stdlib.h#include iostream.hvoid main()int a44,b44,c44,i,j,max,imax,jmax; for(i=0;i4;i+) for(j=0;j4;j+) aij=rand()%41+30; for(i=0;i4;i+) for(j=0;j4;j+) bij=rand()%35+101; cout矩阵A的

24、内容如下:n; for(i=0;i4;i+)for(j=0;j4;j+) coutaij ; coutendl; cout矩阵B的内容如下:n; for(i=0;i4;i+)for(j=0;j4;j+) coutbij ; coutendl; for(i=0;i4;i+) for(j=0;j4;j+) cij=aij+bij; cout矩阵C的内容如下:n; for(i=0;i4;i+)for(j=0;j4;j+) coutcij ; coutendl; max=c00; for(i=0;i4;i+) for(j=0;jmax) max=cij; imax=i; jmax=j; coutmax

25、=cimaxjmax=maxendl;(3)#include stdlib.h#include iostream.hvoid main()int a44,b44,i,j; for(i=0;i4;i+) for(j=0;j4;j+) aij=rand()%41+30; for(i=0;i4;i+) for(j=0;j4;j+) bij=rand()%35+101; cout矩阵A的主对角线以下元素内容如下:n; for(i=0;i4;i+)for(j=0;j=i;j+) coutaij ; coutendl; cout矩阵B的主对角线以上元素内容如下:n; for(i=0;i4;i+)for(j

26、=i;j4;j+) coutbij ; coutendl; (4)#include stdlib.h#include iostream.hvoid main()int a44,i,j; for(i=0;i4;i+) for(j=0;j4;j+) aij=rand()%41+30; cout矩阵A的内容如下:n; for(i=0;i4;i+)for(j=0;j4;j+) coutaij ; coutendl; for(j=0;j4;j+)int t=a0j; a0j=a2j; a2j=t; cout改变后矩阵A的内容如下:n; for(i=0;i4;i+)for(j=0;j4;j+) couta

27、ij ; coutendl;(5)#include stdlib.h#include iostream.hvoid main()int a44,i,j,s=0; for(i=0;i4;i+) for(j=0;j4;j+) aij=rand()%41+30; cout矩阵A的内容如下:n; for(i=0;i4;i+)for(j=0;j4;j+) coutaij ; coutendl; for(i=0;i4;i+) for(j=0;j4;j+) if(i=j|i+j=3) s=s+aij; cout对角线元素之和为:sendl;2.用字符数组实现#include stdio.hvoid main

28、()int i;char s150,s220; gets(s1); for(i=0;s1i!=0;i+) s2i=s1i;s2i=0;puts(s2);用字符指针实现#include stdio.hvoid main()int i;char s150,*p1=s1,*p2; p2=new char20; gets(p2); while(*p2!=0) *p1+=*p2+; *p1=0;puts(s1);3.#include stdio.hvoid main() char s80,ch; int i,len=0; gets(s); while(slen!=0)len+; for(i=0;ich;

29、 for(i=j=0;si!=0;i+)if(si!=ch)sj=si;j+; sj=0; puts(s);第六章习题一、选择题1、A(在C语言中int占2个字节)2、D3、BC4、D5、a10.ch6、A7、C8、B9、A10、A二、程序填空1、stui.scorek=jpk=pipi=temp2、p!=NULLp-datap-next3、struct node *s-data=chr=sNULL三、编程题1、#include iostream.hstruct staffchar num6; char name8; float salary3; /分项工资 float gs; /实得工资s1

30、00;void main( ) int i,j,n;cinn; /*输入职工人数*/for(i=0;isi.numsi.name;for(j=0;jsi.salaryj; coutNO. salaryn;for(i=0;in;i+)si.gs= si.salary0+ si.salary1- si.salary2 ;coutsi.numtsi.gsch=si; if(head=NULL) head=insert ; head-next=NULL; else insert-next=head ; head=insert; i+; p=head;while(p!=NULL)if(p-ch=A&p-

31、ch=Z)num+;coutch; p=p-next; coutendlnum=nump1-ch)p2=p1;p1=p1-next;if(p1=NULL)insert=new node;insert-ch=c;insert-count=1;if(head=NULL)insert-next=head;head=insert;else insert-next=NULL;p2-next=insert;else if(c=p1-ch)p1-count+;elseinsert=new node;insert-ch=c;insert-count=1;if(p1=head)insert-next=head;head=insert;elseinsert-next=p1;p2-next=insert;p

展开阅读全文
相似文档                                   自信AI助手自信AI助手
猜你喜欢                                   自信AI导航自信AI导航
搜索标签

当前位置:首页 > 通信科技 > 开发语言

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        获赠5币

©2010-2024 宁波自信网络信息技术有限公司  版权所有

客服电话:4008-655-100  投诉/维权电话:4009-655-100

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :gzh.png    weibo.png    LOFTER.png 

客服