• 146.02 KB
  • 2022-04-22 11:17:52 发布

全国计算机等级考试二级教程 C语言程序设计课后习题答案.pdf

  • 37页
  • 当前文档由用户上传发布,收益归属用户
  1. 1、本文档共5页,可阅读全部内容。
  2. 2、本文档内容版权归属内容提供方,所产生的收益全部归内容提供方所有。如果您对本文有版权争议,可选择认领,认领后既往收益都归您。
  3. 3、本文档由用户上传,本站不保证质量和数量令人满意,可能有诸多瑕疵,付费之前,请仔细先通过免费阅读内容等途径辨别内容交易风险。如存在严重挂羊头卖狗肉之情形,可联系本站下载客服投诉处理。
  4. 文档侵权举报电话:19940600175。
'《全国计算机等级考试二级教程--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(a*b)/ca*b/ca/c*b2.23把常量10赋给变量s2.24位1或02.2581270111111-128100000002.2632767-3276810000000000000002.2710816三.上机改错题2.28#include"stdio.h";删除行尾的";"main();/*mainfunction*/删除")"后的";",注释中的*要紧靠“/”,即应为“/*”和“*/”函数开始处遗失了一个“{”floatr,s;/*/*risradius*/,/*sisareaofcircuilar*/*/注释符号不可嵌套使用r=5.0;s=3.14159*r*r;printf("%fn",s)行尾遗失了“;”函数结束处遗失了一个“}”2.29#include"stdio.h"main/*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=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,&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: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!&&||4.15!4.16a==b||a4||x<-44.1714.18x<=01>04.193224.20*#三.编程题4.21略4.22 #include/*检查日期的合法性*/intcheckdate(intyear,intmonth,intday){if(year<1900||year>2005){printf("输入的年份无效!n");return0;}elseif(month<0&&month>12){printf("输入的月份无效!n");return0;}elseif(day<=0&&day>31){printf("输入的日期无效!n");return0;}else{switch(month){case4:case6:case9:case11:if(day>30){printf("输入的日期无效!n");return0;}break;case2:if((year%4==0&&year%100!=0)||year%400==0){if(day>29){printf("输入的日期无效!n");return0;}}else {if(day>28){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(checkdate(y0,m0,d0)){printf("请输入当前日期:");scanf("%d%d%d",&y1,&m1,&d1);/*当前日期合法性检查*/if(!checkdate(y1,m1,d1)){return;}elseif(y0>y1){printf("出生年份比当前年份晚!n");return;}elseif(y0==y1){if(m0>m1){printf("出生年月比当前年月晚!n");return;}elseif(m0==m1) {if(d0>d1){printf("出生年月日比当前年月日晚!n");return;}}}}/*计算实足年龄*/years=y1-y0;months=m1-m0;days=d1-d0;/*修正实足年龄天数*/if(days<0){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;}else{days+=28; }break;}/*endofswitch(m1)*/}/*endofif(days<0)*//*修正实足年龄月数*/if(months<0){months+=12;years--;}/*endofif(months<0)*/printf("出生日期:%d年%d月%d日n",y0,m0,d0);printf("当前日期:%d年%d月%d日n",y1,m1,d1);printf("实足年龄:%d年%d月%d日n",years,months,days);return;}4.23#includevoidmain(){inta;printf("请输入一个整数:");scanf("%d",&a);if(a%2==0){printf("%d是偶数n",a);}else{printf("%d是奇数n",a);}}4.24#includevoidmain(){inta,b,c,temp,max; printf("请输入三个整数:");scanf("%d%d%d",&a,&b,&c);temp=(a>b)?a:b;max=(temp>c)?temp:c;printf("n");printf("你输入的数中最大的是%d.n",max);}4.25(1)不嵌套的if语句#includevoidmain(){intx,y;printf("inputx:");scanf("%d",&x);if(x>-5&&x<0){printf("yis%dn",y=x);}if(x==0){printf("yis%dn",y=x-1);}if(x>0&&x<10){printf("yis%dn",y=x+1);}if(x>=10||x<=-5){printf("errorn");}}(2)嵌套的if语句#includevoidmain(){intx,y;printf("inputx:");scanf("%d",&x);printf("n");if(x<0) {if(x>-5){printf("yis%d.n",y=x);}else{printf("error!n");}}if(0==x){printf("yis%d.n",y=x-1);}if(x>0){if(x<10){printf("yis%d.n",y=x+1);}else{printf("error!n");}}}(3)if_else语句#includevoidmain(){intx,y;printf("inputx:");scanf("%d",&x);if(x>-5&&x<0){printf("yis%d.n",y=x);}elseif(x==0){printf("yis%d.n",y=x-1);}elseif(x>0&&x<10){ printf("yis%d.n",y=x+1);}else{printf("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;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<=n5.16x>=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<50;i++){m*=i;e+=1/m;}printf("e=%fn",e);}(2) #includevoidmain(){inti=1;doublem=1.0;doublee=1.0;while(1/m>=0.0004){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;i{m=i-k;if(m<0){m*=-1;}for(j=0;j{printf("");}for(j=0;j<2*(k-m)+1;j++){printf("*");}printf("n");}}第六章一.选择题6.1B6.2D6.3A6.4A6.5B6.6D6.7D6.8B6.9A6.10A6.11C二.填空题6.12-16.1316.14ctype.h6.1516.1610A20B30C40D6.177.29101.298AB6.18A7.29B101.2986.19ABC(每个字符后有三个空格)三.编程题6.20#include#defineN80voidmain(){ charstr[N];intiLoop=0;gets(str);while(str[iLoop]){printf("%c-%dt",str[iLoop],str[iLoop]);iLoop++;if(iLoop%3==0){printf("n");}}printf("n");}6.21#include#defineN80voidmain(){charstr[N];intnum=0;intiLoop=0;gets(str);while(str[iLoop]){if(str[iLoop]>="0"&&str[iLoop]<="9"){num=10*num+(str[iLoop]-"0");}iLoop++;}printf("%dn",num);}6.22#include#include#defineN80voidmain(){charstr[N]; intnum=-1;do{gets(str);num++;}while(strcmp(str,"EOF"));printf("您输入了%d行字符!n",num);}6.23#include#defineN80voidmain(){charstr[N];intiLoop=0;intnum=0;gets(str);while(str[iLoop]&&iLoop="a"&&str[iLoop]<="z"){num++;}iLoop++;}printf("您输入了字符中有%d个小写字母!n",num);}6.24#includevoidmain(){intline;intiLoop1;intiLoop2;printf("请输入图案的行数(不大于26):");scanf("%d",&line);for(iLoop1=0;iLoop1y){*min=y;}if(*min>z){*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(){intcounter[10]={0};intiLoop=0;charstr[N];gets(str);while(str[iLoop]){if(str[iLoop]>="0"&&str[iLoop]<="9"){counter[str[iLoop]-"0"]++;}iLoop++;}for(iLoop=0;iLoop<10;iLoop++){printf("%d-%dn",iLoop,counter[iLoop]);}return0;}9.28voidfun(intarray[],intarraysize,intstart){intiLoop;if(startinertNumber){for(iLoop2=arraysize-1;iLoop2>=iLoop;iLoop2--){array[iLoop2+1]=array[iLoop2];}array[iLoop]=inertNumber;break;}}if(iLoop>=arraysize){array[arraysize]=inertNumber;}}else{for(iLoop=0;iLoop=iLoop;iLoop2--){array[iLoop2+1]=array[iLoop2];}array[iLoop]=inertNumber;break;}}if(iLoop>=arraysize){array[arraysize]=inertNumber;} }}intmain(){intiLoop;inta[20]={7,6,5,3,2,1};for(iLoop=0;iLoop<6;iLoop++){printf("%d",a[iLoop]);}printf("n");fun(a,6,0);for(iLoop=0;iLoop<7;iLoop++){printf("%d",a[iLoop]);}printf("n");fun(a,7,4);for(iLoop=0;iLoop<8;iLoop++){printf("%d",a[iLoop]);}printf("n");fun(a,8,8);for(iLoop=0;iLoop<9;iLoop++){printf("%d",a[iLoop]);}printf("n");return0;}9.32intfun(intnumber,intarray[]){intiLoop=0;intiLoop2;intbinLen;intmidNumber;intdiv;intremain;midNumber=number;do {div=midNumber/2;remain=midNumber%2;midNumber=div;array[iLoop++]=remain;}while(midNumber);binLen=iLoop;for(iLoop2=0,iLoop=binLen-1;iLoop20){x=rand()%20;iLoop2=0;}iLoop2++;}while(iLoop2len){returnNULL;}ch=str[pos];for(iLoop=pos;iLoopdig){pos=dig;}outStr[pos]="";}printf("处理后的字串为:");printf("%sn",outStr);return0;}11.12#include#include voidmovebin(char*bin){intlen;intiLoop;len=strlen(bin);for(iLoop=len;iLoop>0;iLoop--){bin[iLoop]=bin[iLoop-1];}return;}voidfun(intn,char*bin){intpos;pos=strlen(bin);if(n==0){return;}if(n==1){movebin(bin);bin[0]="1";return;}movebin(bin);bin[0]=(n%2)+"0";n/=2;fun(n,bin);return;}intmain(){inta=4;charbin[50]={""};fun(a,bin);printf("%sn",bin);return0;}11.13 #includelongfun(intn){if(n==1){returnn;}else{returnfun(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;}else{returnfun(n-1)+fun(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>="A"&&C<="Z")||(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;iLoop<2;iLoop++){for(iLoop2=0;iLoop2<2-iLoop;iLoop2++){if(*(p+iLoop2)>*(p+iLoop2+1)){tmp=*(p+iLoop2);*(p+iLoop2)=*(p+iLoop2+1);*(p+iLoop2+1)=tmp;}}}printf("%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->nextm>p->data14.11(structlist*)structliststructlist*structlistreturnh三.编程题14.12#include#defineN3structstud{charnum[5],name[10];ints[4];doubleave;};voidreadrec(structstudarray[],intsize) {intiLoop;for(iLoop=0;iLoop{scanf("%s%s%d%d%d%d",array[iLoop].num,array[iLoop].name,&array[iLoop].s[0],&array[iLoop].s[1],&array[iLoop].s[2],&array[iLoop].s[3]);array[iLoop].ave=(array[iLoop].s[0]+array[iLoop].s[1]+array[iLoop].s[2]+array[iLoop].s[3])/4.0;}return;}voidwriterec(structstudarray[],intsize){intiLoop;for(iLoop=0;iLoop{printf("%st%st%dt%dt%dt%dt%fn",array[iLoop].num,array[iLoop].name,array[iLoop].s[0],array[iLoop].s[1],array[iLoop].s[2],array[iLoop].s[3],array[iLoop].ave);}return;}intmain(){structstudstu[N];readrec(stu,N);writerec(stu,N);return0;}14.13#include#include#defineN100structnode{intdata;structnode*next; };intseekMaxValue(structnode*pNode){intmax;structnode*pMove;pMove=pNode;max=pMove->data;pMove=pMove->next;while(pMove){if(maxdata){max=pMove->data;}pMove=pMove->next;}returnmax;}structnode*seekMaxValueAddress(structnode*pNode){intmax;structnode*maxAddress;structnode*pMove;pMove=pNode;max=pMove->data;maxAddress=pMove;pMove=pMove->next;while(pMove){if(maxdata){max=pMove->data;maxAddress=pMove;}pMove=pMove->next;}returnmaxAddress;}intmain() {structnode*head;structnode*pNode;intiLoop;head=(structnode*)malloc(sizeof(structnode));pNode=head;pNode->next=NULL;for(iLoop=0;iLoop{pNode->next=(structnode*)malloc(sizeof(structnode));pNode=pNode->next;pNode->next=NULL;pNode->data=iLoop;}printf("%dn",seekMaxValue(head->next));printf("%dn",seekMaxValueAddress(head->next));return0;}第十五章一.选择题15.1D15.2A15.3B15.4A二.填空题15.51111000015.6a^a15.7a|0xffff15.8x|0xff0015.9a=(012500>>2)15.10ch|0x20第十六章一.选择题16.1B16.2C二.填空题16.33!feof(f1)f2fclose(f1)fclose(f2)16.4fopen(fname,"w")ch16.5"r"!feof(fp)fgetc(fp)16.6CCCCBBBBAAAA 三.编程题16.7#include#defineN10#defineLEN81intmain(){char*str[N]={"AAAAAAAAA","BBBBBBBBB","CCCCCCCCC","DDDDDDDDD","EEEEEEEEE","FFFFFFFFF","GGGGGGGGG","HHHHHHHHH","IIIIIIIII","JJJJJJJJJ"};charstr2[N][LEN];FILE*fp;intiLoop;fp=fopen("str.txt","w");if(fp==NULL){printf("创建文件失败!n");return1;}else{for(iLoop=0;iLoop

您可能关注的文档