C语言程序设计(张淑华、朱丽莉主编)课后习题解答(仅供参考)习题一一、选择题1.B 2.D 3.B 4.A 5.B6.C 7.C二、填空题1..obj 2.函数3./* */ 4.#include <stdio.h> int a,b; a,b三、简答题(略)习题二一、选择题1.B 2.B 3.A 4.D 5.A6.C 7.C 8.A 9.C 10.B11.A 12.A 13.C 14.A 15.C二、填空题1.变量2.存储数据的单元3.非0、0 4.(1)a>=0 (2)0 5.1 6.1 7.-30 8.38 9.4 , 7 10.-6 177772 11.9.9 12.7三、写出下列程序的运行结果1.0,0,5 2.18 3.64 512 8 4.55.d4,c四、编程题1.#include <stdio.h>#define PI 3.1415926void main(){ float r,s;printf(“Please input circinal radii\n”);scanf(“%f”,&r);s=PI*r*r;printf(“r=%.2f,s=%.2f\n”,r,s);}2.#include <stdio.h>void main(){ char c1,c2;printf(“Please input a majuscule\n”);scanf(“%c”,&c1);c2=c1>’A’&&c1<’Z’?c1+32:c1;printf(“%c\n”,c2);}3.#include <stdio.h>void main(){ int a=0,b;scanf(“%c”,&b);b=b&a;printf(“b=%d\n”,b);}习题三一、选择题1.C 2.C 3.A 4.C 5.D6.原题第2行应为c=(c-=a-5),(a=b,b+3); 答案为B 否则输出结果为5,0,57.D 8.C 9.A 10.B二、填空题1.-50 300、a=-50,b=300a=-50 b=3002.10 20 30、10,20,30、a=10,b=20,c=303.15、0、0三、写出下列程序的运行结果1.a=789.654321,a= 789.65,a= 789.65432100,a=789.6543212.b=15,b=17,b=f 3.c=1,x=0,y=0,z=1 4.2120,205.在TC环境下为216,125 在VC环境下为216,1806.a=A,b=d,c=F7.9,4,12,12 8.x=4.500000,y=4,x+y=8.500000四、编程题1.#include <stdio.h>void main(){ printf("**********************************\n");printf(" HAPPY NEW YEAR! \n");printf("**********************************\n");}2.(1) #include <stdio.h>void main(){ int a=5,b=8,c=10,m,max;m= a>b?a:b;max=c>m?c:m;printf(“max=%d\n”,max);}(2)#include <stdio.h>void main(){ int a,b,c,m,max;scanf("%d,%d,%d",&a,&b,&c);max=a>b?a:(b>c?b:c);printf(“max=%d\n”,max);}习题四一、选择题1.D 2.D 3.C 4.A 5.D 6.A 7.C 8.C 9.D 10.B二、填空题1.6、5、5 2.(a%3==0||a%7==0) 3.(1) a>b (2) a=c; (3) a,b,c三、写出下列程序的运行结果1.4 2.64 3.a=7,b=4 4.46 46 5.*#46四、编程题1.另见填空题3#include <stdio.h>void main(){ float a,b,c,t;scanf("%f,%f,%f",&a,&b,&c);if (a>b){ t=a;a=b;b=t;}if (a>c){t=a;a=c;c=t;}if (b>c){t=b;b=c;c=t;}printf(“%.2f,%.2f,%.2f\n”,a,b,c);}2.#include <stdio.h>void main(){ int a,b;scanf("%d,%d",&a,&b);if(a%b==0)printf(“%d is divisible by %d\n”,a,b);elseprintf(“%d is not divisible by %d\n”,a,b);}3.(1) #include <stdio.h>void main(){ int x,y;scanf(“%d”,&x);if (x>-5&&x<0)y=x;printf("y=%d\n",y);else if (x==0)y=x-1;printf("y=%d\n",y);else if(x>0&&x<10)y=x+1;printf("y=%d\n",y);elseprintf("Data out of range\n");}(2) #include <stdio.h>void main(){ int x,y;scanf(“%d”,&x);switch(x){ case -1:case -2:case -3:case -4:y=x; printf("y=%d\n",y);break;case 0:y=x-1; printf("y=%d\n",y);break;case 1:case 2:case 3:case 4:case 5:case 6:case 7:case 8:case 9:y=x+1; printf("y=%d\n",y);break;default: printf("Data out of range\n");}4.#include <stdio.h>void main(){long num;int indiv,ten,hundred,thousand,tenthousand,place; /*分别代表个位、十位、百位、千位、万位和位数*/printf(“Please Input a integer(0~99999):”);scanf(“%ld”,&num);if (num>9999)place=5;else if (num>999)place=4;else if (num>99)place=3;else if (num>9)place=2;elseplace=1;printf(“place=%d\n”,place);printf(“For each digit:”);tenthousand=num/10000;thousand=(int)(num-tenthousand*10000)/1000;hundred=(int)(num-tenthousand*10000-thousand*1000)/100;ten=(int)(num-tenthousand*10000-thousand*1000-hundred*100)/10;indiv=(int)(num-tenthousand*10000-thousand*1000-hundred*100-ten*10);switch(place){case 1:printf(“%d”,indiv);break;case 2:printf(“%d,%d”,ten,indiv);break;case 3:printf(“%d,%d,%d”,hundred,ten,indiv);break;case 4:printf(“%d,%d,%d,%d”,thousand,hundred,ten,indiv);break;case 5:printf(“%d,%d,%d,%d,%d”,tenthousand,thousand,hundred,ten,indiv);}}习题五一、选择题1.D 2.C 3.D 4.B 5.C6.B 7.C 8.B 9.B 10.B二、填空题1.(1) x>=0 或!(x<0) (2) x<amin2.(1) i<10 或i<=9 (2)j%3 或j%3!=03.(1) pi (2) fabs(t) (3)4三、写出下列程序的运行结果1.1 2.y=-1 3.4 4.a=2,b=8 5.*7 6.45 7.1 8.s=03423四、编程题1.#include <stdio.h>void main(){int i,j,k;for (i=1;i<=4;i++) /*输出上面4行*号*/{ for (j=1;j<=5-i;j++)printf(““); /*输出*号前面的空格*/for (k=1;k<=2*i-1;k++)printf(“*”); /*输出*号*/printf(“\n”); /*输出完一行*号后换行*/}}2.#include "stdio.h"void main(){ int num,n1,n2,p=1;scanf("%d",&num);while (num){ n1=num/10;n2=num%10;p*=n2;num=n1;}printf("num=%d,p=%d\n",num,p);}3.#include "stdio.h"void main(){ int i;float s1,s2,s3,s4,s5,sum,avg;for(i=1;i<=6;i++){ printf("Please enter a five subjects score\n");scanf("%f,%f,%f,%f,%f",&s1,&s2,&s3,&s4,&s5);sum=0;sum+=s1+s2+s3+s4+s5;avg=sum/5;printf("%d individuals,the average score is %.2f\n",i,avg);}}习题六一、选择题1.D 2.C 3.D 4.D 5.B6.A 7.C 8.D 9.A二、填空题1.地址常量2.非0 3.3.(1) j=9 (2) j>=0 4.(1) 0 (2) 6 5.(1) gets(temp); (2) strcpy(temp,str); 6.(1) i<3 (2) a[i][i] 7.(1) &a[i] (2) i%4==0 (3)printf("\n:\");8.(1) s[i]>=48 && s[i]<=57 或s[i]>='0' && s[i]<='9' (2) d[j]=0; 或d[j]='\0';三、写出下列程序的运行结果1.13715 2.s=15 3.58 4.11 5.7 0 6.how 7.max=9,row=1,col=0 8.6,4,3,2, are you? I am fine四、编程题1.#include <stdio.h>#define N 10void main(){ int a[32],x,k,r,i;printf(“Please input an integer:\");k=-1;do{ r=x%2;a[++k]=r;x/=2;}while(x>=1);for(i=k;i>=0;i--)printf(“%1d”,a[i]);printf(“\n”);}2.#include <stdio.h>#define N 10void main(){ int a[N+1]={10,20,30,40,50,60,70,80,90,100};int t1,t2,num,i,j;printf(“array a:\n”);for (i=0;i<N;i++)printf(“%d”,a[i]);printf(“\n”);printf(“Insert data:”);scanf(“%d”,&num);if (num>a[N-1])a[N]=num;else{ for (i=0;i<N;i++){ if (a[i]>num){ t1=a[i];a[i]=num;for (j=i+1;j<N+1;j++){ t2=a[j];a[j]=t1;t1=t2;}break;}}}printf("Current array a:\n”);for (i=0;i<N+1;i++)printf(“%5d\n”,a[i]);}3.#include <stdio.h>#define N 6void main(){ int i,j,a[N][N];for (i=1;i<N;i++){ a[i][i]=1;a[i][1]=1;}for (i=3;i<N;i++)for (j=2;j<i-1;j++)a[i][j]=a[i-1][j-1]+a[i-1][j];for (i=1;i<N;i++){ for (j=1;j<=i;j++)printf(“%5d ”,a[i][j]);printf(”\n”);}}4.#include "stdio.h"void main(){ char s[80],t[80],d;int i,j;printf("Please enter a string:\n");gets(s);printf("delete?\n");d=getchar();for(i=j=0;s[i]!='\0';i++)if(s[i]!=d){ t[j]=s[i];j++;}t[j]='\0';printf("%s\n",t);}5.#include "stdio.h"#define N 8void main(){ int s[N][3],i,j,k,m,t1;float a[N],sum,t2;for(i=0;i<N;i++){ printf("Please enter the %d-th three subjects score\n",i);sum=0;for(j=0;j<3;j++){ scanf("%d",&s[i][j]);sum+=s[i][j];}a[i]=sum/3;}for(i=0;i<N-1;i++){ k=i;for(j=i+1;j<N;j++)if(a[k]<a[j]) k=j;if(k!=i){ for(m=0;m<3;m++){ t1=s[i][m];s[i][m]=s[k][m];s[k][m]=t1;} /*交换整行的值*/t2=a[i];a[i]=a[k];a[k]=t2;}}for(i=0;i<N;i++){ printf("%d-th ",i);for(j=0;j<N;j++)printf("%d ",s[i][j]);printf("%.2f\n",a[i]);}}习题七一、选择题1.C 2.A 3.B 4.B 5.A 6.D 7.C 8.D 9.C 10.C二、填空题1.struct STRU t[20]; 2.x.x、x.y、x.z.y、x.z.z3.4,8 三、写出下列程序的运行结果1.6 2.0 3.4E5S 4.21 5.Zhao,m,85,90 四、编程题1.#include "stdio.h"#define N 45struct student{ char name[20];char sex[7];int age;int score;}std[N];void main(){ int i,max=0,rowi;for(i=0;i<N;i++){ printf("Please enter the data of the %d-th student:\n",i);printf("name:");scanf("%s",std[i].name);printf("sex:");scanf("%s",std[i].sex);printf("age:");scanf("%d",&std[i].age);printf("score:");scanf("%d",&std[i].score);if(max<std[i].score){ max=std[i].score;rowi=i;}}printf("name:%s ",std[rowi].name);printf("sex:%s ",std[rowi].sex);printf("age:%d ",std[rowi].age);printf("score:%d\n",std[rowi].score);}2.#include "stdio.h"#define N 5struct student{ char name[20];char sex[7];int score;}std[N];void main(){ int i,ml=0,fml=0;float sum=0,avg;for(i=0;i<N;i++){ printf("Please enter the data of the %d-th student:\n",i);printf("name:");scanf("%s",std[i].name);printf("sex:");scanf("%s",std[i].sex);if(std[i].sex[0]=='m'||std[i].sex[0]=='M')ml+=1;elsefml+=1;printf("score:");scanf("%d",&std[i].score);sum+=std[i].score;}avg=sum/N;printf("The number of male students:%d\n",ml);printf("The number of female students are:%d\n",fml);printf("The average scores are:%.2f\n",avg);}习题八一、选择题1.A 2.C 3.D 4.A 5.C6.A 7.D二、填空题1.(1) high=mid-1 (2) low=mid+1 2.(1) int m (2) m%i==0 (3) return s 3.(1) max=x[i][j] (2) max三、写出下列程序的运行结果1.625 2.Qian,p,95,92 3.2 4.12 5.h=8四、编程题1.#include "stdio.h"void main(){ int prime(int);int x;printf("\nPlease input an integer:");scanf("%d",&x);if(prime(x))printf("\n%d is prime.\n",x);elseprintf("\n%d is not prime.\n",x);}int prime(int n){ int flag=1,i;for(i=2;i<=n-1 && flag==1;i++)if(n%i==0)flag=0;return(flag);}2.#include "stdio.h"void delask(char a[]){ int i,j;char b[80];for(i=j=0;a[i]!='\0';i++)if(a[i]!='*'){ while(a[i]!='\0'){ b[j]=a[i];j++;i++;}break;}b[j]='\0';for(i=0;b[i]!='\0';i++)a[i]=b[i];a[i]='\0';}void main(){ char s[80];printf("Please enter a string:\n");scanf("%s",s);delask(s);printf("%s\n",s);}3.#include "stdio.h"struct date{ int day;int month;int year;};int days(struct date dt){ int y,m,d,ds[12]={31,28,31,30,31,30,31,31,30,31,30,31},i,s=0;y=dt.year;d=dt.day;if(y%4==0 && y%100!=0||y%400==0) ds[1]=29;/* 计算每月的天数是本年第几天*/ switch(m){ case 1:for(i=0;i<m-1;i++)s+=ds[i];s+=d;break;case 2:for(i=0;i<m-1;i++)s+=ds[i];s+=d;break;case 3:for(i=0;i<m-1;i++)s+=ds[i];s+=d;break;case 4:for(i=0;i<m-1;i++)s+=ds[i];s+=d;break;case 5:for(i=0;i<m-1;i++)s+=ds[i];s+=d;break;case 6:for(i=0;i<m-1;i++)s+=ds[i];s+=d;break;case 7:for(i=0;i<m-1;i++)s+=ds[i];s+=d;break;case 8:for(i=0;i<m-1;i++)s+=ds[i];s+=d;break;case 9:for(i=0;i<m-1;i++)s+=ds[i];s+=d;break;case 10:for(i=0;i<m-1;i++)s+=ds[i];break;case 11:for(i=0;i<m-1;i++)s+=ds[i];s+=d;break;case 12:for(i=0;i<m-1;i++)s+=ds[i];s+=d;break;}return (s);}void main(){ struct date ymd;int dys;printf("Input year month and day\n");scanf("%d,%d,%d",&ymd.year,&ymd.month,&ymd.day);dys=days(ymd);printf("Today is the first %d days in the middle of this year.\n",dys);}习题九一、选择题1.A 2.D 3.A 4.D 5.B 6.A 二、写出下列程序的运行结果1.10 2.9 11 11 3.*** 4.12 5.8 6.1 11 21 3三、编程题1.#include "stdio.h"#define N 10float fmax=0,fmin=0;float average(float fscore[],int n){ int i;float faver,fsum=fscore[0];fmax=fmin=fscore[0];for(i=1;i<n;i++){if(fscore[i]>fmax)fmax=fscore[i];else if(fscore[i]<fmin)fmin=fscore[i];fsum+=fscore[i];}faver=fsum/n;return (faver);}void main(){ float fave,fnumb[N];int i;for(i=0;i<N;i++)scanf("%f",&fnumb[i]);fave=average(fnumb,N);printf("max=%.2f min=%.2f average=%.2f\n",fmax,fmin, fave);}2.此题有误,用宏定义较难实现。