《全国计算机等级考试二级教程——C语言程序设计》课后题及参考答案
【免费下载】全国计算机等级考试二级教程 C语言程序设计课后习题答案

二. 填空题 3.21 (1)-2002500(2)i=-200,j=2500 (3)i=-200 j=2500 3.22 12 0 0 3.23 一条语句 ; 3.24 ; 3.25 100,25.81,1.89234 100 25.81 1.89234 100 25.81 1.89234 3.26 x=127,x= 127,x= 177,x= 7f,x= 127 3.27 x=127,x=127 ,x=$127 ,x=$000127,x=%06d 3.28 a=513.789215,a= 513.79,a= 513.78921500,a= 513.78921500
三. 编程题 4.21 略 4.22
对全部高中资料试卷电气设备,在安装过程中以及安装结束后进行高中资料试卷调整试验;通电检查所有设备高中资料电试力卷保相护互装作置用调与试相技互术关,系电,力根通保据过护生管高产线中工敷资艺设料高技试中术卷资,配料不置试仅技卷可术要以是求解指,决机对吊组电顶在气层进设配行备置继进不电行规保空范护载高与中带资负料荷试下卷高问总中题体资,配料而置试且时卷可,调保需控障要试各在验类最;管大对路限设习度备题内进到来行位确调。保整在机使管组其路高在敷中正设资常过料工程试况中卷下,安与要全过加,度强并工看且作护尽下关可都于能可管地以路缩正高小常中故工资障作料高;试中对卷资于连料继接试电管卷保口破护处坏进理范行高围整中,核资或对料者定试对值卷某,弯些审扁异核度常与固高校定中对盒资图位料纸置试,.卷保编工护写况层复进防杂行腐设自跨备动接与处地装理线置,弯高尤曲中其半资要径料避标试免高卷错等调误,试高要方中求案资技,料术编试交写5、卷底重电保。要气护管设设装线备备置敷4高、调动设中电试作技资气高,术料课中并3中试、件资且包卷管中料拒含试路调试绝线验敷试卷动槽方设技作、案技术,管以术来架及避等系免多统不项启必方动要式方高,案中为;资解对料决整试高套卷中启突语动然文过停电程机气中。课高因件中此中资,管料电壁试力薄卷高、电中接气资口设料不备试严进卷等行保问调护题试装,工置合作调理并试利且技用进术管行,线过要敷关求设运电技行力术高保。中护线资装缆料置敷试做设卷到原技准则术确:指灵在导活分。。线对对盒于于处调差,试动当过保不程护同中装电高置压中高回资中路料资交试料叉卷试时技卷,术调应问试采题技用,术金作是属为指隔调发板试电进人机行员一隔,变开需压处要器理在组;事在同前发一掌生线握内槽图部内 纸故,资障强料时电、,回设需路备要须制进同造行时厂外切家部断出电习具源题高高电中中源资资,料料线试试缆卷卷敷试切设验除完报从毕告而,与采要相用进关高行技中检术资查资料和料试检,卷测并主处且要理了保。解护现装场置设。备高中资料试卷布置情况与有关高中资料试卷电气系统接线等情况,然后根据规范与规程规定,制定设备调试高中资料试卷方案。
《全国计算机等级考试二级教程——C语言程序设计》课后题及参考答案

第二章 C程序设计的初步知识 一、选择题 2.1 以下选项中正确的整型常量是 A)12. B)-20 C)1,000 D)4 5 6 答案:B 2.2 以下选项中正确的实型常量是 A)0 B)3. 1415 C)0.329×10(2)(10的2次方) D).871 答案:D 2.3 以下选项中不正确的实型常量是 A)2.607E-1 B)0.8103e 2 C)-77.77 D)456e-2 答案:B 2.4 以下选项中不合法的用户标识符是 A)abc.c B)file C)Main D)PRINTF 答案:A 2.5 以下选项中不合法的用户标识符是 A)_123 B)printf C)A$ D)Dim 答案:C 2.6 C语言中运算对象必需是整型的运算符是 A)% B)/ C)! D)** 答案:A 2.7 可在C程序中用作用户标识符的一组标识符是 A)void define WORD B)as_b3 _123 If C)For -abc case D)2c DO SIG 答案:B 2.8 若变量已正确定义并赋值,符合C语言语法的表达式是 A)a=a+7; B)a=7+b+c,a++ C)int(12.3%4) D)a=a+7=c+b 答案:B 2.9 以下叙述中正确的是 A)a是实型变量,C允许进行以下赋值a=10,因此可以这样说:实型变量中允许存放整型值。 B)在赋值表达式中,赋值号右边既可以是变量也可以是任意表达式。 C)执行表达式a=b后,在内存中a和b存储单元中的原有值都将被改变,a的值已由原值改变为b的值,b的值由原值变为0。 D)已有a=3,b=5。当执行了表达式a=b,b=a之后,已使a中的值为5,b中的值为3。 答案:B 2.10 以下叙述中正确的是 A)在C程序中无论是整数还是实数,只要在允许的范围内都能准确无误的表示。 B)C程序由主函数组成。 C)C程序由函数组成。 D)C程序由函数和过程组成。 答案:C 2.11 TURBO C中int类型变量所占字节数是
全国计算机等级考试二级教程--C语言程序设计课后习题答案

