CC程序设计教程龚沛曾课后习题答案全集.doc_第1页
CC程序设计教程龚沛曾课后习题答案全集.doc_第2页
CC程序设计教程龚沛曾课后习题答案全集.doc_第3页
CC程序设计教程龚沛曾课后习题答案全集.doc_第4页
CC程序设计教程龚沛曾课后习题答案全集.doc_第5页
已阅读5页,还剩37页未读 继续免费阅读

付费下载

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

- 习题一 一、 1、C 2、 A 3、B 4、C 5、D 6、A 7、C 8、B 二、 main 1、 2、数字、下划线 、字符串3 #define 、4、 #include hex 5、 .h 6、 7逻辑错误、语法错误 Insert/Remove Breakpoint 、8 .cpp 、9 Fileview 10、 三、略 * 习题二 一、 B 、9C 、5D 、1、B 2 C 3、4、C 、D 6C 7、8、B 、B 10 D 11、 二、 x+y!=a+b 、1 pow(ln(10)+x*y),3) 2、 注:对于函数、 sin(w*u)/(cos(45*3.14/180)+3*x*x) cos,其要求的参数是弧度表示。3 exp(3)+sqrt(2*x+3*y) 4、 fabs(x-y)+(x+y)/(3*x) 、5 1.0/(1.0/r1+1.0/r2+1.0/r3) 6、 三、 d) 和(a 中有且只有一个大于、1 ad&b=d|ad b z=(xy?x:y) 、2 x0&yz&x+zy&y+zx 4、 int(x*100+0.5)/100.0 、5 d0&d=100&d%2=0 、6 x%5=0|y%5=0 、7 n%k=0?n/k:n/k+1 8、 四、 1、略- - 2、主要语句: x%2=0?cout 偶数 :cout 基数 ; 3、主要语句: coutx/_x0010_x/10_x0010_x/100; 4、主要语句: year0=0|year%4=0&year_x0010_0!=0?cout 闰年 :cout0 count (2)cinmn m=m-n n=n-m (3)n=m n=n/5 m=m+5 四、 1、 #include iostream.h #include math.h void main() - - float x,y; coutinput x:x; if(x5) - - y=fabs(x); else if(x20) y=3*x*x-2*x+1; else y=x/5; 潣瑵?硜尽?尼礬尽?攼摮? 2、 #include iostream.h void main() float s,p,t,w; coutinput s:s; switch(int(s)/1000) case 0:p=0;break; case 1:p=0.05;break; case 2:p=0.08;break; case 3: case 4:p=0.1;break; default:p=0.15;break; t=s*p; w=s-t; cout 纳税款 =t, 实得工资 =wendl; 3、 #include iostream.h void main() int m,count=0; for(m=100;m1000;m+) if(m_x0010_+m/10_x0010_+m/100=5) count+; coutcount=countendl; - - 4、 #include iostream.h void main() int m,n; - - for(m=0;m=30;m+) for(n=0;n=30;n+) if(m*2+n*4=90&m+n=30) cout 鸡 :m 兔:n1e-6;n+) t=t*n; e=e+1/t; coute=eendl; 6、 #include iostream.h void main() int x,y; for(x=1;x=100;x+) for(y=1;y=100;y+) if(x*x+2*x=y*y*y) 潣瑵?硜尽?尼礬尽?攼摮? 7、 #include iostream.h #include stdio.h void main() char ch; while(ch=getchar()!=.) if(ch= ) coutendl; - - else coutch; 8、 - - #include iostream.h #include math.h void main() int m,k1,k2; for(m=0;m=1000;m+) k1=sqrt(m+100); k2=sqrt(m+100+168); if(k1*k1=m+100&k2*k2=m+100+168) endl; coutm0) x1=x; else x2=x; while(fabs(x2-x1)eps); coutx; 第四章 一、 B 、A 、7、6C 5D 、B、3D A、2、1C 、 AC 4、D 8 二、 678904321 1、 144 、2 - - 3、 m-n=14 三、 1、 - - #include iomanip.h 0,1 xi=xi-1+xi-2 setw(5)xi 2、 rand()_x0010_0+1 jai/2 A(i) 3、 bm=0 nm+4 bm=bm/4 4、 gets(ch1) gets(ch2) *s1=*s2 r=0 5、 j=k=l=0 jM&kN cl=aj cl+=bk+ kN jM 四、 1.(1) #include stdlib.h #include iostream.h void main() int a44,b44,c44,i,j; for(i=0;i4;i+) for(j=0;j4;j+) aij=rand()A+30; - - for(i=0;i4;i+) for(j=0;j4;j+) bij=rand()5+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; 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.h void main() int a44,t,i,j; for(i=0;i4;i+) for(j=0;j4;j+) aij=rand()A+30; :n; 矩阵cout A 的内容如下 for(i=0;i4;i+) for(j=0;j4;j+) coutaij ; coutendl; for(i=0;i4;i+) for(j=0;ji;j+) - - t=aij; aij=aji; aji=t; cout 转置后矩阵 A 的内容如下 :n; for(i=0;i4;i+) for(j=0;j4;j+) - - coutaij ; coutendl; (3) #include stdlib.h #include iostream.h void main() int a44,b44,c44,i,j,max,imax,jmax; for(i=0;i4;i+) for(j=0;j4;j+) aij=rand()A+30; for(i=0;i4;i+) for(j=0;j4;j+) bij=rand()5+101; :n; 的内容如下cout 矩阵 A for(i=0;i4;i+) for(j=0;j4;j+) coutaij ; coutendl; :n; 矩阵 B 的内容如下cout 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; :n; C 的内容如下矩阵cout 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=cimaxjmax=maxendl; (4) #include stdlib.h #include iostream.h void main() int a44,b44,i,j; for(i=0;i4;i+) for(j=0;j4;j+) aij=rand()A+30; for(i=0;i4;i+) for(j=0;j4;j+) bij=rand()5+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=i;j4;j+) coutbij ; coutendl; (5) #include stdlib.h #include iostream.h void main() int a44,i,j; for(i=0;i4;i+) for(j=0;j4;j+) aij=rand()A+30; - - cout 矩阵 A 的内容如下 :n; for(i=0;i4;i+) for(j=0;j4;j+) coutaij ; coutendl; for(j=0;j4;j+) - - a2j=t; a0j=a2j; int t=a0j; :n; A 的内容如下cout 改变后矩阵 for(i=0;i4;i+) for(j=0;j4;j+) coutaij ; coutendl; (6) #include stdlib.h #include iostream.h void main() int a44,i,j,s=0; for(i=0;i4;i+) for(j=0;j4;j+) aij=rand()A+30; :n; 的内容如下矩阵 A cout 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; sendl; cout 对角线元素之和为: 第五章习题 一、选择题 _A_ 下列叙述错误的是 1.。 A )主函数中定义的变量在整个程序中都是有效的。 ( )复合语句中定义的变量只在该复合语句中有效。B( )其它函数中定义的变量在主函数中不能使用。C ( ()形式参数是局部变量。D 2._B若函数的形参为一维数组,则下列说法中错误的是 C_ 、。- - ( A )形参数组可以不指定大小。 ( B)调用函数时的对应实参必须是数组名。 ( C)调用函数时,系统会为形参数组分配存储单元。 ( D)函数中对形参的修改将会影响实参值。 - - 3. 若函数的类型和 return 语句中的表达式的类型不一致,则 _D_ 。 ( A )编译时出错。 ( B) 运行时出现不确定结果。 ( C)不会出错 ,且返回值的类型以 return 语句中表达式的类型为准。 ( D)不会出错 ,且返回值的类型以函数类型为准。 4. 在函数调用语句 f (g(x,y),z=x+y,(x,y); 中 ,实参的个数是 _A_ 。 (A ) 3 (B) 4 (C)5 (D)7 5. 以下关于定义重载函数的要求中,错误的是 _D_ 。(A )要求参数个数不同。 (B )要求至少有一个参数类型不同。 (C)要求参数个数相同时,参数类型不同。(D )要求函数的返回值不同。 6. 下面的函数定义正确的是 _D_ 。 ( B)A ) float f(float x;float y) (float f(float x,y) return x*y; return x*y; ( D) float f(x,y) )(Cfloat f( int x, int y) int x,y ; return x*y; return x*y; 7. 下面函数头的定义格式正确的是_C_。 (A ) void sort(int an,int n) ( B ) void sort(int a,int n) (C) void sort(int a,int n) (D ) void sort(int a,n) 8. 以下几种模板函数的定义错误的是_C_。 (A ) template T fun1(T a,int b) ? (C) template void fun1(int a,int b) T i; ? ? 125 的有 _C_ 个。 个程序中输出结果9. 下面 4 是 #include (1). iostream.h void cube(int x) x=x*x*x; void main() int x=5;cube(x); coutx; #include iostream.h (3). - - int cube(int x) x=x*x*x; return(x); void main() - - int x=cube(5); / 将 x 改为 5 coutx; (A ) 1 (B)2 (C)3 (D)4 10. 下面函数说明正确的是 _C_。 (A ) f1(int a=3, int b, int c); void (B ) f2 int a, int b=3, int c); void f3(int a, int b, int c=3); void (C) )(D f4(int a, int b, int 3); void *); 利用函数 void m()的说明形式为 m()对数 5 和整数 j 作函数 m()定11. 设函数义 m(int,int 的计算,正确的调用形式为 。_C_ (D )( B)A () m(&5,&j) m(5,j) ) m(5,&j) m(&5,j) ( Cint fun(int a,int 设函数的说明12. &x); 则下面调用该函数正确的是为: _B_ 。 x=fun( a, n); )( fun( a, n); A () B fun( a10, )C( x=fun( a, n); n); )( D 二、写运行结果 1、 2、 3、 4、 5、 6、 三程序填空 1. 【程序说明】分段函数为 float y(float x) if(x=0) return (x*x-x+1); elsreturn x*x*x+x+3; e 【程序说明】2. 【程序段】是把字符指函数 backmove() 针 为非负整数。 m串首。、 n 如“ - - abcdefghij ”,后移动 3 个位置,成“ hijabcdefg ”。 x 所指长度为 n 的字符串后移动 m 个位置,移出的字符移到 void backmove(char *x,int n,int m) int i,j;char w; for(j=0;jm;j+) w= *(x+n-1); - - *(x+n-1-i)= *(x+n-2-i) ; for(i=0; in-1;i+) *x= w ; 3. 【程序说明】 中首次出现的为查找字符串 sub 是否是字符串 st 的子串。若是,返回 sub 在 st 函数 index() 。,返回 2,和 st 非空。如 sub: cd st: abcdefcd -1下标,否则返回。字符串 sub 【程序】 #include “ iostream.h ” #include void main() char s180,s280; int index(char ,char ); 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. 。二分法求根的基本思想为x1,x2 f(x)=0 root 函数为用二分法求方程在的实根,精度为 eps f(a) 符号和 f(b) f(a) 在区间f(x) a,b 上连续,与异号,区间中点c=(a+b)/2 的 f(c) 符号确 c |f(c)|eps |a- b|eps ,,使根所在区间每次减半或a代替定 c b 直到或,即为所求的根,其 。 1 为精度。下面程序为求方程eps 中 在,区间的实根,精度取 10-6 7 【程序】 “#include math.h ” #include double root(double x1, double x2, double eps=1e-6) double f(double x); double x,y,y1; y1=f(x1); - - do x=0.5*(x1+x2); y=f(x); if(y1*y0.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() coutroot(1,7)endl; 5. 【程序说明】下面程序的功能是求出二维数组中最大的元素所在的行号和列号。【程序】 #define SIZE1 3 #define SIZE2 4 #include “ iostream.h ” float max_value(float xSIZE2,int &ii,int &jj) float max; max=x00; for(int i=0;iSIZE1;i+) for(int j=0;jmax) jj=j; max=xiii= i; j; return(max); void main() int i,j,t1,t2;float aSIZE1SIZE2; coutenter the array:n; for(i=0;iSIZE1;i+) for(j=0;j aij; coutmax value is max_value(a,t1,t2); cout line=t1ow=t2=0;-i) chk+=b ci ; chk=0; - - void main() char ch10;int m,h; cinmh; convert(m,h,ch); coutchendl; 四、编写程序 1. #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; cinc1c2; replace(s,c1,c2); coutsendl; 2. 方法一、 #include double area(double r,double *girth,double pi=3.14159) *girth=2*pi*r; return(pi*r*r); - - 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=se

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论