C程序习题答案.doc
参考答案习题二 (P33)一单选题1.C 2.B 3.D 4.C 5.A 6.D 7.D 8.B 9.B 10.D 11.D 12.C 13.C . 15. A14题最后一句应为printf("%fn",d*y);结果为2.2二填空题1. 18 2. int float double 3. 10 11 4. 八 十六 十 5. %三阅读程序题110,10,9,102j=1,i=2 k=3,i=3 j=3,i=2 k=1,i=1习题三 (P52)一单选题1.D 2.C 3.D 4.B 5.A 6.B 7.C 8.A 9.C 10.B 11. -1,二填空题5. L 6. -1三阅读程序题1. 6 6 6.00 6.002. x=127,x= 127,x=177,x=7f Y=123.4567 , y= 123.46 , y=123.456703. 2,14. 12345. 4,36. -6,-6四程序设计题1#include "stdio.h"#include "math.h"main() float a,b,c,d,x1,x2; a=2;b=-3;c=-5; d=b*b-4*a*c; x1=(-b+sqrt(d)/(2*a); x2=(-b-sqrt(d)/(2*a); printf("x1=%.2f,x2=%.2fn",x1,x2);2.#include <stdio.h> main() float a,v,s; scanf("%f",&a); v=a*a*a;s=6*a*a; printf("v=%.2f,s=%.2fn",v,s); 3.#include <stdio.h> main() int a,b,c,t; scanf("%d%d%d",&a,&b,&c); printf("a=%d,b=%d,c=%dn",a,b,c); t=c;c=b;b=a;a=t; printf("a=%d,b=%d,c=%dn",a,b,c); 4.#include <stdio.h> main() char s1,s2; s1=getchar(); s2=s1-32; printf("%cn",s2); 习题四 (P70)一单选题1.C 2.D 3.D 4.B 5.A 6.D 7.D 8.B 9.C 10.A 11.B 12.D 13.passwarnerror . 14.C 15. C 16.B 17. B 18. C二填空题1. 非0 0 2. k=0 3. n%7=0 && n%8=0 else三阅读程序题1. a=1,b=0 2. c=1四程序设计题1.#include <stdio.h> main() int a,b; char c; scanf("%d%c%d",&a,&c,&b); switch(c) case'+':printf("%d+%d=%dn",a,b,a+b);break; case'-':printf("%d-%d=%dn",a,b,a-b);break; case'*':printf("%d*%d=%dn",a,b,a*b);break; case'/':printf("%d/%d=%dn",a,b,a/b);break; default:printf("INPUT ERROR!"); 2.#include <stdio.h> main() float x,y; scanf("%f",&x); if(x=0|x=2)y=0; else if(x>0) y=(x+1)/(x-2); else y=(x-1)/(x-2); printf("y=%fn",y);3.#include <stdio.h> main() int g; printf("请输入学生成绩:"); scanf("%d",&g); printf("g=%d:",g); switch(g/10) case 10:printf("An");break; case 9:printf("Bn");break; case 8:printf("Cn");break; case 7:printf("Dn");break; case 6:printf("En");break; default:printf("Fn"); 4.#include <stdio.h>int main(void)int x,i; printf("Enter x:n"); scanf("%d",&x); if (x/1000) i=4; else if (x/100) i=3; else if (x/10) i=2; else i=1;printf(“%d是%d位数”,x,i); return 0;习题五 (P87)一单选题1.B 2.D 3.C 4.C 5.B 6.C 7.D 8.C 9.A 10.D 11.B 12.B 13.B 14.D 15. C 二填空题1. for语句 while语句 do-while语句2. a=14,y=263. k=14,n= -14. 395. s=196. 222227. 108. 79. 810. 5,5三改错题应改为for(i=0;i<5;i+)j+;应改为int j=0;while(j<10)j+;i=j; 注while(j<10)后不应有分号应改为while(j<10); 注while(j<10)后分号不能少s*=i;i+;应改为s*=i;i+; continue 应改为 break四程序设计题1.#include <stdio.h> main() int i,j,s=0; for(i=1,j=1;i<=100;i+,j=j*(-1) s=s+i*j; printf("s=%dn",s);1.(另一做法)#include<stdio.h>main() int i,s; for(i=1,s=0;i<=100;i+) if(i%2=0)s=s-i; else s=s+i; printf ("%dn",s);2#include <stdio.h> main() int i,j,s1=0,s2=0; for(i=0;i<10;i+) scanf("%d",&j); if(j>0)s1=s1+j; else s2=s2+j; printf("s1=%d,s2=%dn",s1,s2);3#include<stdio.h>main() int i,s; for(i=6,s=0;i<=96;i=i+1) if(i%10=6|i/10=6) s=s+i; printf ("%dn",s);4#include<stdio.h>main() int i,a,b,c; for(i=100;i<=999;i+)a=(i%10);b=(i/10%10);c=(i/100); if(i=a*a*a+b*b*b+c*c*c) printf ("%dn",i);5#include <stdio.h>main( ) int i,j,k=0; for (i=1;i<=4;i+) for (j=1;j<=i;j+) k+;printf("%d",k%10); printf("n"); 6.#include <stdio.h>main( ) int i,j,k=0; for (i=-3;i<=3;i+) if(i<0)k=-i;else k=i; for(j=1;j<=k;j+) printf(" "); for(j=1;j<=7-2*k;j+) printf("*"); printf("n");习题六 (P111)一单选题1.D 2.B 3.C 4.C 5.C 6.D 7.A 8.B 二填空题1. 20 0 192. 数组名3. 越界4. 65. j=k ajk=1; ajk=0;三阅读程序题1 6 5 4 3 2 12 aaa bbb ccc ddd3 2,2,1四程序设计题1.#include<stdio.h>main() int a44=1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16; int i,j,s=0; for(i=0;i<4;i+) for(j=0;j<4;j+) if(i=j|i+j=3)s+=aij; printf("%d",s);2.#include <stdio.h> main() char a80; int i,j=5; /*假设删除位置为5*/ gets(a); for(i=j-1;ai!='0'i+) ai=ai+1; ai='0' puts(a);3#include <stdio.h> #include <string.h> main() char a80; int i,j=5; /*假设插入位置为5*/ char s='t' /*假设插入字符为t*/ gets(a); for(i=strlen(a);i>j;i-) ai+1=ai; aj='t' puts(a);4#include<stdio.h>main() int a35=1,3,5,7,9,2,4,6,8,10,3,5,8,7,6; int i,j,s13=0,s25=0; for(i=0;i<3;i+) for(j=0;j<5;j+) s1i+=ai j; for(i=0;i<5;i+) for(j=0;j<3;j+) s2i+=a ji; for(i=0;i<3;i+) for(j=0;j<5;j+) printf("%6d",aij); printf (" |%6dn",s1i); for(i=0;i<33;i+) printf("-"); printf("n"); for(i=0;i<5;i+) printf("%6d",s2i); printf("n");5#include<stdio.h>main() char s380; int a=0,b=0,c=0,d=0,e=0,i,j; for(i=0;i<3;i+) gets(si); for(i=0;i<3;i+) for(j=0;sij!='0'j+) if(sij>='A'&&sij<='Z')a+;else if(sij>='a'&&sij<='z')b+;else if(sij>='0'&&sij<='9')c+; else if(sij=32)d+;else e+; printf("%3d%3d%3d%3d%3d",a,b,c,d,e);习题七 (P145)一单选题1.A 2.B 3.C 4.A 5.C 6.D 7.C 8.B 9.B 10.C 11.D 二填空题1. 该函数内 局部2. 整型3. k<=b return y;4. xi return (ave); fun(a,20)5. 1; add(n-1); add(n);6. n*f(n-1)0;f(i)三阅读程序,写出运行结果1 10,20,302 643 84. hlo5. sum=55四程序设计题1.#include <stdio.h>main()float add(float, float), sub(float, float);float aver(float, float),a,b;scanf("%f,%f",&a,&b);printf("add=%f, sub=%fn ", add(a,b), sub(a,b);printf("aver =%f", aver(a,b);float add(float x, float y) return(x+y);float sub(float x, float y) return(x-y);float aver(float x, float y) return(x+y)/2);2.#include<stdio.h>main() int a,b,c,abmax(int,int); scanf("%d%d%d",&a,&b,&c); printf("max=%dn",abmax(abmax(a,b),c);int abmax(int a,int b)if(a>b)return a; else return b;3.#include <stdio.h>main()float x,fun(float, int);int n; scanf("%f%d",&x,&n);printf("%fn", fun(x,n);float fun(float x, int n)float y;if(n=0)y=1;else y=x*fun(x,n-1);return y;4.#include <stdio.h>main()long f(int);int n,k; scanf("%d",&n);for(k=1;k<=n;k+)printf("%ld,",f(k);printf("n");long f(int n)long y;if(n<=2)y=1;else y=f(n-1)+f(n-2);return y;5.#include <stdio.h>#include <string.h>main() char a50;int n;void fun(char x ,int n); gets(a);n=strlen(a); fun(a,n); puts(a); void fun(char a ,int n)int k,s; for(k=0;k<n/2;k+)s=ak;ak=an-k-1;an-k-1=s;5. (另一做法)#include <stdio.h>#include <string.h>main() char a50;int n;void fun(char x ,int n); gets(a);n=strlen(a); fun(a,n); puts(a); void fun(char a ,int n)int k; char b50; for(k=0;k<n;k+) bn-1-k=ak; bk=ak;strcpy(a,b);6.#include <stdio.h>#define KK 100main() char aKK;long sjz(char a);int i=0,f1=0;printf("input a data:");gets(a);for(i=0;ai!='0'i+)if(ai>='0'&&ai<='9'|ai>='A'&&ai<='F'|ai>='a'&&ai<='f') continue; else f1=1;break;ai='0'if(f1=1)printf("Data Error!n");else printf("result is :%dn",sjz(a);long sjz(char a)long n=0,i; for(i=0;ai!='0'i+) if(ai>='0'&&ai<='9')n=n*16+ai-'0' if(ai>='A'&&ai<='F')n=n*16+ai-'A'+10; if(ai>='a'&&ai<='f')n=n*16+ai-'a'+10; return n;6.(另一做法)#include <stdio.h>#define KK 100main() char strKK,c;long sjz(char a);int i=0,f1=0;printf("input a data:");while(c=getchar()!='n'&&i<KK)if(c>='0'&&c<='9'|c>='A'&&c<='F'|c>='a'&&c<='f') stri+=c; else f1=1;stri='0'if(f1=1)printf("Data Error!n");else printf("result is :%dn",sjz(str);long sjz(char a)long n=0,i; for(i=0;ai!='0'i+) if(ai>='0'&&ai<='9')n=n*16+ai-'0' if(ai>='A'&&ai<='F')n=n*16+ai-'A'+10; if(ai>='a'&&ai<='f')n=n*16+ai-'a'+10; return n;习题八 (P181) (注:无答案的题不在本课程讲授范围内)一单选题1.D 2.A 3.C 4.D 5.C 6.B 7.C 8.C 9.C 13.D 14.B 15.A 16.C 17.A二填空题1. 指向 取地址2. 2 +23. 286. *(p+5)7. ABCD A三阅读程序题1 102 103 04. 3,65. 1 2 3 4四程序填空题1a,b,c或者&x,&y,&z max=*b或者max=y max=*c或者max=z2. + =*q + +4. int *a,int *b bj=ai bi+5. *str+=3 *str>z&&*str<a|*str>z a6. else 0 t2*j+1五程序设计题1.#include <stdio.h>main()int a10,*p,*q,t;p=a;for(q=a;q<a+10;q+)scanf("%d",q);for(q=a;q<a+10;q+)printf("%5d",*q);printf("n");q-;for(;p<q;p+,q-)t=*p;*p=*q;*q=t;for(p=a;p<a+10;p+)printf("%5d",*p);printf("n");2.#include <stdio.h>#include <string.h>void main( ) char a50,*p1,*p2,t,n; gets(a);n=strlen(a); p1=a; p2=a+n-1; for(; p1<p2; p1+, p2-) t=*p1; *p1=*p2; *p2=t; puts(a);另一做法#include <stdio.h>main()char str20,*p=str;gets(str);while(*p)p+;p-;while(p>=str)printf("%c",*p);p-;printf("n");3.#include <stdio.h>main()int a10,*p,*max,*min,t;for(p=a;p<a+10;p+)scanf("%d",p);for(p=a;p<a+10;p+)printf("%5d",*p);printf("n");max=min=a;for(p=a+1;p<a+10;p+)if(*p>*max)max=p;if(*p<*min)min=p;t=a0;a0=*min;*min=t;t=a9;a9=*max;*max=t;for(p=a;p<a+10;p+)printf("%5d",*p);printf("n");5.#include <stdio.h>int length(char *s)int n=0;while(*s)n+;s+;return n;main()char str20;int n;gets(str);n=length(str);printf("The string length is %dn",n);6.#include <stdio.h>main()char str81,*p=str,*q,t;gets(str);printf("The origenal string:n");puts(str);for(p=str;*(p+1);p+) for(q=p+1;*q;q+) if(*q<*p)t=*p;*p=*q;*q=t;printf("The result string:n");puts(str);习题九 (P222) (注:无答案的题不在本课程讲授范围内)一单选题1.D 2.A 3.B 4.D 8.B 9.B 10.C 二填空题1. 结构体成员 结构体指针指向2. 343. 224 ex三阅读程序题1 92 10,x3 134. 46 40 415. 06. 3839