《全国计算机等级考试二级教程--C语言程序设计》课后习题答案第一章1.1 EXE1.2 C OBJ EXE1.3 顺序选择循环第二章一. 选择题2.1 B 2.2 D 2.3 B 2.4 A 2.5 C 2.6 A 2.7 B2.8 B 2.9 D 2.10 C 2.11 B 2.12 B 2.13 A二. 填空题2.14 11 122.15 4.2 4.22.16 { } 定义执行语句2.17 关键字用户标识符2.18 int float double2.19 float a1=1; float a2=1;2.20 存储单元2.213.52.22 (a*b)/c a*b/c a/c*b2.23 把常量10赋给变量s2.24 位1或0三. 上机改错题2.28#include "stdio.h"; 删除行尾的";"main(); / * main function * / 删除")"后的";",注释中的*要紧靠“/”,即应为“/*”和“*/”函数开始处遗失了一个“{”float r,s ; /*/*r is radius*/,/* s is area of circuilar*/*/ 注释符号不可嵌套使用r = 5.0 ;s = 3.14159 * r * r ;printf("%f\n",s) 行尾遗失了“;”函数结束处遗失了一个“}”2.29#include "stdio.h"main /* main function */ main后遗失了“()”{float a,b,c,v; /*a,b,c are sides, v is volume of cube */a=2.0; b=3.0; c=4.0 行尾遗失了“;”v=a*b*c;printf("%f\n", v) 行尾遗失了“;”}第三章一.选择题3.1 C 3.2 C 3.3 D 3.4 C 3.5 D 3.6 B 3.7 C 3.8 D 3.9 A 3.10 B3.11 C 3.12 D 3.13 D 3.14 A 3.15 C 3.16 C 3.17 C 3.18 无答案3.19 C 3.20 B二. 填空题3.21 (1)-2002500(2)i=-200,j=2500(3)i=-200j=25003.22 12 0 03.23 一条语句;3.24 ;3.25 100,25.81,1.89234 100 25.81 1.89234 100 25.81 1.89234 3.26 x=127,x= 127,x= 177,x= 7f,x= 1273.27 x=127,x=127 ,x=$127 ,x=$000127,x=%06d3.28 a=513.789215,a= 513.79,a= 513.78921500,a= 513.78921500三. 编程题和改错题3.29 修改后的程序如下:main(){double a,b,c,s,v;printf("input a,b,c:");scanf("%lf%lf%lf",&a,&b,&c);s =a*b;v=a*b*c;printf("a=%f,b=%f,c=%f\n", a,b,c);printf("s=%f,v=%f\n",s,v);}3.30#includemain(){int a=560,b=60;printf("560 minute is %d hour and %d minute.\n",a/b,a%b);}3.31#includemain(){int a,b;a=1500;b=350;printf("a div b is : %d\n",a/b);printf("a mod b is : %d\n",a%b);}3.32#includemain(){double a,b,c,ave;printf ("input 3 double number : \n");scanf ("%lf%lf%lf",&a,&b,&c);printf ("%.1f\n",(a+b+c)/3);}3.33#includevoid main(){int a,b,c,t;printf("请依次输入整数a,b,c:");scanf("%d%d%d",&a,&b,&c);printf("\n你输入的值是: a=%d,b=%d,c=%d\n",a,b,c);t=b;b=a;a=c;c=t;printf("交换之后的值是:a=%d,b=%d,c=%d\n",a,b,c);}第四章一. 选择题4.1 A 4.2 A 4.3 A 4.4 D 4.5 C 4.6 A 4.7 B 4.8 C 4.9 D 4.10 C二. 填空题4.11 非0 04.12 < > >= <=同级== !=同级4.13 ! && ||4.15 !4.16 a == b || a < c x > 4 || x < -44.17 14.18 x <= 0 1 > 04.19 3 2 24.20 *#三. 编程题4.21 略4.22#include/* 检查日期的合法性*/int checkdate(int year, int month, int day){if(year < 1900 || year > 2005){printf("输入的年份无效!\n");return 0;}else if(month < 0 && month > 12){printf("输入的月份无效!\n");return 0;}else if(day <= 0 && day > 31){printf("输入的日期无效!\n");return 0;}else{switch(month){case 4:case 6:case 9:case 11:if(day > 30){printf("输入的日期无效!\n");return 0;}break;case 2:if((year%4 == 0 && year%100 != 0) || year%400 == 0){if(day > 29){printf("输入的日期无效!\n");return 0;}}else{if(day > 28){printf("输入的出生日期无效!\n");return 0;}}break;}/* end of switch(m0)*/}return 1;}void main(){int y0, m0, d0; /* 生日*/int y1, m1, d1; /* 当前日期*/int years, 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;}else if(y0 > y1){printf("出生年份比当前年份晚!\n"); return;}else if(y0 == y1){if(m0 > m1){printf("出生年月比当前年月晚!\n"); return;}else if(m0 == m1){if(d0 > d1){printf("出生年月日比当前年月日晚!\n"); return;}}}}/* 计算实足年龄*/years = y1 - y0;months = m1 - m0;days = d1 - d0;/* 修正实足年龄天数*/if(days < 0){months--;switch(m1){case 1:case 5:case 7:case 10:case 12:days += 30;break;case 2:case 4:case 6:case 8:case 9:case 11:days += 31;break;case 3:if((y1%4 == 0 && y1%100 != 0) || y1%400 == 0){days += 29;}else{days += 28;}break;}/* end of switch(m1) */}/* end of if(days < 0) *//* 修正实足年龄月数*/if(months < 0){months += 12;years--;}/* end of if(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#includevoid main()int a;printf ("请输入一个整数:");scanf ("%d",&a);if (a%2==0){printf ("%d 是偶数\n", a);}else{printf ("%d 是奇数\n", a);}}4.24#includevoid main(){int a,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语句#includevoid main(){int x,y;printf("input x :");scanf("%d",&x);if ( x>-5 && x<0 ){printf("y is %d\n",y=x);}if ( x==0 ){printf("y is %d\n",y=x-1);if ( x>0 && x<10 ){printf("y is %d\n",y=x+1); }if ( x>=10 || x<=-5){printf("error\n");}}(2)嵌套的if语句#includevoid main(){int x,y;printf("input x :");scanf("%d",&x);printf("\n");if(x < 0){if(x > -5){printf("y is %d.\n",y=x); }else{printf("error!\n");}}if(0 == x){printf("y is %d.\n",y=x-1); }if(x > 0){if(x < 10){printf("y is %d.\n",y=x+1); }else{printf("error!\n");}}(3)if_else语句#includevoid main(){int x,y;printf("input x :");scanf("%d",&x);if( x>-5 && x<0 ){printf("y is %d.\n",y=x); }else if( x==0 ){printf("y is %d.\n",y=x-1); }else if( x>0 && x<10 ) {printf("y is %d.\n",y=x+1); }else{printf("error!\n");}}(4)switch语句#includevoid main(){int x,y;printf("input x : ");scanf("%d",&x);switch (x){case -4:case -3:case -2:case -1:printf("y is %d.\n",y=x); break;case 0:printf("y is %d.\n",y=x-1);break;case 1:case 2:case 3:case 4:case 5:case 6:case 7:case 8:case 9:printf("y is %d.\n",y=x+1);break;default:printf("error!\n");}}第五章一. 选择题5.1 D 5.2 C 5.3 B 5.4 C 5.5 C 5.6 B 5.7 D 5.8 A 5.9 D 5.10 D二. 填空题5.11 5 4 65.12 死循环5.13 -15.14 115.15 d=1.0 k++ k<=n5.16 x>=0 x三. 编程题5.17#includevoid main(){int i;int sig = 1;int sum = 0;for(i=1; i<=101; i++,i++){sum += sig*i;sig *= -1;}printf("sum=%d\n", sum);}5.18(1)#includevoid main(){int i;double m=1.0;double e = 1.0;for(i=1; i<50; i++){m *= i;e += 1/m;}printf("e=%f\n",e);}(2)#includevoid main(){int i=1;double m=1.0;double e = 1.0;while(1/m >= 0.0004){m *= i;e += 1/m;i++;}printf("e=%f\n",e);}5.19#includevoid main(){int year;int col = 0;for(year=1600; year<=2000; year++){if((year%4 == 0 && year%100 != 0) || year%400 == 0)printf("%d\t", year);col++;if(col%5 == 0){printf("\n");}}}printf("\n");}5.20#include#define N 7void main(){int i;int j;int m;int k = 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.1 B 6.2 D 6.3 A 6.4 A 6.5 B 6.6 D 6.7 D 6.8 B 6.9 A 6.10 A 6.11 C二. 填空题6.12 -16.13 16.14 ctype.h6.15 16.16 10A 20B 30C 40D6.177.29 101.298AB6.18 A7.29B101.2986.19 A B C (每个字符后有三个空格)三. 编程题6.20#include#define N 80void main(){char str[N];int iLoop = 0;gets(str);while(str[iLoop]){printf("%c-%d\t", str[iLoop],str[iLoop]); iLoop++;if(iLoop%3 == 0){printf("\n");}}printf("\n");}6.21#include#define N 80void main(){char str[N];int num = 0;int iLoop = 0;gets(str);while(str[iLoop]){if(str[iLoop] >= '0' && str[iLoop] <= '9') {num = 10*num + (str[iLoop] - '0');iLoop++;}printf("%d\n",num);}6.22#include#include#define N 80void main(){char str[N];int num = -1;do{gets(str);num++;}while(strcmp(str, "EOF"));printf("您输入了%d行字符!\n",num);}6.23#include#define N 80void main(){char str[N];int iLoop = 0;int num = 0;gets(str);while(str[iLoop] && iLoop < N){if(str[iLoop] >= 'a' && str[iLoop] <= 'z'){num++;}iLoop++;}printf("您输入了字符中有%d个小写字母!\n",num); }#includevoid main(){int line;int iLoop1;int iLoop2;printf("请输入图案的行数(不大于26):");scanf("%d", &line);for(iLoop1 = 0; iLoop1 < line; iLoop1++){for(iLoop2 = 0; iLoop2 < line - iLoop1; iLoop2++) {printf(" ");}for(iLoop2 = 0; iLoop2 < 2*iLoop1+1; iLoop2++) {printf("%c",iLoop1 + 'A');}printf("\n");}}第七章一. 选择题7.1 C 7.2 C 7.3 B 7.4 C 7.5 A 7.6 D 7.7 A二. 填空题7.8 127.9 9.0000007.10 47.11 n=1 s7.12 <=y z*x7.13 1 s*i 0 f(k)三. 程序调试和编程题7.14fun(int n){ int k,yes;for(k=2; k<=n/2; k++){if(n%k == 0) { yes = 0; break;}}return yes;}7.15int mymod(int a, int b){return a%b;}7.16double fun(int n){double sum = 0;int iLoop;int sig = -1;for(iLoop=1; iLoop<=n; iLoop++) {sig *= -1;sum += sig*1.0/iLoop;}return sum;}7.17double fun(int n){double t = 1.0;int iLoop;long tmp;for(iLoop=2; iLoop<=n; iLoop++) {tmp = iLoop*iLoop;t -= 1.0/tmp;}return t;}7.18#include#includedouble fun(double x){return x*x + 5*x + 4;}{int x = 2;printf("y1=%f\n", fun(x));printf("y2=%f\n", fun(x+15));printf("y3=%f\n", fun(sin(x)));}第八章一. 选择题8.1 A 8.2 B 8.3 B 8.4 C 8.5 B 8.6 B 8.7 C 8.8 D 8.9 B 8.10 C 8.11 C 8.12 C二. 填空题8.13 1108.14 7 18.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)10 20 30 40 50三. 编程题8.17void fun(double x, double y, double *sum, double *div){*sum = x + y;*div = x - y;return;}8.18void fun(double x, double y, double z, double *max, double *min){*max = x;*min = x;if(*max < y){*max = y;}if(*max < z){*max = z;}if(*min > y){*min = y;}if(*min > z){}return;}第九章一. 选择题9.1 D 9.2 A 9.3 A 9.4 C 9.5 C 9.6 A 9.7 B 9.8 D 9.9 C 9.10 C 9.11 C 9.12 D 9.13 D 9.14 A 9.15 A 9.16 A 9.17 C 9.18 C二. 填空题9.19 9 09.20 69.21 129.22 39.23 27219.24 -850,2,09.25 k=p k9.26 (c=getchar()) c-'A'三. 编程题9.27#include#define N 81int main(){int counter[10] = {0};int iLoop = 0;char str[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 - %d\n", iLoop, counter[iLoop]);}return 0;}void fun(int array[], int arraysize, int start){int iLoop;if(start < arraysize-1){if(start <=0){start = 1;}for(iLoop = start; iLoop < arraysize; iLoop++){array[iLoop-1] = array[iLoop];}}for(iLoop = 0; iLoop < arraysize; iLoop++){printf("No.%d = %d\n", iLoop, array[iLoop]);}}9.29int fun(int arry1[], int arry2[], int arrysize){int iLoop;int counter = 0;for(iLoop = 0; iLoop < arrysize; iLoop++){if(arry1[iLoop] % 2){arry2[counter++] = arry1[iLoop];}}return counter;}9.30void fun(char array[], int arraysize){int iLoop1;int iLoop2;char temp;/* 冒泡排序*/for(iLoop1 = 0; iLoop1 < arraysize - 1; iLoop1++){for(iLoop2 = 0; iLoop2 < arraysize - 1 - iLoop1; iLoop2++)if(array[iLoop2] < array[iLoop2 + 1]){temp = array[iLoop2];array[iLoop2] = array[iLoop2 + 1];array[iLoop2 + 1] = temp;}}}}9.31#includevoid fun(int array[], int arraysize, int inertNumber) {int iLoop;int iLoop2;if(array[0] < array[arraysize-1]){for(iLoop = 0; iLoop< arraysize; iLoop++){if(array[iLoop] > inertNumber){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< arraysize; iLoop++){if(array[iLoop] < inertNumber){for(iLoop2 = arraysize - 1; iLoop2 >= iLoop; iLoop2--) {array[iLoop2 + 1] = array[iLoop2]; }array[iLoop] = inertNumber; break;}}if(iLoop >= arraysize){array[arraysize] = inertNumber;}}}int main(){int iLoop;int a[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");return 0;}9.32int fun(int number, int array[]){int iLoop = 0;int iLoop2;int binLen;int midNumber;int div;int remain;midNumber = number;do{div = midNumber/2;remain = midNumber%2;midNumber = div;array[iLoop++] = remain;}while(midNumber);binLen = iLoop;for(iLoop2 = 0, iLoop = binLen - 1; iLoop2 < iLoop; iLoop2++, iLoop--) {midNumber = array[iLoop2];array[iLoop2] = array[iLoop];array[iLoop] = midNumber;}return binLen;}9.33#include#include#define N 15void fun(int array[], int arraysize){int x;int iLoop;int iLoop2;for(iLoop = 0; iLoop < arraysize; iLoop++){iLoop2 = 0;x = rand()%20;do{if(x == array[iLoop2] && iLoop > 0){x = rand()%20;iLoop2 = 0;}iLoop2++;}while(iLoop2 < iLoop);array[iLoop] = x;}}int main(){int a[N];int iLoop;fun(a, N);for(iLoop = 0; iLoop < N; iLoop++){printf("%d\n", a[iLoop]);}return 0;}第十章一. 选择题10.1 C 10.2 B 10.3 C 10.4 B 10.5 C 10.6 A 10.7 C 10.8 A 10.9 C 10.10 C二. 填空题10.11 GFEDCB10.12 XYZ10.13 SO10.14 1010.15 Itis10.16 strlen(str)-1 j--10.17 310.18 goodgood!三. 编程题10.19char* mygets(char *str){int iLoop = 0;char ch;while((ch=getchar()) != '\n')str[iLoop++] = ch;}str[iLoop] = '\0';return str;}char * myputs(char *str){int iLoop = 0;while(str[iLoop]){putchar(str[iLoop++]);}putchar('\n');return str;}10.20#include#includeint fun(char *str){int len;int iLoop1;int iLoop2;int result = 1;len = strlen(str);for(iLoop1 = 0, iLoop2 = len - 1; iLoop1 < iLoop2; iLoop1++, iLoop2--) {if(str[iLoop1] != str[iLoop2]){result = 0;break;}}return result;}int main(){char a[20] = "ABCDCBA";char b[20] = "ABCDEBA";printf("%d\n", fun(a));printf("%d\n", fun(b));return 0;}10.21char fun(char *str, int pos){int len;int iLoop;char ch;len = strlen(str);if(pos > len){return NULL;}ch = str[pos];for(iLoop = pos; iLoop < len - 1; iLoop++) {str[iLoop] = str[iLoop + 1];}str[len-1] = '\0';return ch;}第十一章一. 选择题11.1 D 11.2 B 11.3 A 11.4 C二. 填空题11.5 IJKLEFGHABCD11.6 711.7 811.8 *(s+j) i+1 i11.9 1711.10 (*fun)() (*fun)(a+i*h)/h mypoly三. 编程题11.11#include#include#define N 81int main(int argc, char **argv)char sig;int dig;int pos;char str[N] = {'\0'};char outStr[N] = {'\0'};if(argc < 2){sig = '-';dig = 10;}else{sig = argv[1][0];dig = argv[1][1] - '0';}printf("请输入一个字符串:"); gets(str);if(sig == '-'){pos = strlen(str) - dig;if(pos <= 0){pos = 0;}strcpy(outStr, str + pos);}else if(sig == '+'){strcpy(outStr, str);pos = strlen(outStr);if(pos > dig){pos = dig;}outStr[pos] = '\0';}printf("处理后的字串为:"); printf("%s\n", outStr);}11.12#include#includevoid movebin(char *bin){int len;int iLoop;len = strlen(bin);for(iLoop = len; iLoop > 0; iLoop--) {bin[iLoop] = bin[iLoop - 1];}return;}void fun(int n, char *bin){int pos;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;}int main(){int a = 4;char bin[50] = {""};printf("%s\n", bin);return 0;}11.13#includelong fun(int n){if(n == 1){return n;}else{return fun(n-1) + n;}}int main(){int num;int sum;printf("请输入一个自然数:"); scanf("%d", &num);sum = fun(num);printf("结果是:%d\n", sum);return 0;}11.14#includeint fun(int n){if(n == 0 || n == 1){return 1;}else{return fun(n-1) + fun(n-2);}}int main(){int num;int result;printf("请输入一个自然数:");scanf("%d", &num);result = fun(num);printf("斐波拉契级数为:%d\n", result);return 0;}第十二章一. 选择题12.1 B 12.2 B 12.3 A 12.4 C 12.5 D 12.6 B 12.7 A 12.8 A二. 填空题12.9 2,5,1,2,3,-212.10 2468第十三章一. 选择题13.1 A 13.2 C 13.3 B 13.4 C 13.5 D 13.6 D 13.7 D二. 填空题13.8 ar=9 ar=9 ar=1113.9 int* s *b三. 编程题13.10#define MYALPHA(C) ((C>='A' && C<='Z') || (C>='a' && C<='z')) ? 1 : 0 13.11#define SWAP(t,x,y) {t tmp; tmp=x; x=y; y=tmp;}13.12#include#includeint main(){int *p;int tmp;int iLoop;int iLoop2;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 %d\n", *p, *(p+1), *(p+2));free(p);p = NULL;return 0;}第十四章一. 选择题14.1 D 14.2 D 14.3 D 14.4 A 14.5 C 14.6 C 14.7 C 14.8 B二. 填空题14.9 struct link *next14.10 p->next m>p->data14.11 (struct list*) struct list struct list* struct list return h三. 编程题14.12#include#define N 3struct stud{char num[5], name[10];int s[4];double ave;};void readrec(struct stud array[], int size){int iLoop;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;}void writerec(struct stud array[], int size){int iLoop;for(iLoop=0; iLoop{printf("%s\t%s\t%d\t%d\t%d\t%d\t%f\n",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;}int main(){struct stud stu[N];readrec(stu, N);writerec(stu, N);return 0;}14.13#include#include#define N 100struct node{int data;struct node* next;};int seekMaxValue(struct node *pNode){int max;struct node* pMove;pMove = pNode;max = pMove->data;pMove = pMove->next;while(pMove){if(max < pMove->data){max = pMove->data;}pMove = pMove->next;}return max;}struct node* seekMaxValueAddress(struct node *pNode) {int max;struct node* maxAddress;struct node* pMove;pMove = pNode;max = pMove->data;maxAddress = pMove;pMove = pMove->next;while(pMove){if(max < pMove->data){max = pMove->data;maxAddress = pMove;}pMove = pMove->next;}return maxAddress;}int main(){struct node* head;struct node* pNode;int iLoop;head = (struct node*)malloc(sizeof(struct node)); pNode = head;pNode->next = NULL;for(iLoop=0; iLoop {pNode->next = (struct node*)malloc(sizeof(struct node)); pNode = pNode->next;pNode->next = NULL;pNode->data = iLoop;}printf("%d\n", seekMaxValue(head->next));printf("%d\n", seekMaxValueAddress(head->next));return 0;}第十五章一.选择题15.1 D 15.2 A 15.3 B 15.4 A二. 填空题15.5 1111000015.6 a^a15.7 a|0xffff15.8 x|0xff0015.9 a=(012500>>2)15.10 ch|0x20第十六章一. 选择题16.1 B 16.2 C二. 填空题16.3 3 !feof(f1) f2 fclose(f1) fclose(f2)16.4 fopen(fname,"w") ch16.5 "r" !feof(fp) fgetc(fp)16.6 CCCCBBBBAAAA三. 编程题#include#define N 10#define LEN 81int main(){char *str[N] = {"AAAAAAAAA", "BBBBBBBBB", "CCCCCCCCC", "DDDDDDDDD", "EEEEEEEEE", "FFFFFFFFF", "GGGGGGGGG", "HHHHHHHHH","IIIIIIIII","JJJJJJJJJ"};char str2[N][LEN];FILE *fp;int iLoop;fp = fopen("str.txt", "w");if(fp == NULL){printf("创建文件失败!\n"); return 1;}else{for(iLoop = 0; iLoop < N; iLoop++) {fputs(str[iLoop], fp);fputs("\n",fp);}}fclose(fp);fp = fopen("str.txt", "r");if(fp == NULL){printf("打开文件失败!\n"); return 1;}elsefor(iLoop = 0; iLoop < N; iLoop++){fgets(str2[iLoop], LEN - 1, fp);}}fclose(fp);for(iLoop = 0; iLoop < N; iLoop++){printf("%s", str2[iLoop]);}return 0;}16.8#include#define N 10int main(){float num;int iLoop;FILE *fp;fp = fopen("num.bin", "wb+");if(fp == NULL){printf("创建文件失败!\n");return 1;}/* 从键盘读入10个数并写文件*/printf("请输入%d个数:", N); for(iLoop = 0; iLoop < N; iLoop++){scanf("%f", &num);fwrite(&num, sizeof(num), 1, fp);}/* 文件指针回到开始处*/rewind(fp);/* 从文件读出10个数并显示*/for(iLoop = 0; iLoop < N; iLoop++){fread(&num, sizeof(num), 1, fp);printf("%f\n", num);/* 移文件指针到第四个数开始处*/fseek(fp, 3L*sizeof(num), SEEK_SET);/* 读入一个新数据*/printf("请输入一个新数据:"); scanf("%f", &num);fwrite(&num, sizeof(num), 1, fp);/* 文件指针回到开始处*/rewind(fp);/* 从文件读出10个数并显示*/for(iLoop = 0; iLoop < N; iLoop++){fread(&num, sizeof(num), 1, fp);printf("%f\n", num);} /* 关闭文件*/fclose(fp);return 0;}。
《全国计算机等级考试二级教程——C语言程序设计》课后习题详细答案

