1注:以下内容为教材部分习题答案,如发现错误请告知,欢迎讨论——张惕远习题二一、选择题1、C2、BCD3、A4、A(注:本题强调用户标识符,所以if、int不是,但是sin是用户标识符,原因是库函数不是标准语法成分,开发库函数的程序员也是用户,main虽然不是语法成分,但开发平台预定义了main为consoleapplication的执行入口,并且规定了定义格式,所以用户不能用作它用,但_main可以)5、(D)(C)6、C7、D8、B9、A10、D11、B(注:本题是求strlen(\\\”DEF\”\\”)的值,即字符串中有效字符的个数,不含串结尾符,sizeof(“…”)的值与前者不同,含串结尾符)12、D13、C14、D15、A16、B17、A18、B二、判断题(对)(错)(错)(对)三、填空题1、B66回车22、n1=%d\nn2=%d3、5四、计算下列各表达式的值1、注:本书假设采用16位编译平台,所以int和unsignedint均为2字节类型。(1)47(2)38(3)9(4)0xFFC6or65478(5)104(6)22、(1)6(2)50(3)1(4)-16or0xFFF0(5)1(6)203、表达式值a的值b的值(1)053(2)153(3)313(4)151043(5)883(6)1534、表达式值x的值y的值(1)12252(2)0243(3)1243(4)27243(5)1243(6)663(7)242424(y=3)(8)27243(9)-29or0xffe3243以上类型均为int5、表达式值x的值y的值(1)001(2)121(3)100(4)-3-33(5)424以上类型均为int五、程序分析题b=20(注:中间4个空格)a=3600回车4习题三一、填空题1、s=6回车2、s=96回车3、(1)c=getchar()orscanf(“%c”,&c)(2)c-32orc-‘a’+’A’习题四一、写出程序运行结果:1、no1a=1no1a=0no1a=1no1a=0(注:教材中关于func函数的原型有错,应该改为:voidfunc();)2、a=0b=0c=0a=1b=0c=0a=2b=0c=03、main:x=5,y=1,n=1fun:x=6,y=21,n=11main:x=5,y=1,n=11fun:x=8,y=31,n=21习题五一、选择题和填空题1、D2、D3、C4、65、CDABC6、(1)j+=2(2)a[j]a[i](注:教材中for语句有错,应该去掉表达式3后面的分号“;”)最后输出为unalggace7、(1)s[j](2)s[j]58、D9、B10、611、isdigit(s[i])该带参数的宏用来判断字符s[i]是不是数字字符,该宏的定义在头文件ctype.h中12、‘\0’和str1[i]-str2[i]二、1、第7行sum=0;第10行改为:scanf(“%d”,&a[i][j]);习题六一、选择题1、D2、D3、D4、C5、CD6、D7、B8、B二、填空题1、*(p+3)2222、cdefgbcdefgabcdefg73、6385三、程序分析题9、第5行改为:p=&s[0];orp=s;10、第4,5行合为:doublex,y,*p;11、第4行改为:intx,*p=&x;否则p为“野指针”或者在给p赋值之前定义一个int变量y,然后p=&y;12、第4,5行交换书写次序。6习题七一、选择题1、A2、D3、B4、D5、C6、A7、B二、程序填空题1、a[k]a[k]a[k]2、a[i]j6ori+1三、改错题1、第3行最后加分号“;”第4行改为:voidmain()第7行去掉三个&运算符第8,9行改为:if(strcmp(a,b)0)swap(a,b);if(strcmp(b,c)0)swap(b,c);if(strcmp(a,b)0)swap(a,b);swap函数改为:voidswap(char*pstr1,char*pstr2){charp[80];//千万不能用char*p;没有赋初值即为“野指针”,危险!strcpy(p,pstr1);strcpy(pstr1,pstr2);strcpy(pstr2,p);}2、第1行后面加入:floatprocess(float*,int,int*);voidmain()intm;定义后加入:floatmax;process定义改为:floatprocess(…){…floattemp=p1[0];或改为:=*p1*p2=p1[0];//插入for(x=1;xn;x++)…}7四、编程题//P.230第七章习题//编程题4参考程序答案////////#includestdio.hvoiddelete_char(char*s,charc);voidmain(){charstr[80],ch;puts(请输入一个不足79个字符的串:);scanf(%s,str);getchar();puts(请输入待删除的字符:);scanf(%c,&ch);delete_char(str,ch);printf(删除特定字符%c后的字符串为%s\n,ch,str);}voiddelete_char(char*s,charc){char*p;p=s;for(;*s;s++)if(*s!=c)*p++=*s;*p='\0';}//P.230第七章习题//编程题8参考程序答案////8/////*#includestdio.h#includestdlib.h#includestring.h//#includealloc.hvoidreverse(char*s);intlen;//全局变量使得reverse函数通用性降低voidmain(){char*str;str=(char*)malloc(80*sizeof(char));if(!str)//if(str==NULL)puts(内存空间不够!);puts(请输入待翻转的字符串:);scanf(%s,str);//gets(str);len=strlen(str);reverse(str);printf(翻转后的字符串为:%s\n,str);free(str);//很重要,必须与前面的malloc配对,编程时,最好习惯性地在写//完malloc后马上先把free写上,免得遗忘}voidreverse(char*s){chartemp;if(len==2||len==3){temp=*s;*s=*(s+len-1);*(s+len-1)=temp;}else{temp=*s;9*s=*(s+len-1);*(s+len-1)=temp;len-=2;reverse(s+1);}}*///----------------------------------------------------------------//下面是通用性强的reverse函数#includestdio.h#includestdlib.h#includestring.h//#includealloc.hvoidreverse(char*s,intlen);voidmain(){char*str;intlength;str=(char*)malloc(80*sizeof(char));if(!str)//if(str==NULL)puts(内存空间不够!);puts(请输入待翻转的字符串:);scanf(%s,str);length=strlen(str);reverse(str,length);printf(翻转后的字符串为:%s\n,str);free(str);}voidreverse(char*s,intlen){chartemp;if(len==2||len==3){temp=*s;*s=*(s+len-1);*(s+len-1)=temp;10}else{temp=*s;*s=*(s+len-1);*(s+len-1)=temp;len-=2;reverse(s+1,len);}}//P.230第七章习题//编程题15参考程序答案//////本例可以采用结构体数组//#includestdio.h#includestring.hvoidinput(int[],char[][20],int[],int[][4]);voidcalcu(int[][4],float[],float*);voidsort(int[],char[][20],int[],int[][4],float[]);voidoutput(int[],char[][20],int[],int[][4],float[],float);voidmain(){intscore[30][4];floatavg[30],total_avg;charname[30][20];intid[30],sex[30];input(id,name,sex,score);calcu(score,avg,&total_avg);sort(id,name,sex,score,avg);output(id,name,sex,score,avg,total_avg);}voidsort(intstu_id[],charstu_name[][20],intstu_sex[],intstu_score[][4],floatstu_avg[]){11inti,j,k,m,tmp;chartemp[20];floataverage;for(i=0;i29;i++){m=i;for(j=i+1;j4;j++)if(stu_avg[j]stu_avg[m])m=j;if(m!=i){tmp=stu_id[m];stu_id[m]=stu_id[i];stu_id[i]=tmp;strcpy(temp,stu_name[m]);strcpy(stu_name[m],stu_name[i]);strcpy(stu_name[i],temp);tmp=stu_sex[m];stu_sex[m]=stu_sex[i];stu_sex[i]=tmp;for(k=0;k4;k++){tmp=stu_score[m][k];stu_score[m][k]=stu_score[i][k];stu_score[i][k]=tmp;}average=stu_avg[m];stu_avg[m]=stu_avg[i];stu_avg[i]=average;}}}voidcalcu(intstu_score[][4],floatstu_avg[],float*stu_total_avg){inti,j;*stu_total_avg=0;for(i=0;i=29;i++)12{stu_avg[i]=0;for(j=0;j4;j++){stu_avg[i]+=stu_score[i][j];*stu_total_avg+=stu_score[i][j];}stu_avg[i]/=4;}*stu_total_avg/=30*4;}voidinput(intstu_id[],charstu_name[][20],intstu_sex[],intstu_score[][4]){puts(请按学号依次输入学生的学号、姓名、性别(1:男生,0:女生));puts(和数学、物理、英语、计算机的课程成绩:);printf(%6s%6s%6s%6s%6s%6s%6s\n,学号,姓名,性别,数学,物理,英语,计算机);inti,j;for(i=0;i=29;i++){scanf(%d%s%d,&stu_id[i],stu_name[i],&stu_sex[i]);for(j=0;j4;j++){scanf(%6d,&stu_score[i][j]);}}puts(----------------------------------------------