C语言程序设计(科学出版社)第5章 课后习题参考答案

上传人:仙*** 文档编号:135677629 上传时间:2022-08-15 格式:DOC 页数:12 大小:87.50KB
收藏 版权申诉 举报 下载
C语言程序设计(科学出版社)第5章 课后习题参考答案_第1页
第1页 / 共12页
C语言程序设计(科学出版社)第5章 课后习题参考答案_第2页
第2页 / 共12页
C语言程序设计(科学出版社)第5章 课后习题参考答案_第3页
第3页 / 共12页
资源描述:

《C语言程序设计(科学出版社)第5章 课后习题参考答案》由会员分享,可在线阅读,更多相关《C语言程序设计(科学出版社)第5章 课后习题参考答案(12页珍藏版)》请在装配图网上搜索。

1、12 《C语言程序设计》习题参考答案 第5章 习题参考答案 1 判断题 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 √ × √ × × √ × × × × × × × × × 2 填空题 (1) 32 (2) 4 (3) str1[7]= '\0'; (4) for(i=0;i<6;i++) gets(a[i]); (5) strcpy(web, ""); (6) int *p=a; (7) 5 (8) 1 (9) *(p+6)或 *(s+6) 3 选择题 1 2

2、3 4 5 6 7 8 9 10 11 12 13 14 15 BD C B A B C B A C D B D B A D 4 程序阅读题 (1) 6 (2) str (3) 60 (4) bcdefga cdefgab defgabc (5) a1e2 5 程序填空题 (1) # include scanf("%f", &a[i]) s=0; x=a[0] fabs(a[i]-s) (2) scanf("%d",&n) a[i][i]=1 a[i-1][j-1] printf(

3、"\n") (3) b[i]!='\0' strcpy puts(b) (4)p=a[0]; 或p=&a[0][0]; *p 六、编程题 1、使用数组和指针2种方法来处理。 方法一(使用数组) #include void main() { int i=0,a[10],t; printf("Enter the 10 number=\n"); for(i=0; i<10; i++) scanf("%d",&a[i]); for(i=0;i<5;i++) { t=a[i];a[i]=a[9-i];a[9

4、-i]=t;} for(i=0; i<10; i++) printf("%d ",a[i]); printf("\n"); } 方法二(使用指针) #include void main() { int i=0,a[10],t,*p,*q; printf("Enter the 10 number=\n"); for(p=a; p

5、*q=t;} for(p=a; p main() { int i, j, t, n, a[11]={0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; for(i=0;i<10;i++) printf("%d ", a[i]); printf("\n"); printf("Enter the Move n=?"); scanf("

6、%d", &n); for(i=1; i<=n; i++) { t=a[10]; for(j=9;j>=0;j--) a[j+1]=a[j]; a[0]=t; } for(i=0; i<=10; i++) printf("%d ", a[i]); getch(); } 方法二(使用指针) #include void main() { int i,t, n, a[11]={0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10},*p=a; for(;p

7、 printf("%d ", *p); printf("\n"); printf("Enter the Move n=?"); scanf("%d", &n); for(i=1; i<=n; i++) { p=a+10; t=*p; for(;p>a;p--) *p=*(p-1); *p=t; } for(p=a;p #include main() { in

8、t a[100], b[100], i, j, p, n=0, t; randomize(); for(i=0; i<100; i++) { a[i]=random(100); if(a[i]%2==0) b[n++]=a[i]; } for(i=0; ib[p]) p=j; t=b[p]; b[p]=b[i]; b[i]=t; } for(i=0; i

9、printf("%d ", b[i]); if(i%10==9) printf("\n"); } getch(); } 4、 #include #include #define N 50 main() { int a[N], i, j,p,t; randomize(); for(i=0; i

10、 break; } } for(i=0; ia[p]) p=j; t=a[p]; a[p]=a[i]; a[i]=t; } for(i=0; i #define N 10 void m

11、ain() { int i; float a[N],s=0.0,x0=0.0; for(i=0;i

12、put score\n"); for(i=0;i

13、 a[i][M]=sum; /*求第i个学生的总成绩放到a[i][M]*/ } max=a[0][M]; p=0; for(i=1;imax) p=i; printf("No.%d Total score:%d\n",p+1,a[p][M]); } 7、 #include void main() { int a[5][6]; int i, j,

14、 mi, mj; for(i=0; i<5; i++) for(j=0; j<6; j++) { printf("input a[%d][%d]:", i, j); scanf("%d", &a[i][j]); } mi=0; mj=0; for(i=0; i<5; i++) { for(j=0; j<6; j++) { if(a[i][j]

15、printf("\n"); } printf("min=%d ",a[mi][mj]); } 8、 #include #include void main() { float a[5][6]; int i, j, max; printf("Enter array a=?\n" ); for(i=0; i<5; i++) for(j=0; j<6; j++) scanf("%f", &a[i][j]); for(i=0; i<5; i++) { max=a

16、[i][0]; for(j=0; j<6; j++) /*查找第i行中绝对值最大的元素*/ if(fabs(a[i][j])>fabs(max)) max=a[i][j]; for(j=0; j<6; j++) /*第i行中所有元素除以绝对值最大的元素*/ { a[i][j]=a[i][j]/max; printf("%8.4", a[i][j]); } printf(" -----max =%8.4\n",max); } }