《全国计算机等级考试二级教程——C语言程序设计》习题分析与详细解答第一章程序设计基本概念习题分析与解答1.1 【参考答案】EXE1.2 【参考答案】[1].C [2].OBJ [3].EXE1.3 【参考答案】[1]顺序结构[2]选择结构[3]循环结构第二章C程序设计的初步知识习题分析与解答一、选择题2.1 【参考答案】B)2.2 【参考答案】D)2.3 【参考答案】B)2.4 【参考答案】A)2.5 【参考答案】C)2.6 【参考答案】A)2.7 【参考答案】B)2.8 【参考答案】B)2.9 【参考答案】D)2.10 【参考答案】C)2.11 【参考答案】B)2.12 【参考答案】B)2.13 【参考答案】A)二、填空题2.14 【参考答案】[1]11 [2]122.15 【参考答案】[1]4.2 [2]4.22.16 【参考答案】[1]{ [2]} [3]定义[4]执行2.17 【参考答案】[1]关键字[2]用户标识符2.18 【参考答案】[1]int [2]float [3]double2.19 【参考答案】float a1=1.0, a2=1.0;或float a1=1, a2=1;(系统将自动把1转换为1.0)2.20 【参考答案】存储单元2.21 【参考答案】 3.52.22 【参考答案】[1]a*b/c [2]a/c*b [3]b/c*a2.23 【参考答案】把10赋给变量s2.24 【参考答案】[1]位[2]1位二进制数据(0或1)2.25 【参考答案】[1]8 [2]127 [3]01111111 [4]-128 [ 5 ] 10000000 2.26 【参考答案】[1]32767 [2]-32768 [3]100002.27 【参考答案】[1]十[2]八[3]十六三、上机改错题2.28 【分析与解答】第1行的错误:(1) include是一个程序行,因此在此行的最后不应当有分号(;)。
全国计算机等级考试二级教程--C语言程序设计课后习题答案

