《全国计算机等级考试二级教程--C语言程序设计课后习题答案复习课程.doc》由会员分享,可在线阅读,更多相关《全国计算机等级考试二级教程--C语言程序设计课后习题答案复习课程.doc(62页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、Good is good, but better carries it.精益求精,善益求善。全国计算机等级考试二级教程-C语言程序设计课后习题答案-全国计算机等级考试二级教程-C语言程序设计课后习题答案第一章1.1EXE1.2COBJEXE1.3顺序选择循环第二章一.选择题2.1B2.2D2.3B2.4A2.5C2.6A2.7B2.8B2.9D2.10C2.11B2.12B2.13A二.填空题2.1411122.154.24.22.16定义执行语句2.17关键字用户标识符2.18intfloatdouble2.19floata1=1;floata2=1;2.20存储单元2.213.52.22(
2、a*b)/ca*b/ca/c*b2.23把常量10赋给变量s2.24位1或02.2581270111111-128100000002.2632767-3276810000000000000002.2710816三.上机改错题2.28#includestdio.h;删除行尾的;main();/*mainfunction*/删除)后的;,注释中的*要紧靠“/”,即应为“/*”和“*/”函数开始处遗失了一个“”floatr,s;/*/*risradius*/,/*sisareaofcircuilar*/*/注释符号不可嵌套使用r=5.0;s=3.14159*r*r;printf(%fn,s)行尾遗失
3、了“;”函数结束处遗失了一个“”2.29#includestdio.hmain/*mainfunction*/main后遗失了“()”floata,b,c,v;/*a,b,caresides,visvolumeofcube*/a=2.0;b=3.0;c=4.0行尾遗失了“;”v=a*b*c;printf(%fn,v)行尾遗失了“;”第三章一.选择题3.1C3.2C3.3D3.4C3.5D3.6B3.7C3.8D3.9A3.10B3.11C3.12D3.13D3.14A3.15C3.16C3.17C3.18无答案3.19C3.20B二.填空题3.21(1)-2002500(2)i=-200,j=
4、2500(3)i=-200j=25003.2212003.23一条语句;3.24;3.25100,25.81,1.8923410025.811.8923410025.811.892343.26x=127,x=127,x=177,x=7f,x=1273.27x=127,x=127,x=$127,x=$000127,x=%06d3.28a=513.789215,a=513.79,a=513.78921500,a=513.78921500三.编程题和改错题3.29修改后的程序如下:main()doublea,b,c,s,v;printf(inputa,b,c:);scanf(%lf%lf%lf,&a
5、,&b,&c);s=a*b;v=a*b*c;printf(a=%f,b=%f,c=%fn,a,b,c);printf(s=%f,v=%fn,s,v);3.30#includemain()inta=560,b=60;printf(560minuteis%dhourand%dminute.n,a/b,a%b);3.31#includemain()inta,b;a=1500;b=350;printf(adivbis:%dn,a/b);printf(amodbis:%dn,a%b);3.32#includemain()doublea,b,c,ave;printf(input3doublenumber:
6、n);scanf(%lf%lf%lf,&a,&b,&c);printf(%.1fn,(a+b+c)/3);3.33#includevoidmain()inta,b,c,t;printf(请依次输入整数a,b,c:);scanf(%d%d%d,&a,&b,&c);printf(n你输入的值是:a=%d,b=%d,c=%dn,a,b,c);t=b;b=a;a=c;c=t;printf(交换之后的值是:a=%d,b=%d,c=%dn,a,b,c);第四章一.选择题4.1A4.2A4.3A4.4D4.5C4.6A4.7B4.8C4.9D4.10C二.填空题4.11非004.12=同级=!=同级4.13
7、!&|4.15!4.16a=b|a4|x-44.1714.18x04.193224.20*#三.编程题4.21略4.22#include/*检查日期的合法性*/intcheckdate(intyear,intmonth,intday)if(year2005)printf(输入的年份无效!n);return0;elseif(month12)printf(输入的月份无效!n);return0;elseif(day31)printf(输入的日期无效!n);return0;elseswitch(month)case4:case6:case9:case11:if(day30)printf(输入的日期无效
8、!n);return0;break;case2:if(year%4=0&year%100!=0)|year%400=0)if(day29)printf(输入的日期无效!n);return0;elseif(day28)printf(输入的出生日期无效!n);return0;break;/*endofswitch(m0)*/return1;voidmain()inty0,m0,d0;/*生日*/inty1,m1,d1;/*当前日期*/intyears,months,days;/*实足年龄*/printf(请输入学生的生日:);scanf(%d%d%d,&y0,&m0,&d0);if(checkda
9、te(y0,m0,d0)printf(请输入当前日期:);scanf(%d%d%d,&y1,&m1,&d1);/*当前日期合法性检查*/if(!checkdate(y1,m1,d1)return;elseif(y0y1)printf(出生年份比当前年份晚!n);return;elseif(y0=y1)if(m0m1)printf(出生年月比当前年月晚!n);return;elseif(m0=m1)if(d0d1)printf(出生年月日比当前年月日晚!n);return;/*计算实足年龄*/years=y1-y0;months=m1-m0;days=d1-d0;/*修正实足年龄天数*/if(d
10、ays0)months-;switch(m1)case1:case5:case7:case10:case12:days+=30;break;case2:case4:case6:case8:case9:case11:days+=31;break;case3:if(y1%4=0&y1%100!=0)|y1%400=0)days+=29;elsedays+=28;break;/*endofswitch(m1)*/*endofif(days0)*/*修正实足年龄月数*/if(months0)months+=12;years-;/*endofif(monthsb)?a:b;max=(tempc)?tem
11、p:c;printf(n);printf(你输入的数中最大的是%d.n,max);4.25(1)不嵌套的if语句#includevoidmain()intx,y;printf(inputx:);scanf(%d,&x);if(x-5&x0&x=10|x=-5)printf(errorn);(2)嵌套的if语句#includevoidmain()intx,y;printf(inputx:);scanf(%d,&x);printf(n);if(x-5)printf(yis%d.n,y=x);elseprintf(error!n);if(0=x)printf(yis%d.n,y=x-1);if(x0
12、)if(x-5&x0&x10)printf(yis%d.n,y=x+1);elseprintf(error!n);(4)switch语句#includevoidmain()intx,y;printf(inputx:);scanf(%d,&x);switch(x)case-4:case-3:case-2:case-1:printf(yis%d.n,y=x);break;case0:printf(yis%d.n,y=x-1);break;case1:case2:case3:case4:case5:case6:case7:case8:case9:printf(yis%d.n,y=x+1);break
13、;default:printf(error!n);第五章一.选择题5.1D5.2C5.3B5.4C5.5C5.6B5.7D5.8A5.9D5.10D二.填空题5.115465.12死循环5.13-15.14115.15d=1.0k+k=0x三.编程题5.17#includevoidmain()inti;intsig=1;intsum=0;for(i=1;i=101;i+,i+)sum+=sig*i;sig*=-1;printf(sum=%dn,sum);5.18(1)#includevoidmain()inti;doublem=1.0;doublee=1.0;for(i=1;i=0.0004)
14、m*=i;e+=1/m;i+;printf(e=%fn,e);5.19#includevoidmain()intyear;intcol=0;for(year=1600;year=2000;year+)if(year%4=0&year%100!=0)|year%400=0)printf(%dt,year);col+;if(col%5=0)printf(n);printf(n);5.20#include#defineN7voidmain()inti;intj;intm;intk=N/2;for(i=0;im=i-k;if(m0)m*=-1;for(j=0;jprintf();for(j=0;j=0
15、&striLoop=9)num=10*num+(striLoop-0);iLoop+;printf(%dn,num);6.22#include#include#defineN80voidmain()charstrN;intnum=-1;dogets(str);num+;while(strcmp(str,EOF);printf(您输入了%d行字符!n,num);6.23#include#defineN80voidmain()charstrN;intiLoop=0;intnum=0;gets(str);while(striLoop&iLoop=a&striLoop=z)num+;iLoop+;pr
16、intf(您输入了字符中有%d个小写字母!n,num);6.24#includevoidmain()intline;intiLoop1;intiLoop2;printf(请输入图案的行数(不大于26):);scanf(%d,&line);for(iLoop1=0;iLoop1line;iLoop1+)for(iLoop2=0;iLoop2line-iLoop1;iLoop2+)printf();for(iLoop2=0;iLoop22*iLoop1+1;iLoop2+)printf(%c,iLoop1+A);printf(n);第七章一.选择题7.1C7.2C7.3B7.4C7.5A7.6D7
17、.7A二.填空题7.8127.99.0000007.1047.11n=1s7.12=yz*x7.131s*i0f(k)三.程序调试和编程题7.14fun(intn)intk,yes;for(k=2;k=n/2;k+)if(n%k=0)yes=0;break;elseyes=1;returnyes;7.15intmymod(inta,intb)returna%b;7.16doublefun(intn)doublesum=0;intiLoop;intsig=-1;for(iLoop=1;iLoop=n;iLoop+)sig*=-1;sum+=sig*1.0/iLoop;returnsum;7.17
18、doublefun(intn)doublet=1.0;intiLoop;longtmp;for(iLoop=2;iLoop=n;iLoop+)tmp=iLoop*iLoop;t-=1.0/tmp;returnt;7.18#include#includedoublefun(doublex)returnx*x+5*x+4;voidmain()intx=2;printf(y1=%fn,fun(x);printf(y2=%fn,fun(x+15);printf(y3=%fn,fun(sin(x);第八章一.选择题8.1A8.2B8.3B8.4C8.5B8.6B8.7C8.8D8.9B8.10C8.11
19、C8.12C二.填空题8.131108.14718.15(1)char*p=&ch;(2)p=&ch;(3)scanf(%c,p);(4)*p=A;(5)printf(%c,*p);8.16(1)s=p+3;(2)s=s-2(3)50(4)*(s+1)(5)2(6)1020304050三.编程题8.17voidfun(doublex,doubley,double*sum,double*div)*sum=x+y;*div=x-y;return;8.18voidfun(doublex,doubley,doublez,double*max,double*min)*max=x;*min=x;if(*m
20、axy)*max=y;if(*maxy)*min=y;if(*minz)*min=z;return;第九章一.选择题9.1D9.2A9.3A9.4C9.5C9.6A9.7B9.8D9.9C9.10C9.11C9.12D9.13D9.14A9.15A9.16A9.17C9.18C二.填空题9.19909.2069.21129.2239.2327219.24-850,2,09.25k=pk9.26(c=getchar()c-A三.编程题9.27#include#defineN81intmain()intcounter10=0;intiLoop=0;charstrN;gets(str);while(
21、striLoop)if(striLoop=0&striLoop=9)counterstriLoop-0+;iLoop+;for(iLoop=0;iLoop10;iLoop+)printf(%d-%dn,iLoop,counteriLoop);return0;9.28voidfun(intarray,intarraysize,intstart)intiLoop;if(startarraysize-1)if(start=0)start=1;for(iLoop=start;iLooparraysize;iLoop+)arrayiLoop-1=arrayiLoop;for(iLoop=0;iLoopa
22、rraysize;iLoop+)printf(No.%d=%dn,iLoop,arrayiLoop);9.29intfun(intarry1,intarry2,intarrysize)intiLoop;intcounter=0;for(iLoop=0;iLooparrysize;iLoop+)if(arry1iLoop%2)arry2counter+=arry1iLoop;returncounter;9.30voidfun(chararray,intarraysize)intiLoop1;intiLoop2;chartemp;/*冒泡排序*/for(iLoop1=0;iLoop1arraysi
23、ze-1;iLoop1+)for(iLoop2=0;iLoop2arraysize-1-iLoop1;iLoop2+)if(arrayiLoop2arrayiLoop2+1)temp=arrayiLoop2;arrayiLoop2=arrayiLoop2+1;arrayiLoop2+1=temp;9.31#includevoidfun(intarray,intarraysize,intinertNumber)intiLoop;intiLoop2;if(array0arrayarraysize-1)for(iLoop=0;iLoopinertNumber)for(iLoop2=arraysize
24、-1;iLoop2=iLoop;iLoop2-)arrayiLoop2+1=arrayiLoop2;arrayiLoop=inertNumber;break;if(iLoop=arraysize)arrayarraysize=inertNumber;elsefor(iLoop=0;iLooparraysize;iLoop+)if(arrayiLoop=iLoop;iLoop2-)arrayiLoop2+1=arrayiLoop2;arrayiLoop=inertNumber;break;if(iLoop=arraysize)arrayarraysize=inertNumber;intmain(
25、)intiLoop;inta20=7,6,5,3,2,1;for(iLoop=0;iLoop6;iLoop+)printf(%d,aiLoop);printf(n);fun(a,6,0);for(iLoop=0;iLoop7;iLoop+)printf(%d,aiLoop);printf(n);fun(a,7,4);for(iLoop=0;iLoop8;iLoop+)printf(%d,aiLoop);printf(n);fun(a,8,8);for(iLoop=0;iLoop9;iLoop+)printf(%d,aiLoop);printf(n);return0;9.32intfun(int
26、number,intarray)intiLoop=0;intiLoop2;intbinLen;intmidNumber;intdiv;intremain;midNumber=number;dodiv=midNumber/2;remain=midNumber%2;midNumber=div;arrayiLoop+=remain;while(midNumber);binLen=iLoop;for(iLoop2=0,iLoop=binLen-1;iLoop2iLoop;iLoop2+,iLoop-)midNumber=arrayiLoop2;arrayiLoop2=arrayiLoop;arrayi
27、Loop=midNumber;returnbinLen;9.33#include#include#defineN15voidfun(intarray,intarraysize)intx;intiLoop;intiLoop2;for(iLoop=0;iLoop0)x=rand()%20;iLoop2=0;iLoop2+;while(iLoop2iLoop);arrayiLoop=x;intmain()intaN;intiLoop;fun(a,N);for(iLoop=0;iLoopN;iLoop+)printf(%dn,aiLoop);return0;第十章一.选择题10.1C10.2B10.3
28、C10.4B10.5C10.6A10.7C10.8A10.9C10.10C二.填空题10.11GFEDCB10.12XYZ10.13SO10.141010.15Itis10.16strlen(str)-1j-10.17310.18goodgood!三.编程题10.19char*mygets(char*str)intiLoop=0;charch;while(ch=getchar()!=n)striLoop+=ch;striLoop=0;returnstr;char*myputs(char*str)intiLoop=0;while(striLoop)putchar(striLoop+);putch
29、ar(n);returnstr;10.20#include#includeintfun(char*str)intlen;intiLoop1;intiLoop2;intresult=1;len=strlen(str);for(iLoop1=0,iLoop2=len-1;iLoop1len)returnNULL;ch=strpos;for(iLoop=pos;iLooplen-1;iLoop+)striLoop=striLoop+1;strlen-1=0;returnch;第十一章一.选择题11.1D11.2B11.3A11.4C二.填空题11.5IJKLEFGHABCD11.6711.7811.
30、8*(s+j)i+1i11.91711.10(*fun)()(*fun)(a+i*h)/hmypoly三.编程题11.11#include#include#defineN81intmain(intargc,char*argv)charsig;intdig;intpos;charstrN=0;charoutStrN=0;if(argc2)sig=-;dig=10;elsesig=argv10;dig=argv11-0;printf(请输入一个字符串:);gets(str);if(sig=-)pos=strlen(str)-dig;if(posdig)pos=dig;outStrpos=0;pri
31、ntf(处理后的字串为:);printf(%sn,outStr);return0;11.12#include#includevoidmovebin(char*bin)intlen;intiLoop;len=strlen(bin);for(iLoop=len;iLoop0;iLoop-)biniLoop=biniLoop-1;return;voidfun(intn,char*bin)intpos;pos=strlen(bin);if(n=0)return;if(n=1)movebin(bin);bin0=1;return;movebin(bin);bin0=(n%2)+0;n/=2;fun(n,
32、bin);return;intmain()inta=4;charbin50=;fun(a,bin);printf(%sn,bin);return0;11.13#includelongfun(intn)if(n=1)returnn;elsereturnfun(n-1)+n;intmain()intnum;intsum;printf(请输入一个自然数:);scanf(%d,&num);sum=fun(num);printf(结果是:%dn,sum);return0;11.14#includeintfun(intn)if(n=0|n=1)return1;elsereturnfun(n-1)+fun(
33、n-2);intmain()intnum;intresult;printf(请输入一个自然数:);scanf(%d,&num);result=fun(num);printf(斐波拉契级数为:%dn,result);return0;第十二章一.选择题12.1B12.2B12.3A12.4C12.5D12.6B12.7A12.8A二.填空题12.92,5,1,2,3,-212.102468第十三章一.选择题13.1A13.2C13.3B13.4C13.5D13.6D13.7D二.填空题13.8ar=9ar=9ar=1113.9int*s*b三.编程题13.10#defineMYALPHA(C)(C
34、=A&C=a&C=z)?1:013.11#defineSWAP(t,x,y)ttmp;tmp=x;x=y;y=tmp;13.12#include#includeintmain()int*p;inttmp;intiLoop;intiLoop2;p=(int*)malloc(sizeof(int)*3);scanf(%d%d%d,p,p+1,p+2);for(iLoop=0;iLoop2;iLoop+)for(iLoop2=0;iLoop2*(p+iLoop2+1)tmp=*(p+iLoop2);*(p+iLoop2)=*(p+iLoop2+1);*(p+iLoop2+1)=tmp;printf(
35、%d%d%dn,*p,*(p+1),*(p+2);free(p);p=NULL;return0;第十四章一.选择题14.1D14.2D14.3D14.4A14.5C14.6C14.7C14.8B二.填空题14.9structlink*next14.10p-nextmp-data14.11(structlist*)structliststructlist*structlistreturnh三.编程题14.12#include#defineN3structstudcharnum5,name10;ints4;doubleave;voidreadrec(structstudarray,intsize)
36、intiLoop;for(iLoop=0;iLoopscanf(%s%s%d%d%d%d,arrayiLoop.num,arrayiLoop.name,&arrayiLoop.s0,&arrayiLoop.s1,&arrayiLoop.s2,&arrayiLoop.s3);arrayiLoop.ave=(arrayiLoop.s0+arrayiLoop.s1+arrayiLoop.s2+arrayiLoop.s3)/4.0;return;voidwriterec(structstudarray,intsize)intiLoop;for(iLoop=0;iLoopprintf(%st%st%dt%dt%dt%dt%fn,arrayiLoop.num,arrayiLoop.name,arrayiLoop.s0,arrayiLoop.s1,a