17、 9、 #include #include #define N 20 main() { char str[N][15],st[15],*p=st; int i, j; for(i=0; i0) { strcpy(p,str[j]);

18、 strcpy(str[j],str[j+1]); strcpy(str[j+1],p); } for(i=0; i main() { int a[10], b[10], t; int n=0, m=0, i, j, f; printf("Input Array a, end with –1:"); scanf("%d", &t); while(n<1

19、0 && t!=-1) { a[n++]=t; scanf("%d", &t); } printf("Input Array b, end with –1:"); scanf("%d", &t); while(m<10 && t!=-1) { b[m++]=t; scanf("%d", &t); } for(i=0; i

20、 if(f) printf("%d", a[i]); } for(i=0; i main() { char str[81]; int i=-1; printf("Input:"); scanf("%s", s

21、tr); while(++i<80 && str[i]!= '\0') if(str[i]== '$') str[i]= 'S'; puts(str); } 12、 #include void main() { char x[80],y[26]; int i,j,ny=0; gets(x); for(i=0;x[i]!='\0';i++) if(x[i]>='A'&&x[i]<='Z') { for(j=0;j

22、k; if(j==ny) { y[ny]=x[i]; ny++; } } for(i=0;i void main() { int m, bin[32],j; scanf("%d",&m); for(j=0;m!=0;j++) { bin[j]= m%2; m=m/2; } for(;j!

23、=0;j--) printf("%d", bin[j-1] ); } 14、 #include #include #include void main() { char s[80]; int i=0; gets(s); while(s[i]!='\0') if(isdigit(s[i])) strcpy (s+i,s+i+1); else i++; puts(s); } 15、 void main() { int a,b,c; int *p1,*p2,

24、*p3,*t; printf("\nPlease input three integers:\n"); scanf("%d,%d,%d",&a,&b,&c); p1=&a,p2=&b,p3=&c; if(*p1>*p2) t=p1,p1=p2,p2=t; if(*p1>*p3) t=p1,p1=p3,p3=t; if(*p2>*p3) t=p2,p2=p3,p3=t; printf("new order:\n"); printf("%d,%d,%d",*p1,*p2,*p3); } 16、 void main() { char st[80]

25、,ss[20],sp[20],*a=st,*b=ss,*t=sp; int i,n,m,p=-1; printf("\nPlease input two string:\n"); gets(a);gets(b); n=strlen(a); m=strlen(b); for(i=0;i<=n-m+1;i++) { a=st+i; strncpy(t,a,m); /*将字符指针a指向的字符串取前m个字符复制到t所指的字符串中*/ if(strcmp(b,t)==0) {p=i;

26、 break;} } if(p==-1) printf("%d",p); else printf("%d",p+1); /*因数组下标从0开始,所以输出p+1*/ getch(); } 17、 #include void main() { char st[80],*p=st,ch; printf("Enter a string\n"); gets(p); while(*p!='\0') { ch=*p; if(ch>='A'

27、 && ch<='Z') ch='A'+('Z'-ch); else if(ch>='a' && ch<='z') ch='a'+('z'-ch); *p=ch; /*转换后的字符替换原字符*/ p++; /*指针指向下一个字符*/ } /*指针指向字符串首字符*/ p=st; puts(p); getch(); } 18、 #include void main() { char st[80],*p=st,c

28、h; long k=0; printf("Enter a string\n"); gets(p); strupr(p); while(*p!='\0') { ch=*p; if(ch>='0' && ch<='9' || ch>='A' && ch<='F') p++; else strcpy(p,p+1); /*滤去所有的非十六进制字符*/ } p=st; while(*p!='\0') /*将十六制数转换成十进数*/ { ch=*p; if(ch>='0' && ch<='9' ) k=16*k+(ch-'0'); else if(ch>='A' && ch<='F') k=16*k+(10+ch-'A'); p++; } printf("%ld\n",k); }

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

相关资源

更多
正为您匹配相似的精品文档
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!