《全国计算机等级考试二级教程--C语言程序设计》课后习题答案第一章1.1 EXE1.2 C OBJ EXE1.3 顺序选择循环第二章一. 选择题2.1 B 2.2 D 2.3 B 2.4 A 2.5 C 2.6 A 2.7 B2.8 B 2.9 D 2.10 C 2.11 B 2.12 B 2.13 A二. 填空题2.14 11 122.15 4.2 4.22.16 { } 定义执行语句2.17 关键字用户标识符2.18 int float double2.19 float a1=1; float a2=1;2.20 存储单元2.213.52.22 (a*b)/c a*b/c a/c*b2.23 把常量10赋给变量s2.24 位1或02.25 8 127 0111111 -128 100000002.26 32767 -32768 10000000000000002.27 10 8 16三. 上机改错题2.28#include "stdio.h"; 删除行尾的";"main(); / * main function * / 删除")"后的";",注释中的*要紧靠“/”,即应为“/*”和“*/”函数开始处遗失了一个“{”float r,s ; /*/*r is radius*/,/* s is area of circuilar*/*/ 注释符号不可嵌套使用r = 5.0 ;s = 3.14159 * r * r ;printf("%f\n",s) 行尾遗失了“;”函数结束处遗失了一个“}”2.29#include "stdio.h"main /* main function */ main后遗失了“()”{float a,b,c,v; /*a,b,c are sides, v is volume of cube */a=2.0; b=3.0; c=4.0 行尾遗失了“;”v=a*b*c;printf("%f\n", v) 行尾遗失了“;”}第三章一. 选择题3.1 C 3.2 C 3.3 D 3.4 C 3.5 D 3.6 B 3.7 C 3.8 D 3.9 A 3.10 B3.11 C 3.12 D 3.13 D 3.14 A 3.15 C 3.16 C 3.17 C 3.18 无答案3.19 C 3.20 B二. 填空题3.21 (1)-2002500(2)i=-200,j=2500(3)i=-200j=25003.22 12 0 03.23 一条语句;3.24 ;3.25 100,25.81,1.89234 100 25.81 1.89234 100 25.81 1.89234 3.26 x=127,x= 127,x= 177,x= 7f,x= 1273.27 x=127,x=127 ,x=$127 ,x=$000127,x=%06d3.28 a=513.789215,a= 513.79,a= 513.78921500,a= 513.78921500三. 编程题和改错题3.29 修改后的程序如下:main(){double a,b,c,s,v;printf("input a,b,c:");scanf("%lf%lf%lf",&a,&b,&c);s =a*b;v=a*b*c;printf("a=%f,b=%f,c=%f\n", a,b,c);printf("s=%f,v=%f\n",s,v);}3.30#includemain(){int a=560,b=60;printf("560 minute is %d hour and %d minute.\n",a/b,a%b); }3.31#includemain(){int a,b;a=1500;b=350;printf("a div b is : %d\n",a/b);printf("a mod b is : %d\n",a%b);}3.32#includemain(){double a,b,c,ave;printf ("input 3 double number : \n");scanf ("%lf%lf%lf",&a,&b,&c);printf ("%.1f\n",(a+b+c)/3);}3.33#includevoid main()int a,b,c,t;printf("请依次输入整数a,b,c:");scanf("%d%d%d",&a,&b,&c);printf("\n你输入的值是: a=%d,b=%d,c=%d\n",a,b,c);t=b;b=a;a=c;c=t;printf("交换之后的值是:a=%d,b=%d,c=%d\n",a,b,c);}第四章一. 选择题4.1 A 4.2 A 4.3 A 4.4 D 4.5 C 4.6 A 4.7 B 4.8 C 4.9 D 4.10 C二. 填空题4.11 非0 04.12 < > >= <=同级== !=同级4.13 ! && ||4.15 !4.16 a == b || a < c x > 4 || x < -44.17 14.18 x <= 0 1 > 04.19 3 2 24.20 *#三. 编程题4.21 略#include/* 检查日期的合法性*/int checkdate(int year, int month, int day) {if(year < 1900 || year > 2005){printf("输入的年份无效!\n");return 0;}else if(month < 0 && month > 12){printf("输入的月份无效!\n");return 0;}else if(day <= 0 && day > 31){printf("输入的日期无效!\n");return 0;}else{switch(month){case 4:case 9:case 11:if(day > 30){printf("输入的日期无效!\n");return 0;}break;case 2:if((year%4 == 0 && year%100 != 0) || year%400 == 0) {if(day > 29){printf("输入的日期无效!\n");return 0;}}else{if(day > 28){printf("输入的出生日期无效!\n");return 0;}}}/* end of switch(m0)*/}return 1;}void main(){int y0, m0, d0; /* 生日*/int y1, m1, d1; /* 当前日期*/int years, 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;}else if(y0 > y1){printf("出生年份比当前年份晚!\n"); return;}else if(y0 == y1){if(m0 > m1){printf("出生年月比当前年月晚!\n"); return;}else if(m0 == m1){if(d0 > d1){printf("出生年月日比当前年月日晚!\n"); return;}}}}/* 计算实足年龄*/years = y1 - y0;months = m1 - m0;days = d1 - d0;/* 修正实足年龄天数*/if(days < 0){months--;switch(m1){case 1:case 5:case 7:case 10:case 12:days += 30;break;case 2:case 4:case 6:case 8:case 9:case 11:days += 31;break;case 3:if((y1%4 == 0 && y1%100 != 0) || y1%400 == 0) {days += 29;}else{days += 28;}break;}/* end of switch(m1) */}/* end of if(days < 0) *//* 修正实足年龄月数*/if(months < 0){months += 12;years--;}/* end of if(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#includevoid main(){int a;printf ("请输入一个整数:"); scanf ("%d",&a);if (a%2==0){printf ("%d 是偶数\n", a);}else{printf ("%d 是奇数\n", a);}}4.24#includevoid main(){int a,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语句#includevoid main(){int x,y;printf("input x :");scanf("%d",&x);if ( x>-5 && x<0 ){printf("y is %d\n",y=x);}if ( x==0 ){printf("y is %d\n",y=x-1);}if ( x>0 && x<10 ){printf("y is %d\n",y=x+1);}if ( x>=10 || x<=-5){printf("error\n");}}(2)嵌套的if语句#includevoid main(){int x,y;printf("input x :");scanf("%d",&x);printf("\n");if(x < 0){if(x > -5){printf("y is %d.\n",y=x); }else{printf("error!\n");}}if(0 == x){printf("y is %d.\n",y=x-1);if(x > 0){if(x < 10){printf("y is %d.\n",y=x+1); }else{printf("error!\n");}}}(3)if_else语句#includevoid main(){int x,y;printf("input x :");scanf("%d",&x);if( x>-5 && x<0 ){printf("y is %d.\n",y=x); }else if( x==0 )printf("y is %d.\n",y=x-1); }else if( x>0 && x<10 ) {printf("y is %d.\n",y=x+1); }else{printf("error!\n");}}(4)switch语句#includevoid main(){int x,y;printf("input x : ");scanf("%d",&x);switch (x){case -4:case -3:case -2:case -1:printf("y is %d.\n",y=x);break;case 0:printf("y is %d.\n",y=x-1);break;case 1:case 2:case 3:case 4:case 5:case 6:case 7:case 8:case 9:printf("y is %d.\n",y=x+1);break;default:printf("error!\n");}}第五章一. 选择题5.1 D 5.2 C 5.3 B 5.4 C 5.5 C 5.6 B 5.7 D 5.8 A 5.9 D 5.10 D二. 填空题5.11 5 4 65.12 死循环5.13 -15.14 115.15 d=1.0 k++ k<=n5.16 x>=0 x三. 编程题5.17#includevoid main(){int i;int sig = 1;int sum = 0;for(i=1; i<=101; i++,i++) {sum += sig*i;sig *= -1;}printf("sum=%d\n", sum); }5.18(1)#includevoid main(){int i;double m=1.0; double e = 1.0;for(i=1; i<50; i++) {m *= i;e += 1/m;}printf("e=%f\n",e); }(2)#includevoid main(){int i=1;double m=1.0; double e = 1.0; while(1/m >= 0.0004) {m *= i;e += 1/m;i++;}printf("e=%f\n",e);}5.19#includevoid main(){int year;int col = 0;for(year=1600; year<=2000; year++){if((year%4 == 0 && year%100 != 0) || year%400 == 0) {printf("%d\t", year);col++;if(col%5 == 0){printf("\n");}}}printf("\n");}5.20#include#define N 7void main(){int i;int j;int m;int k = 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.1 B 6.2 D 6.3 A 6.4 A 6.5 B 6.6 D 6.7 D 6.8 B 6.9 A 6.10 A 6.11 C二. 填空题6.12 -16.13 16.14 ctype.h6.15 16.16 10A 20B 30C 40D6.177.29 101.298AB6.18 A7.29B101.2986.19 A B C (每个字符后有三个空格)三. 编程题6.20#include#define N 80void main(){char str[N];int iLoop = 0;gets(str);while(str[iLoop]){printf("%c-%d\t", str[iLoop],str[iLoop]); iLoop++;if(iLoop%3 == 0){printf("\n");}}printf("\n");}6.21#include#define N 80void main(){char str[N];int num = 0;int iLoop = 0;gets(str);while(str[iLoop]){if(str[iLoop] >= '0' && str[iLoop] <= '9') {num = 10*num + (str[iLoop] - '0');}iLoop++;}printf("%d\n",num);}6.22#include#include#define N 80void main(){char str[N];int num = -1;do{gets(str);num++;}while(strcmp(str, "EOF"));printf("您输入了%d行字符!\n",num); }#include#define N 80void main(){char str[N];int iLoop = 0;int num = 0;gets(str);while(str[iLoop] && iLoop < N){if(str[iLoop] >= 'a' && str[iLoop] <= 'z'){num++;}iLoop++;}printf("您输入了字符中有%d个小写字母!\n",num); }6.24#includevoid main(){int iLoop1;int iLoop2;printf("请输入图案的行数(不大于26):");scanf("%d", &line);for(iLoop1 = 0; iLoop1 < line; iLoop1++){for(iLoop2 = 0; iLoop2 < line - iLoop1; iLoop2++) {printf(" ");}for(iLoop2 = 0; iLoop2 < 2*iLoop1+1; iLoop2++) {printf("%c",iLoop1 + 'A');}printf("\n");}}第七章一. 选择题7.1 C 7.2 C 7.3 B 7.4 C 7.5 A 7.6 D 7.7 A二. 填空题7.8 127.9 9.0000007.10 47.11 n=1 s7.12 <=y z*x7.13 1 s*i 0 f(k)三. 程序调试和编程题7.14fun(int n){ int k,yes;for(k=2; k<=n/2; k++){if(n%k == 0) { yes = 0; break;} else yes = 1;}return yes;}7.15int mymod(int a, int b){return a%b;}7.16double fun(int n){double sum = 0;int iLoop;int sig = -1;for(iLoop=1; iLoop<=n; iLoop++) {sig *= -1;sum += sig*1.0/iLoop;}return sum;}7.17double fun(int n){double t = 1.0;int iLoop;long tmp;for(iLoop=2; iLoop<=n; iLoop++) {tmp = iLoop*iLoop;t -= 1.0/tmp;}return t;}7.18#includedouble fun(double x){return x*x + 5*x + 4;}void main(){int x = 2;printf("y1=%f\n", fun(x));printf("y2=%f\n", fun(x+15));printf("y3=%f\n", fun(sin(x)));}第八章一. 选择题8.1 A 8.2 B 8.3 B 8.4 C 8.5 B 8.6 B 8.7 C 8.8 D 8.9 B 8.10 C 8.11 C 8.12 C二. 填空题8.13 1108.14 7 18.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)10 20 30 40 50三. 编程题8.17void fun(double x, double y, double *sum, double *div){*sum = x + y;*div = x - y;return;}8.18void fun(double x, double y, double z, double *max, double *min){*max = x;*min = x;if(*max < y){*max = y;}if(*max < z){}if(*min > y){*min = y;}if(*min > z){*min = z;}return;}第九章一. 选择题9.1 D 9.2 A 9.3 A 9.4 C 9.5 C 9.6 A 9.7 B 9.8 D 9.9 C 9.10 C 9.11 C 9.12 D 9.13 D 9.14 A 9.15 A 9.16 A 9.17 C 9.18 C二. 填空题9.19 9 09.20 69.21 129.22 39.23 27219.24 -850,2,09.25 k=p k9.26 (c=getchar()) c-'A'三. 编程题9.27#include#define N 81int main(){int counter[10] = {0};int iLoop = 0;char str[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 - %d\n", iLoop, counter[iLoop]); }return 0;}9.28void fun(int array[], int arraysize, int start) {int iLoop;if(start < arraysize-1){if(start <=0){start = 1;}for(iLoop = start; iLoop < arraysize; iLoop++) {array[iLoop-1] = array[iLoop];}}for(iLoop = 0; iLoop < arraysize; iLoop++) {printf("No.%d = %d\n", iLoop, array[iLoop]); }}9.29int fun(int arry1[], int arry2[], int arrysize) {int iLoop;int counter = 0;for(iLoop = 0; iLoop < arrysize; iLoop++) {if(arry1[iLoop] % 2){arry2[counter++] = arry1[iLoop];}}return counter;}9.30void fun(char array[], int arraysize){int iLoop1;int iLoop2;char temp;/* 冒泡排序*/for(iLoop1 = 0; iLoop1 < arraysize - 1; iLoop1++){for(iLoop2 = 0; iLoop2 < arraysize - 1 - iLoop1; iLoop2++) {if(array[iLoop2] < array[iLoop2 + 1]){temp = array[iLoop2];array[iLoop2] = array[iLoop2 + 1];array[iLoop2 + 1] = temp;}}}}9.31#includevoid fun(int array[], int arraysize, int inertNumber){int iLoop;int iLoop2;if(array[0] < array[arraysize-1]){for(iLoop = 0; iLoop< arraysize; iLoop++){if(array[iLoop] > inertNumber){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< arraysize; iLoop++){if(array[iLoop] < inertNumber){for(iLoop2 = arraysize - 1; iLoop2 >= iLoop; iLoop2--) {array[iLoop2 + 1] = array[iLoop2];}array[iLoop] = inertNumber;break;}}if(iLoop >= arraysize){array[arraysize] = inertNumber;}}}int main(){int iLoop;int a[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");return 0;}9.32int fun(int number, int array[]){int iLoop = 0;int iLoop2;int binLen;int midNumber;int div;int remain;midNumber = number;do{div = midNumber/2;remain = midNumber%2;midNumber = div;array[iLoop++] = remain;}while(midNumber);binLen = iLoop;for(iLoop2 = 0, iLoop = binLen - 1; iLoop2 < iLoop; iLoop2++, iLoop--) {midNumber = array[iLoop2];array[iLoop2] = array[iLoop];array[iLoop] = midNumber;}return binLen;}9.33#include#include#define N 15void fun(int array[], int arraysize){int x;int iLoop2;for(iLoop = 0; iLoop < arraysize; iLoop++){iLoop2 = 0;x = rand()%20;do{if(x == array[iLoop2] && iLoop > 0){x = rand()%20;iLoop2 = 0;}iLoop2++;}while(iLoop2 < iLoop);array[iLoop] = x;}}int main(){int a[N];int iLoop;fun(a, N);for(iLoop = 0; iLoop < N; iLoop++){printf("%d\n", a[iLoop]);}return 0;}第十章一. 选择题10.1 C 10.2 B 10.3 C 10.4 B 10.5 C 10.6 A 10.7 C 10.8 A 10.9 C 10.10 C二. 填空题10.11 GFEDCB10.12 XYZ10.13 SO10.14 1010.16 strlen(str)-1 j--10.17 310.18 goodgood!三. 编程题10.19char* mygets(char *str){int iLoop = 0;char ch;while((ch=getchar()) != '\n'){str[iLoop++] = ch;}str[iLoop] = '\0';return str;}char * myputs(char *str){int iLoop = 0;while(str[iLoop]){putchar(str[iLoop++]);}putchar('\n');return str;}10.20#include#includeint fun(char *str){int len;int iLoop1;int iLoop2;int result = 1;len = strlen(str);for(iLoop1 = 0, iLoop2 = len - 1; iLoop1 < iLoop2; iLoop1++, iLoop2--) {if(str[iLoop1] != str[iLoop2]){break;}}return result;}int main(){char a[20] = "ABCDCBA";char b[20] = "ABCDEBA";printf("%d\n", fun(a));printf("%d\n", fun(b));return 0;}10.21char fun(char *str, int pos){int len;int iLoop;char ch;len = strlen(str);if(pos > len){return NULL;}ch = str[pos];for(iLoop = pos; iLoop < len - 1; iLoop++) {str[iLoop] = str[iLoop + 1];}str[len-1] = '\0';return ch;}第十一章一. 选择题11.1 D 11.2 B 11.3 A 11.4 C二. 填空题11.5 IJKLEFGHABCD11.6 711.7 811.8 *(s+j) i+1 i11.9 1711.10 (*fun)() (*fun)(a+i*h)/h mypoly三. 编程题11.11#include#include#define N 81int main(int argc, char **argv){char sig;int dig;int pos;char str[N] = {'\0'};char outStr[N] = {'\0'};if(argc < 2){sig = '-';dig = 10;}else{sig = argv[1][0];dig = argv[1][1] - '0';}printf("请输入一个字符串:");gets(str);if(sig == '-'){pos = strlen(str) - dig;if(pos <= 0){pos = 0;}strcpy(outStr, str + pos);}else if(sig == '+'){strcpy(outStr, str);pos = strlen(outStr);if(pos > dig){pos = dig;}outStr[pos] = '\0';}printf("处理后的字串为:");printf("%s\n", outStr);return 0;}11.12#include#includevoid movebin(char *bin){int len;int iLoop;len = strlen(bin);for(iLoop = len; iLoop > 0; iLoop--) {bin[iLoop] = bin[iLoop - 1];}return;}void fun(int n, char *bin){int pos;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;}int main(){int a = 4;char bin[50] = {""};fun(a, bin);printf("%s\n", bin);return 0;}11.13#includelong fun(int n){if(n == 1){return n;}else{return fun(n-1) + n;}}int main(){int num;int sum;printf("请输入一个自然数:"); scanf("%d", &num);sum = fun(num);printf("结果是:%d\n", sum); return 0;}11.14#includeint fun(int n){if(n == 0 || n == 1){return 1;}else{return fun(n-1) + fun(n-2);}}int main(){int num;int result;printf("请输入一个自然数:");scanf("%d", &num);result = fun(num);printf("斐波拉契级数为:%d\n", result);return 0;}第十二章一. 选择题12.1 B 12.2 B 12.3 A 12.4 C 12.5 D 12.6 B 12.7 A 12.8 A二. 填空题12.9 2,5,1,2,3,-212.10 2468第十三章一. 选择题13.1 A 13.2 C 13.3 B 13.4 C 13.5 D 13.6 D 13.7 D二. 填空题13.8 ar=9 ar=9 ar=1113.9 int* s *b三. 编程题13.10#define MYALPHA(C) ((C>='A' && C<='Z') || (C>='a' && C<='z')) ? 1 : 0 13.11#define SWAP(t,x,y) {t tmp; tmp=x; x=y; y=tmp;}13.12#include#includeint main(){int *p;int tmp;int iLoop;int iLoop2;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 %d\n", *p, *(p+1), *(p+2));free(p);p = NULL;return 0;}第十四章一. 选择题14.1 D 14.2 D 14.3 D 14.4 A 14.5 C 14.6 C 14.7 C 14.8 B14.9 struct link *next14.10 p->next m>p->data14.11 (struct list*) struct list struct list* struct list return h三. 编程题14.12#include#define N 3struct stud{char num[5], name[10];int s[4];double ave;};void readrec(struct stud array[], int size){int iLoop;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;}void writerec(struct stud array[], int size){int iLoop;for(iLoop=0; iLoop{printf("%s\t%s\t%d\t%d\t%d\t%d\t%f\n",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;}{struct stud stu[N];readrec(stu, N);writerec(stu, N);return 0;}14.13#include#include#define N 100struct node{int data;struct node* next;};int seekMaxValue(struct node *pNode){int max;struct node* pMove;pMove = pNode;max = pMove->data;pMove = pMove->next;while(pMove){if(max < pMove->data){max = pMove->data;}pMove = pMove->next;}return max;}struct node* seekMaxValueAddress(struct node *pNode) {int max;struct node* maxAddress;struct node* pMove;pMove = pNode;max = pMove->data;maxAddress = pMove;pMove = pMove->next;while(pMove){if(max < pMove->data){max = pMove->data;maxAddress = pMove;}pMove = pMove->next;}return maxAddress;}int main(){struct node* head;struct node* pNode;int iLoop;head = (struct node*)malloc(sizeof(struct node)); pNode = head;pNode->next = NULL;for(iLoop=0; iLoop {pNode->next = (struct node*)malloc(sizeof(struct node)); pNode = pNode->next;pNode->next = NULL;pNode->data = iLoop;}printf("%d\n", seekMaxValue(head->next));printf("%d\n", seekMaxValueAddress(head->next));return 0;}第十五章一. 选择题15.1 D 15.2 A 15.3 B 15.4 A二. 填空题15.5 1111000015.6 a^a15.7 a|0xffff15.8 x|0xff0015.9 a=(012500>>2)15.10 ch|0x20第十六章一. 选择题16.1 B 16.2 C二. 填空题16.3 3 !feof(f1) f2 fclose(f1) fclose(f2) 16.4 fopen(fname,"w") ch16.5 "r" !feof(fp) fgetc(fp)16.6 CCCCBBBBAAAA三. 编程题16.7#include#define N 10#define LEN 81int main(){char *str[N] = {"AAAAAAAAA", "BBBBBBBBB", "CCCCCCCCC", "DDDDDDDDD", "EEEEEEEEE", "FFFFFFFFF", "GGGGGGGGG", "HHHHHHHHH","IIIIIIIII","JJJJJJJJJ"};char str2[N][LEN];FILE *fp;int iLoop;fp = fopen("str.txt", "w");if(fp == NULL){printf("创建文件失败!\n");return 1;}else{for(iLoop = 0; iLoop < N; iLoop++) {fputs(str[iLoop], fp);fputs("\n",fp);}}fclose(fp);fp = fopen("str.txt", "r");if(fp == NULL){printf("打开文件失败!\n"); return 1;}else{for(iLoop = 0; iLoop < N; iLoop++) {fgets(str2[iLoop], LEN - 1, fp);}}fclose(fp);for(iLoop = 0; iLoop < N; iLoop++) {printf("%s", str2[iLoop]);}return 0;}16.8#include#define N 10int main(){float num;int iLoop;FILE *fp;fp = fopen("num.bin", "wb+");if(fp == NULL){printf("创建文件失败!\n");return 1;}/* 从键盘读入10个数并写文件*/ printf("请输入%d个数:", N);for(iLoop = 0; iLoop < N; iLoop++) {scanf("%f", &num);fwrite(&num, sizeof(num), 1, fp);}/* 文件指针回到开始处*/rewind(fp);/* 从文件读出10个数并显示*/for(iLoop = 0; iLoop < N; iLoop++) {fread(&num, sizeof(num), 1, fp); printf("%f\n", num);}/* 移文件指针到第四个数开始处*/ fseek(fp, 3L*sizeof(num), SEEK_SET); /* 读入一个新数据*/printf("请输入一个新数据:");scanf("%f", &num);fwrite(&num, sizeof(num), 1, fp);/* 文件指针回到开始处*/rewind(fp);/* 从文件读出10个数并显示*/for(iLoop = 0; iLoop < N; iLoop++) {fread(&num, sizeof(num), 1, fp); printf("%f\n", num);}/* 关闭文件*/fclose(fp);return 0;}她含着笑,切着冰屑悉索的萝卜,她含着笑,用手掏着猪吃的麦糟,她含着笑,扇着炖肉的炉子的火,她含着笑,背了团箕到广场上去晒好那些大豆和小麦,大堰河,为了生活,在她流尽了她的乳液之后,她就用抱过我的两臂,劳动了。
全国计算机等级考试二级教程--C语言程序设计课后习题答案范文

《全国计算机等级考试二级教程--C语言程序设计》课后习题答案第一章1.1 EXE1.2 C OBJ EXE1.3 顺序选择循环第二章一. 选择题2.1 B 2.2 D 2.3 B 2.4 A 2.5 C 2.6 A 2.7 B2.8 B 2.9 D 2.10 C 2.11 B 2.12 B 2.13 A二. 填空题2.14 11 122.15 4.2 4.22.16 { } 定义执行语句2.17 关键字用户标识符2.18 int float double2.19 float a1=1; float a2=1;2.20 存储单元2.213.52.22 (a*b)/c a*b/c a/c*b2.23 把常量10赋给变量s2.24 位1或02.25 8 127 -1282.26 32767 -32768 000002.27 10 8 16三. 上机改错题2.28#include "stdio.h"; 删除行尾的";"main(); / * main function * / 删除")"后的";",注释中的*要紧靠“/”,即应为“/*”和“*/”函数开始处遗失了一个“{”float r,s ; /*/*r is radius*/,/* s is area of circuilar*/*/ 注释符号不可嵌套使用r = 5.0 ;s = 3.14159 * r * r ;printf("%f\n",s) 行尾遗失了“;”函数结束处遗失了一个“}”2.29#include "stdio.h"main /* main function */ main后遗失了“()”{float a,b,c,v; /*a,b,c are sides, v is volume of cube */a=2.0; b=3.0; c=4.0 行尾遗失了“;”printf("%f\n", v) 行尾遗失了“;”}第三章一. 选择题3.1 C 3.2 C 3.3 D 3.4 C 3.5 D 3.6 B 3.7 C 3.8 D 3.9 A 3.10 B3.11 C 3.12 D 3.13 D 3.14 A 3.15 C 3.16 C 3.17 C 3.18 无答案3.19 C 3.20 B二. 填空题3.21 (1)-(2)i=-200,j=2500(3)i=-200j=25003.22 12 0 03.23 一条语句;3.24 ;3.25 100,25.81,1.89234 100 25.81 1.89234 100 25.81 1.892343.26 x=127,x= 127,x= 177,x= 7f,x= 1273.27 x=127,x=127 ,x=$127 ,x=$,x=%06d3.28 a=513.,a= 513.79,a= 513.,a= 513.三. 编程题和改错题3.29 修改后的程序如下:main(){double a,b,c,s,v;printf("input a,b,c:");scanf("%lf%lf%lf",&a,&b,&c);s =a*b;v=a*b*c;printf("a=%f,b=%f,c=%f\n", a,b,c);printf("s=%f,v=%f\n",s,v);}3.30#includemain(){int a=560,b=60;printf("560 minute is %d hour and %d minute.\n",a/b,a%b);}3.31#includemain(){a=1500;b=350;printf("a div b is : %d\n",a/b);printf("a mod b is : %d\n",a%b);}3.32#includemain(){double a,b,c,ave;printf ("input 3 double number : \n");scanf ("%lf%lf%lf",&a,&b,&c);printf ("%.1f\n",(a+b+c)/3);}3.33#includevoid main(){int a,b,c,t;printf("请依次输入整数a,b,c:");scanf("%d%d%d",&a,&b,&c);printf("\n你输入的值是: a=%d,b=%d,c=%d\n",a,b,c);t=b;b=a;a=c;c=t;printf("交换之后的值是:a=%d,b=%d,c=%d\n",a,b,c);}第四章一. 选择题4.1 A 4.2 A 4.3 A 4.4 D 4.5 C 4.6 A 4.7 B 4.8 C 4.9 D 4.10 C二. 填空题4.11 非0 04.12 < > >= <=同级== !=同级4.13 ! && ||4.15 !4.16 a == b || a < c x > 4 || x < -44.17 14.18 x <= 0 1 > 04.19 3 2 24.20 *#三. 编程题4.21 略4.22/* 检查日期的合法性*/int checkdate(int year, int month, int day){if(year < 1900 || year > 2005){printf("输入的年份无效!\n");return 0;}else if(month < 0 && month > 12){printf("输入的月份无效!\n");return 0;}else if(day <= 0 && day > 31){printf("输入的日期无效!\n");return 0;}else{switch(month){case 4:case 6:case 9:case 11:if(day > 30){printf("输入的日期无效!\n");return 0;}break;case 2:if((year%4 == 0 && year%100 != 0) || year%400 == 0) {if(day > 29){printf("输入的日期无效!\n");return 0;}}elseif(day > 28){printf("输入的出生日期无效!\n"); return 0;}}break;}/* end of switch(m0)*/}return 1;}void main(){int y0, m0, d0; /* 生日*/int y1, m1, d1; /* 当前日期*/int years, 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;}else if(y0 > y1){printf("出生年份比当前年份晚!\n"); return;}else if(y0 == y1){if(m0 > m1){printf("出生年月比当前年月晚!\n"); return;}else if(m0 == m1)if(d0 > d1){printf("出生年月日比当前年月日晚!\n");return;}}}}/* 计算实足年龄*/years = y1 - y0;months = m1 - m0;days = d1 - d0;/* 修正实足年龄天数*/if(days < 0){months--;switch(m1){case 1:case 5:case 7:case 10:case 12:days += 30;break;case 2:case 4:case 6:case 8:case 9:case 11:days += 31;break;case 3:if((y1%4 == 0 && y1%100 != 0) || y1%400 == 0) {days += 29;}else{days += 28;break;}/* end of switch(m1) */}/* end of if(days < 0) *//* 修正实足年龄月数*/if(months < 0){months += 12;years--;}/* end of if(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#includevoid main(){int a;printf ("请输入一个整数:");scanf ("%d",&a);if (a%2==0){printf ("%d 是偶数\n", a);}else{printf ("%d 是奇数\n", a);}}4.24#includevoid main(){int a,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语句#includevoid main(){int x,y;printf("input x :");scanf("%d",&x);if ( x>-5 && x<0 ){printf("y is %d\n",y=x);}if ( x==0 ){printf("y is %d\n",y=x-1);}if ( x>0 && x<10 ){printf("y is %d\n",y=x+1);}if ( x>=10 || x<=-5){printf("error\n");}}(2)嵌套的if语句#includevoid main(){int x,y;printf("input x :");scanf("%d",&x);printf("\n");if(x < 0){if(x > -5){printf("y is %d.\n",y=x); }else{printf("error!\n");}}if(0 == x){printf("y is %d.\n",y=x-1); }if(x > 0){if(x < 10){printf("y is %d.\n",y=x+1); }else{printf("error!\n");}}}(3)if_else语句#includevoid main(){int x,y;printf("input x :");scanf("%d",&x);if( x>-5 && x<0 ){printf("y is %d.\n",y=x); }else if( x==0 ){printf("y is %d.\n",y=x-1); }else if( x>0 && x<10 ) {printf("y is %d.\n",y=x+1); }else{printf("error!\n");}}(4)switch语句#includevoid main(){int x,y;printf("input x : ");scanf("%d",&x);switch (x){case -4:case -3:case -2:case -1:printf("y is %d.\n",y=x); break;case 0:printf("y is %d.\n",y=x-1); break;case 1:case 2:case 3:case 4:case 5:case 6:case 7:case 8:case 9:printf("y is %d.\n",y=x+1); break;default:printf("error!\n");}}第五章一. 选择题5.1 D 5.2 C 5.3 B 5.4 C 5.5 C 5.6 B 5.7 D 5.8 A 5.9 D 5.10 D二. 填空题5.11 5 4 65.12 死循环5.13 -15.14 115.15 d=1.0 k++ k<=n5.16 x>=0 x三. 编程题5.17#includevoid main(){int i;int sig = 1;int sum = 0;for(i=1; i<=101; i++,i++){sum += sig*i;sig *= -1;}printf("sum=%d\n", sum);}5.18(1)#includevoid main(){int i;double m=1.0;double e = 1.0;for(i=1; i<50; i++){m *= i;e += 1/m;}printf("e=%f\n",e);}(2)#includevoid main(){int i=1;double m=1.0;double e = 1.0;while(1/m >= 0.0004){m *= i;e += 1/m;i++;}printf("e=%f\n",e);}5.19#includevoid main(){int year;int col = 0;for(year=1600; year<=2000; year++){if((year%4 == 0 && year%100 != 0) || year%400 == 0) {printf("%d\t", year);col++;if(col%5 == 0){printf("\n");}}}printf("\n");}5.20#include#define N 7void main(){int i;int j;int m;int k = 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.1 B 6.2 D 6.3 A 6.4 A 6.5 B 6.6 D 6.7 D 6.8 B 6.9 A 6.10 A 6.11 C二. 填空题6.12 -16.13 16.14 ctype.h6.15 16.16 10A 20B 30C 40D6.177.29 101.298AB6.18 A7.29B101.2986.19 A B C (每个字符后有三个空格)三. 编程题6.20#include#define N 80void main(){int iLoop = 0;gets(str);while(str[iLoop]){printf("%c-%d\t", str[iLoop],str[iLoop]); iLoop++;if(iLoop%3 == 0){printf("\n");}}printf("\n");}6.21#include#define N 80void main(){char str[N];int num = 0;int iLoop = 0;gets(str);while(str[iLoop]){if(str[iLoop] >= '0' && str[iLoop] <= '9') {num = 10*num + (str[iLoop] - '0');}iLoop++;}printf("%d\n",num);}6.22#include#include#define N 80void main(){char str[N];do{gets(str);num++;}while(strcmp(str, "EOF"));printf("您输入了%d行字符!\n",num);}6.23#include#define N 80void main(){char str[N];int iLoop = 0;int num = 0;gets(str);while(str[iLoop] && iLoop < N){if(str[iLoop] >= 'a' && str[iLoop] <= 'z'){num++;}iLoop++;}printf("您输入了字符中有%d个小写字母!\n",num); }6.24#includevoid main(){int line;int iLoop1;int iLoop2;printf("请输入图案的行数(不大于26):");scanf("%d", &line);for(iLoop1 = 0; iLoop1 < line; iLoop1++){for(iLoop2 = 0; iLoop2 < line - iLoop1; iLoop2++){printf(" ");}for(iLoop2 = 0; iLoop2 < 2*iLoop1+1; iLoop2++) {printf("%c",iLoop1 + 'A');}printf("\n");}}第七章一. 选择题7.1 C 7.2 C 7.3 B 7.4 C 7.5 A 7.6 D 7.7 A二. 填空题7.8 127.9 9.7.10 47.11 n=1 s7.12 <=y z*x7.13 1 s*i 0 f(k)三. 程序调试和编程题7.14fun(int n){ int k,yes;for(k=2; k<=n/2; k++){if(n%k == 0) { yes = 0; break;}else yes = 1;}return yes;}7.15int mymod(int a, int b){return a%b;}7.16double fun(int n){double sum = 0;int iLoop;int sig = -1;for(iLoop=1; iLoop<=n; iLoop++){sig *= -1;sum += sig*1.0/iLoop;}return sum;}7.17double fun(int n){double t = 1.0;int iLoop;long tmp;for(iLoop=2; iLoop<=n; iLoop++){tmp = iLoop*iLoop;t -= 1.0/tmp;}return t;}7.18#include#includedouble fun(double x){return x*x + 5*x + 4;}void main(){int x = 2;printf("y1=%f\n", fun(x));printf("y2=%f\n", fun(x+15));printf("y3=%f\n", fun(sin(x)));}第八章一. 选择题8.1 A 8.2 B 8.3 B 8.4 C 8.5 B 8.6 B 8.7 C 8.8 D 8.9 B 8.10 C 8.11 C 8.12 C8.13 1108.14 7 18.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)10 20 30 40 50三. 编程题8.17void fun(double x, double y, double *sum, double *div){*sum = x + y;*div = x - y;return;}8.18void fun(double x, double y, double z, double *max, double *min){*max = x;*min = x;if(*max < y){*max = y;}if(*max < z){*max = z;}if(*min > y){*min = y;}if(*min > z){*min = z;}return;}第九章一. 选择题9.1 D 9.2 A 9.3 A 9.4 C 9.5 C 9.6 A 9.7 B 9.8 D 9.9 C 9.10 C9.11 C 9.12 D 9.13 D 9.14 A 9.15 A 9.16 A 9.17 C 9.18 C二. 填空题9.20 69.21 129.22 39.23 27219.24 -850,2,09.25 k=p k9.26 (c=getchar()) c-'A'三. 编程题9.27#include#define N 81int main(){int counter[10] = {0};int iLoop = 0;char str[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 - %d\n", iLoop, counter[iLoop]); }return 0;}9.28void fun(int array[], int arraysize, int start) {int iLoop;if(start < arraysize-1){if(start <=0){start = 1;}for(iLoop = start; iLoop < arraysize; iLoop++){array[iLoop-1] = array[iLoop];}}for(iLoop = 0; iLoop < arraysize; iLoop++){printf("No.%d = %d\n", iLoop, array[iLoop]);}}9.29int fun(int arry1[], int arry2[], int arrysize){int iLoop;int counter = 0;for(iLoop = 0; iLoop < arrysize; iLoop++){if(arry1[iLoop] % 2){arry2[counter++] = arry1[iLoop];}}return counter;}9.30void fun(char array[], int arraysize){int iLoop1;int iLoop2;char temp;/* 冒泡排序*/for(iLoop1 = 0; iLoop1 < arraysize - 1; iLoop1++){for(iLoop2 = 0; iLoop2 < arraysize - 1 - iLoop1; iLoop2++) {if(array[iLoop2] < array[iLoop2 + 1]){temp = array[iLoop2];array[iLoop2] = array[iLoop2 + 1];array[iLoop2 + 1] = temp;}}}}#includevoid fun(int array[], int arraysize, int inertNumber) {int iLoop;int iLoop2;if(array[0] < array[arraysize-1]){for(iLoop = 0; iLoop< arraysize; iLoop++){if(array[iLoop] > inertNumber){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< arraysize; iLoop++){if(array[iLoop] < inertNumber){for(iLoop2 = arraysize - 1; iLoop2 >= iLoop; iLoop2--) {array[iLoop2 + 1] = array[iLoop2];}array[iLoop] = inertNumber;break;}}if(iLoop >= arraysize){array[arraysize] = inertNumber;}}int main(){int iLoop;int a[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");return 0;}9.32int fun(int number, int array[]) {int iLoop = 0;int iLoop2;int binLen;int midNumber;int div;int remain;midNumber = number;dodiv = midNumber/2;remain = midNumber%2;midNumber = div;array[iLoop++] = remain;}while(midNumber);binLen = iLoop;for(iLoop2 = 0, iLoop = binLen - 1; iLoop2 < iLoop; iLoop2++, iLoop--) {midNumber = array[iLoop2];array[iLoop2] = array[iLoop];array[iLoop] = midNumber;}return binLen;}9.33#include#include#define N 15void fun(int array[], int arraysize){int x;int iLoop;int iLoop2;for(iLoop = 0; iLoop < arraysize; iLoop++){iLoop2 = 0;x = rand()%20;do{if(x == array[iLoop2] && iLoop > 0){x = rand()%20;iLoop2 = 0;}iLoop2++;}while(iLoop2 < iLoop);array[iLoop] = x;}}int main(){int a[N];int iLoop;fun(a, N);for(iLoop = 0; iLoop < N; iLoop++){printf("%d\n", a[iLoop]);}return 0;}第十章一. 选择题10.1 C 10.2 B 10.3 C 10.4 B 10.5 C 10.6 A 10.7 C 10.8 A 10.9 C 10.10 C二. 填空题10.11 GFEDCB10.12 XYZ10.13 SO10.14 1010.15 Itis10.16 strlen(str)-1 j--10.17 310.18 goodgood!三. 编程题10.19char* mygets(char *str){int iLoop = 0;char ch;while((ch=getchar()) != '\n'){str[iLoop++] = ch;}str[iLoop] = '\0';return str;}char * myputs(char *str)int iLoop = 0;while(str[iLoop]){putchar(str[iLoop++]);}putchar('\n');return str;}10.20#include#includeint fun(char *str){int len;int iLoop1;int iLoop2;int result = 1;len = strlen(str);for(iLoop1 = 0, iLoop2 = len - 1; iLoop1 < iLoop2; iLoop1++, iLoop2--) {if(str[iLoop1] != str[iLoop2]){result = 0;break;}}return result;}int main(){char a[20] = "ABCDCBA";char b[20] = "ABCDEBA";printf("%d\n", fun(a));printf("%d\n", fun(b));return 0;}10.21char fun(char *str, int pos){int len;int iLoop;char ch;len = strlen(str);if(pos > len){return NULL;}ch = str[pos];for(iLoop = pos; iLoop < len - 1; iLoop++) {str[iLoop] = str[iLoop + 1];}str[len-1] = '\0';return ch;}第十一章一. 选择题11.1 D 11.2 B 11.3 A 11.4 C二. 填空题11.5 IJKLEFGHABCD11.6 711.7 811.8 *(s+j) i+1 i11.9 1711.10 (*fun)() (*fun)(a+i*h)/h mypoly三. 编程题11.11#include#include#define N 81int main(int argc, char **argv){char sig;int dig;int pos;char str[N] = {'\0'};char outStr[N] = {'\0'};if(argc < 2){sig = '-';dig = 10;}else{sig = argv[1][0];dig = argv[1][1] - '0';}printf("请输入一个字符串:"); gets(str);if(sig == '-'){pos = strlen(str) - dig;if(pos <= 0){pos = 0;}strcpy(outStr, str + pos);}else if(sig == '+'){strcpy(outStr, str);pos = strlen(outStr);if(pos > dig){pos = dig;}outStr[pos] = '\0';}printf("处理后的字串为:"); printf("%s\n", outStr);return 0;}11.12#include#includevoid movebin(char *bin){int len;int iLoop;len = strlen(bin);for(iLoop = len; iLoop > 0; iLoop--) {bin[iLoop] = bin[iLoop - 1];}return;}void fun(int n, char *bin){int pos;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;}int main(){int a = 4;char bin[50] = {""};fun(a, bin);printf("%s\n", bin);return 0;}11.13long fun(int n){if(n == 1){return n;}else{return fun(n-1) + n;}}int main(){int num;int sum;printf("请输入一个自然数:"); scanf("%d", &num);sum = fun(num);printf("结果是:%d\n", sum);return 0;}11.14#includeint fun(int n){if(n == 0 || n == 1){return 1;}else{return fun(n-1) + fun(n-2); }}int main(){int result;printf("请输入一个自然数:");scanf("%d", &num);result = fun(num);printf("斐波拉契级数为:%d\n", result);return 0;}第十二章一. 选择题12.1 B 12.2 B 12.3 A 12.4 C 12.5 D 12.6 B 12.7 A 12.8 A二. 填空题12.9 2,5,1,2,3,-212.10 2468第十三章一. 选择题13.1 A 13.2 C 13.3 B 13.4 C 13.5 D 13.6 D 13.7 D二. 填空题13.8 ar=9 ar=9 ar=1113.9 int* s *b三. 编程题13.10#define MYALPHA(C) ((C>='A' && C<='Z') || (C>='a' && C<='z')) ? 1 : 0 13.11#define SWAP(t,x,y) {t tmp; tmp=x; x=y; y=tmp;}13.12#include#includeint main(){int *p;int tmp;int iLoop;int iLoop2;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 %d\n", *p, *(p+1), *(p+2));free(p);p = NULL;return 0;}第十四章一. 选择题14.1 D 14.2 D 14.3 D 14.4 A 14.5 C 14.6 C 14.7 C 14.8 B二. 填空题14.9 struct link *next14.10 p->next m>p->data14.11 (struct list*) struct list struct list* struct list return h三. 编程题14.12#include#define N 3struct stud{char num[5], name[10];int s[4];double ave;};void readrec(struct stud array[], int size)int iLoop;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;}void writerec(struct stud array[], int size){int iLoop;for(iLoop=0; iLoop{printf("%s\t%s\t%d\t%d\t%d\t%d\t%f\n",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;}int main(){struct stud stu[N];readrec(stu, N);writerec(stu, N);return 0;}14.13#include#include#define N 100struct node{int data;struct node* next;int seekMaxValue(struct node *pNode){int max;struct node* pMove;pMove = pNode;max = pMove->data;pMove = pMove->next;while(pMove){if(max < pMove->data){max = pMove->data;}pMove = pMove->next;}return max;}struct node* seekMaxValueAddress(struct node *pNode) {int max;struct node* maxAddress;struct node* pMove;pMove = pNode;max = pMove->data;maxAddress = pMove;pMove = pMove->next;while(pMove){if(max < pMove->data){max = pMove->data;maxAddress = pMove;}pMove = pMove->next;}return maxAddress;}int main()struct node* head;struct node* pNode;int iLoop;head = (struct node*)malloc(sizeof(struct node)); pNode = head;pNode->next = NULL;for(iLoop=0; iLoop {pNode->next = (struct node*)malloc(sizeof(struct node)); pNode = pNode->next;pNode->next = NULL;pNode->data = iLoop;}printf("%d\n", seekMaxValue(head->next));printf("%d\n", seekMaxValueAddress(head->next));return 0;}第十五章一. 选择题15.1 D 15.2 A 15.3 B 15.4 A二. 填空题15.515.6 a^a15.7 a|0xffff15.8 x|0xff0015.9 a=(>>2)15.10 ch|0x20第十六章一. 选择题16.1 B 16.2 C二. 填空题16.3 3 !feof(f1) f2 fclose(f1) fclose(f2)16.4 fopen(fname,"w") ch16.5 "r" !feof(fp) fgetc(fp)16.6 CCCCBBBBAAAA三. 编程题16.7#include#define N 10#define LEN 81int main(){char *str[N] = {"AAAAAAAAA", "BBBBBBBBB", "CCCCCCCCC", "DDDDDDDDD", "EEEEEEEEE", "FFFFFFFFF", "GGGGGGGGG", "HHHHHHHHH","IIIIIIIII","JJJJJJJJJ"};char str2[N][LEN];FILE *fp;int iLoop;fp = fopen("str.txt", "w");if(fp == NULL){printf("创建文件失败!\n"); return 1;}else{for(iLoop = 0; iLoop < N; iLoop++) {fputs(str[iLoop], fp);fputs("\n",fp);}}fclose(fp);fp = fopen("str.txt", "r");if(fp == NULL){printf("打开文件失败!\n"); return 1;}else{for(iLoop = 0; iLoop < N; iLoop++) {fgets(str2[iLoop], LEN - 1, fp);}}fclose(fp);for(iLoop = 0; iLoop < N; iLoop++) {printf("%s", str2[iLoop]);}return 0;}16.8#include#define N 10int main(){float num;int iLoop;FILE *fp;fp = fopen("num.bin", "wb+");if(fp == NULL){printf("创建文件失败!\n"); return 1;}/* 从键盘读入10个数并写文件*/ printf("请输入%d个数:", N);for(iLoop = 0; iLoop < N; iLoop++) {scanf("%f", &num);fwrite(&num, sizeof(num), 1, fp); }/* 文件指针回到开始处*/ rewind(fp);/* 从文件读出10个数并显示*/ for(iLoop = 0; iLoop < N; iLoop++) {fread(&num, sizeof(num), 1, fp); printf("%f\n", num);}/* 移文件指针到第四个数开始处*/ fseek(fp, 3L*sizeof(num), SEEK_SET); /* 读入一个新数据*/printf("请输入一个新数据:");scanf("%f", &num);fwrite(&num, sizeof(num), 1, fp);/* 文件指针回到开始处*/rewind(fp);/* 从文件读出10个数并显示*/for(iLoop = 0; iLoop < N; iLoop++) {fread(&num, sizeof(num), 1, fp); printf("%f\n", num);}/* 关闭文件*/fclose(fp);return 0;}。
全国计算机二级c语言课后练习及参考答案试卷1

一单项选择题1.逻辑运算符两侧运算对象的数据类型是〔D 〕。
A〕只能是0或1B〕只能是0或非0正数C〕只能是整型或字符型数据D〕可以是任何类型的数据2.x=43,ch='A',y=0;那么表达式〔x>=y&&ch<'B'&&!y〕的值是〔C 〕。
A〕0B〕语法错C〕1D〕“假〞3. int x=10,y=20,z=30;以下语句执行后x,y,z的值是〔B 〕。
y〕z=x;x=y;y=z;A〕x=10,y=20,z=30B〕x=20,y=30,z=30C〕x=20,y=30,z=10D〕x=20,y=30,z=204.执行以下语句后a的值为〔 C 〕,b的值为〔〕。
int a,b,c;a=b=c=1;++a|| ++b && ++c;A〕错误 1B〕 2 2C〕2 1D〕 1 1当A的值为奇数时,表达式的值为“真〞,A的值为偶数时,表达式的值为“假〞,那么以下不能满足要求的表达式是〔C 〕。
A〕A%2==1B〕!〔A%2==0〕C〕!〔A%2〕D〕A%26.设有:int a=1,b=2,c=3,d=4,m=2,n=2;执行〔m=a>D〕后n的值是〔B 〕。
A〕0B〕 2C〕3D〕 4断char型变量cl是否为小写字母的正确表达式是〔 D 〕。
A〕‘a’<=cl<=‘z’=a〕&&〔cl<=z〕C〕〔‘a’>=cl〕||〔‘z’<=cl〕=‘a’〕&&〔cl<=‘z’〕语句形式是〔C 〕。
y&&x!=y〕;B〕if〔x==y〕x+=y;C〕if〔x!=y〕scanf〔“%d〞,&x〕else scanf〔“%d〞,&y〕D〕if〔x<y〕{x++;y++;}9.请阅读以下程序:main〔〕{int a=5,b=0,c=0;if〔a=b+C〕printf〔“***\n〞〕;else printf〔“$$$\n〞〕;}以上程序〔D 〕。
《全国计算机等级考试二级教程——C语言程序设计》课后习题详解[1]
![《全国计算机等级考试二级教程——C语言程序设计》课后习题详解[1]](https://img.taocdn.com/s3/m/aa789992b52acfc788ebc954.png)
《全国计算机等级考试二级教程——C语言程序设计》习题分析与解答第一章程序设计基本概念习题分析与解答1.1 【参考答案】.EXE1.2 【参考答案】[1].C [2].OBJ [3].EXE1.3 【参考答案】[1]顺序结构[2]选择结构[3]循环结构第二章C程序设计的初步知识习题分析与解答一、选择题2.1 【参考答案】B)2.2 【参考答案】D)2.3 【参考答案】B)2.4 【参考答案】A)2.5 【参考答案】C)2.6 【参考答案】A)2.7 【参考答案】B)2.8 【参考答案】B)2.9 【参考答案】D)2.10 【参考答案】C)2.11 【参考答案】B)2.12 【参考答案】B)2.13 【参考答案】A)二、填空题2.14 【参考答案】[1]11 [2]122.15 【参考答案】[1]4.2 [2]4.22.16 【参考答案】[1]{ [2]} [3]定义[4]执行2.17 【参考答案】[1]关键字[2]用户标识符2.18 【参考答案】[1]int [2]float [3]double2.19 【参考答案】float a1=1.0, a2=1.0;或float a1=1, a2=1;(系统将自动把1转换为1.0)2.20 【参考答案】存储单元2.21 【参考答案】 3.52.22 【参考答案】[1]a*b/c [2]a/c*b [3]b/c*a2.23 【参考答案】把10赋给变量s2.24 【参考答案】[1]位[2]1位二进制数据(0或1)2.25 【参考答案】[1]8 [2]127 [3]01111111 [4]-128 [ 5 ] 10000000 2.26 【参考答案】[1]32767 [2]-32768 [3]100002.27 【参考答案】[1]十[2]八[3]十六三、上机改错题2.28 【分析与解答】第1行的错误:(1) include是一个程序行,因此在此行的最后不应当有分号(;)。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
《全国计算机等级考试二级教程——C语言程序设计》课后题及参考答案(教育部考试中心)高等教育出版社出版(第一章、第二章、第三章)第一章程序设计基本概念1.1 在TRUBO C环境中用RUN命令运行一个C程序时,所运行的程序的后缀是______。
答案:.exe1.2 C语言源程序文件的后缀是_____,经过编译后,生成文件的后缀是_____,经过连接后,生成文件的后缀是_____。
答案:.c .obj .exe1.3 结构化程序由_____、_____、_____三种基本结构组成。
答案:顺序、选择、循环第二章 C程序设计的初步知识一、选择题2.1 以下选项中正确的整型常量是A)12. B)-20 C)1,000 D)4 5 6答案:B2.2 以下选项中正确的实型常量是A)0 B)3. 1415 C)0.329×10(2)(10的2次方)D).871答案:D2.3 以下选项中不正确的实型常量是A)2.607E-1 B)0.8103e 2 C)-77.77 D)456e-2答案:B2.4 以下选项中不合法的用户标识符是A)abc.c B)file C)Main D)PRINTF答案:A2.5 以下选项中不合法的用户标识符是A)_123 B)printf C)A$ D)Dim答案:C2.6 C语言中运算对象必需是整型的运算符是A)% B)/ C)! D)**答案:A2.7 可在C程序中用作用户标识符的一组标识符是A)void define WORDB)as_b3 _123 IfC)For -abc caseD)2c DO SIG答案:B2.8 若变量已正确定义并赋值,符合C语言语法的表达式是A)a=a+7; B)a=7+b+c,a++ C)int(12.3%4) D)a=a+7=c+b答案:B2.9 以下叙述中正确的是A)a是实型变量,C允许进行以下赋值a=10,因此可以这样说:实型变量中允许存放整型值。
B)在赋值表达式中,赋值号右边既可以是变量也可以是任意表达式。
C)执行表达式a=b后,在内存中a和b存储单元中的原有值都将被改变,a的值已由原值改变为b的值,b的值由原值变为0。
D)已有a=3,b=5。
当执行了表达式a=b,b=a之后,已使a中的值为5,b中的值为3。
答案:B2.10 以下叙述中正确的是A)在C程序中无论是整数还是实数,只要在允许的范围内都能准确无误的表示。
B)C程序由主函数组成。
C)C程序由函数组成。
D)C程序由函数和过程组成。
答案:C2.11 TURBO C中int类型变量所占字节数是A)1 B)2 C)3 D)4答案:B2.12 不合法的八进制数是A)0 B)028 C)077 D)01答案:B2.13 不合法的十六进制数是A)oxff B)0Xabc C)0x11 D)0x19答案:A二、填空题2.14 若k为int整型变量且赋值11。
请写出运算k++后表达式的值A)_____和变量的值B)_____。
答案:11、122.15 若x为double型变量,请写出运算x=3.2,++x后表达式的值A)_____和变量的值B)_____。
答案:4.2、4.22.16 函数体由符号____开始,用符号____结束。
函数体的前面是____部分,其后是____部分。
答案:{、}、定义、执行2.17 C语言中的标识符可分为____、____、和预定义标识符三类。
答案:关键字、用户标识符2.18 在C语言程序中,用关键字____定义基本整型量,用关键字____定义单精度实型变量,用关键字___定义双精度实型变量。
答案:int、float、double2.19 把a1、a2定义成单精度实型变量,并赋初值1的定义语句是____。
答案:float a1=1.0,a2=1.0;2.20 C程序中定义的变量,代表内存中的一个____。
答案:存储单元2.21 表达式3.5+1/2的计算结果是____。
答案:3.52.22 对以下数学式,写出三个等价的C语言表达式____、____、____。
a×b——c答案:a*b/c、a/c*b、b/c*a2.23 表达式s=10应当读做“____”。
答案:将10赋给变量s2.24 计算机中内存储器的最小存储单位称____,其中能容纳的数是____。
答案:位、0或12.25 通常一个字节包含____个二进制位。
在一个字节中能存放的最大(十进制)整数是____,它的二进制数的形式是____;最小(十进制)整数是____,它的二进制数的形式是____。
答案:8、255、1111 1111、-128、1000 00002.26 当计算机用两个字节存放一个整数时,其中能存放的最大(十进制)整数是____、最小(十进制)整数是____,它的二进制数的形式是____。
答案:65535、-32768、1000 0000 0000 00002.27 在C语言中整数可用____进制数、____进制数和____进制数三种数制表示。
答案:十、八、十六第三章、顺序结构一、选择题3.1 若a、b、c、d都是int类型变量且初值为0,以下选项中不正确的赋值语句是A) a=b=c=100; B) d++; C) c+b; D) d=(c=22)-(b++);答案:C3.2 以下选项中不是C语句的是A) {int i; i++; printf("%d\\n", i); }B) ; C) a=5,c=10 D) { ; }答案:C3.3 以下合法的C语言赋值语句是A) A=B=58 B) k=int(a+b); C) a=58,b=58 D) --i;答案:D3.4 以下程序的输出结果是A) 0 B) 1 C) 3 D)不确定的值main(){int x=10,y=3;printf("%d\\n", y=x/y);}答案:C3.5 若变量已正确说明为int类型,要给a、b、c输入数据,以下不正确的输入语句是A) read(a,b,c); B) scanf("%d%d%d",a,b,c);C) scanf("%D%D%D",&a,&b,&c); D) scanf("%d%d%d",&a,&b,&c);答案:D3.6 若变量已正确说明为float类型,要通过以下赋值语句给a赋予10、b赋予22、c赋予33,以下不正确的输入形式是A) 10<CR> 22<CR> 33<CR> B) 10.0,22.0,33.0 C) 10.0<CR> 22.0 33.0 D) 10 22<CR> 33scanf("%f %f %f", &a, &b, &c);答案:B3.7 若变量以正确定义,要将a和b中的数进行交换,下面不正确的语句组是A) a=a+b,b=a-b,a=a-b; B) t=a,a=b,b=t;C) a=t; t=b; b=a; D) t=b; b=a; a=t;答案:C3.8 若变量已正确定义,以下程序段的输出结果是A) 输出格式说明与输出项不匹配,输出无定值B) 5.17000 C) 5.168000 D) 5.169000x=5.16894;printf("%f\\n",(int)(x*1000+0.5)/(float)1000);答案:D3.9 若有以下程序段,c3中的值是A) 0 B) 1/2 C) 0.5 D) 1int c1=1,c2=2,c3;c3=c1/c2;答案:A3.10 若有以下程序段,其输出结果是A) 0,0,-10 B) 0,0,5 C) -10,3,-10 D) 3,3,-10int a=0,b=0,c=0;c=(a-=a-5),(a=b,b+3);printf("%d,%d,%d\\n",a,b,c);答案:B3.11 当运行以下程序时,在键盘上从第一列开始输入9876543210<CR>(此处<CR>代表Enter),则程序的输出结果是A) a=98,b=765,c=4321 B) a=10,b=432,c=8765C) a=98,b=765.000000,c=4321.000000 D) a=98,b=765,c=4321.0main(){int a; float b,c;scanf("%2d%3f%4f",&a,&b,&c);printf("\\na=%d,b=%f,c=%f\\n",a,b,c);}答案:C3.12 以下程序的输出结果是A) a=%2,b=%5 B) a=2,b=5 C) a=%%d,b=%%d D) a=%d,b=%dmain(){ int a=2,b=5;printf("a=%%d,b=%%d\\n",a,b);}答案:D3.13 若int类型占两个字节,则以下程序段的输出是A) -1,-1 B) -1,32767 C) -1,32768 D) -1,65535int a=-1;printf("%d,%u\\n",a,a);答案:D3.14 以下程序段的输出是A) *496 * B) * 496* C) *000496* D)输出格式符不合法int x=496;printf("*%-06d*\\n",x);答案:A3.15 以下程序段的输出是A) |3.1415| B) | 3.0| C) | 3| D) | 3.|float a=3.1415;printf("|%6.0f|\\n",a);答案:C3.16 以下程序段的输出是A) |2345.67800| B) |12345.6780| C) |12345.67800| D) |12345.678|printf("|%10.5f|\\n",12345.678);答案:C3.17 以下程序段的输出是A) *0000057.66* B) * 57.66* C) *0000057.67* D) * 57.67*float a=57.666;printf("*%010.2f*\\n",a);答案:C3.18 若从终端输入以下数据,要给变量C赋以283.19,则正确的输入语句是A) scanf("%f",c); B) scanf("%8.4f",&c);C) scanf("%6.2f",&c); D) scanf("%8",&c);283.1900<CR> <CR>表示Enter键答案:由于印刷错误,本题没有正确答案。