计算机二级C语言教程第10章课后编程题
第10章C语言程序设计习题答案

C 语言程序设计( Visual C++6.0 环境)》习题答案习题十、思考题1.简述公有类型成员与私有类型成员的区别。
公有(public) 类型成员不但可以被类的成员函数访问,而且可以被外界访问,所以说公有类型定义了类的外部接口。
私有(private) 类型成员只能被类的成员函数访问,外界不能直接访问它。
类的数据成员一般都应该声明为私有成员。
2.简述构造函数与析构函数的作用。
构造函数的作用就是在对象在被创建时利用特定的值构造对象,将对象初始化。
析构函数的作用与构造函数正好相反,它是用来在对象被删除前进行一些清理工作。
析构函数调用之后,对象被撤消了,相应的内存空间也将被释放。
3.简述什么是友元函数。
友元函数是在类定义中由关键字friend 修饰的非成员函数。
友元函数可以是一个普通函数,也可以其它类中的一个成员函数,它不是本类的成员函数,但它可以访问本类的私有成员和保护成员。
4.简述公有继承、私有继承和保护继承三种继承方式的区别。
⑴、当类的继承方式为公有(public 继承)时,基类的公有(public )成员和保护( protected )成员仍然成为派生类的公有成员和保护成员,而基类的私有成员不能被派生类访问。
⑵、当类的继承方式为保护( protected )继承时,基类的公有(public )成员和保护( protected )成员将成为派生类的保护成员,而基类的私有成员不能被派生类访问。
⑶、当类的继承方式为私有(private )继承时,基类的公有(public )成员和保护(protected )成员将成为派生类的私有成员,而基类的私有成员不能被派生类访问。
5.定义一个圆柱体类,其属性为圆柱体的底面半径和高,能计算出圆柱体的体积。
#include<iostream.h>class cylinder{public:cylinder(float r,float h){radius=r;height=h;}float Volume();private:float radius;float height;};float cylinder::Volume(){return 3.14*radius*radius*height;}void main(){float r,h;cout<<" 请输入圆柱体的底面半径和高:"; cin>>r>>h;cylinder x(r,h);cout<<x.Volume()<<endl;}6.从第 5 题中定义的圆柱体类中派生出圆锥类,覆盖计算体积的成员函数。
C程序设计教程与实验第10章习题参考答案

}
5.
#include<stdio.h>
struct student
{
char num[10];
char name[8];
int score[2];
float ave;
} stu[3];
void main()
{
int i,j,sum;
FILE *fp;
for(i=0;i<3;i++)
{
printf("\ninput score of student %d:\n",i+1);
stu[i].score[1],stu[i].score[2],stu[i].ave);
}
}
她含着笑,切着冰屑悉索的萝卜,
她含着笑,用手掏着猪吃的麦糟,
她含着笑,扇着炖肉的炉子的火,
她含着笑,背了团箕到广场上去
晒好那些大豆和小麦,
大堰河,为了生活,
在她流尽了她的乳液之后,
她就用抱过我的两臂,劳动了。
同着数不尽的奴隶的凄苦,
同着四块钱的棺材和几束稻草,
同着几尺长方的埋棺材的土地,
同着一手把的纸钱的灰,
大堰河,她含泪的去了。
这是大堰河所不知道的:
她的醉酒的丈夫已死去,
大儿做了土匪,
第二个死在炮火的烟里,
第三,第四,第五
而我,我是在写着给予这不公道的世界的咒语。
当我经了长长的飘泊回到故土时,
在山腰里,田野上,
printf("No.:");
scanf("%s",stu[i].num);
printf("name:");
二级C语言课后编程答案的整理版

3.16main(){ float a,b,c,s,v;printf("input a,b,c:");scanf("%f %f %f:",&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.17main(){ int h,m;h=560/60;m=560%60;3.18main(){ int m,n;printf("input m & n:");scanf("%d%d",&m,&n);printf("\n%d,%d\n",m/n,m%n);getch();}3.19main(){ double x,y,z,s;printf("input x,y,z: ");scanf("%lf%lf%lf",&x,&y,&z);s=(x+y+z)/3.0;printf("\nAverage=%6.1lf\n",s);getch();}3.20main(){ int a,b,c,t;printf("Input a,b,c: ");scanf("%d%d%d",&a,&b,&c);t=c;c=b;b=a;a=t;printf("\na,b,c=%d,%d,%d\n",a,b,c); getch();}【4-21】main(){ int a,m;printf("input a: ");scanf("%d",&a);switch(a/10){ case 0:case 1:case 2: m=1 ;break;case 3: m=2 ;break;case 4: m=3 ;break;case 5: m=4 ;break;default: m=5;}printf("a,m=%d, %d",a,m );getch();}【4-22】main(){ int age,y0,m0,d0,y1,m1,d1;printf("\ninput a stedent\' birthday (yy-mm-dd): ");scanf("%d-%d-%d",&y0,&m0,&d0);printf("\ninput today\' date(yy-mm-dd): ");scanf("%d-%d-%d",&y1,&m1,&d1);if ((m1>m0)||(m1==m0)&&(d1>=d0)) age=y1-y0;else age=y1-y0-1;printf("\nThe student\' ageis %d",age);getch();}【4-23】main(){ int m;printf("\ninput a integer: ");scanf("%d",&m);if (m%2==0) printf("\n%d is event.",m);else printf("\n%d is ord.",m);getch();}【4-24】main(){ int a,b,c,max;printf("\ninput a,b,c: ");scanf("%d%d%d",&a,&b,&c);max=a;if (b>max) max=b;if (c>max) max=c;printf("max is %d",max);getch();}【4-25-1】main(){ int x,y;printf("\ninput x: ");scanf("%d",&x);if((x>-5)&&(x<0)) y=x;if (x==0) y=x-1;if ((x>0)&&(x<10)) y=x+1;printf("\nx is %d , y is %d",x,y); getch();}【4-25-2】main(){ int x,y;printf("\ninput x: ");scanf("%d",&x);if((x>-5)&&(x<10)){ if (x<0) y=x;if (x==0) y=x-1;if (x>0) y=x+1;printf("\nx is %d , y is %d",x,y); }else printf("input x iserror ! %c",'\007');getch();}【4-25-3】main(){ int x,y;printf("\ninput x: ");scanf("%d",&x);if ((x>-5)&&(x<0)) y=x;else if (x==0) y=x-1;else if ((x>0)&&(x<10)) y=x+1; printf("\nx is %d , y is %d",x,y); getch();}【4-25-4】main(){ int x,y;printf("\ninput x: ");scanf("%d",&x);switch(x){ case -4:case -3:case -2:case -1: y=x;break;case 0 : y=x-1;break;case 1 :case 2 :case 3 :case 4 :case 5 :case 6 :case 7 :case 8 :case 9 : y=x+1;break;default : printf("Input xerror ! %c",7);}printf("\nx is %d , y is %d",x,y); getch();}【5-17】main(){ int i,s=1,k=-1;for (i=1;i<=50;i++){ s=s+k*(2*i+1);k=-k;}printf("s=%d",s);getch();}【5-18-1】main(){ int i=1;double e=1.0,s=1.0;for(i=1;i<=50;i++){s=s*i;e=e+1/s;}printf("e=%lf",e);getch();}【5-18-2】main(){ int i=1;float e=1.0,s=1.0;while (1/s>=1e-04) /* 8 times */ {s=s*i;i++;e=e+1/s;}printf("e=%10.6f",e);getch();}【5-19】main(){ int y,k=0;for(y=1000;y<=2000;y++){ if (y%4==0&&y%100!=0||y%400==0) {printf("%10d",y);k++;}if (k%3==0) printf("\n");}getch();}【5-20】#include <stdio.h>main(){int i,j,n;printf("Input n (1--10):"); do scanf("%d",&n);while (n<1||n>10);for (i=1;i<=n;i++){for (j=1;j<=40-i;j++)printf(" ");for (j=1;j<=2*i-1;j++)printf("*");printf("\n");}for (i=n+1;i<=2*n-1;i++){ for (j=1;j<=40-2*n+i;j++) printf(" ");for (j=1;j<=4*n-1-2*i;j++) printf("*");printf("\n");}getch();}【6-20】#include <stdio.h>main(){ int k=0; char ch;while((ch=getchar())!=10) { k++;printf("%4c%4d",ch,ch); if(k%3==0) printf("\n"); }getch();}【6-21】#include <stdio.h>main(){ long k=0; char ch;while((ch=getchar())!=EOF) { if (ch>='0'&&ch<='9'){ ch=ch-'0';k=k*10+ch;}}printf("%ld",k);getch();}【6-22】#include <stdio.h>main(){ int flag,k=0; char ch;while((ch=getchar())!=EOF){ if (ch==10) { k++; flag=0; }else flag=1;}if (flag==1) k++;printf("\n The line numberis %d\n",k);getch();}【6-23】#include <stdio.h>main(){ int k=0; char ch;while((ch=getchar())!=10)if (ch>='a'&&ch<='z') k++;printf("\n The lower letter number is %d\n",k);getch();}【6-24】#include <stdio.h>main(){ int i,j,n;printf("Input line number : ");scanf("%d",&n);for(i=1;i<=n;i++){ for (j=1;j<=40-i;j++)printf(" ");for(j=1;j<=2*i-1;j++)printf("%c",64+i);printf("\n");}getch();}【7-15】main(){int a,b;printf("Input a & b:"); scanf("%d%d",&a,&b);printf("\n%d%%%d=%d",a,b,mymod(a,b)); getch();}mymod(int a, int b){int z;z=a%b;return z;}【7-16】float fun(int n){return (1.0/n);}main(){int i,n,k=1;float s=0.0;printf("Input n:");scanf("%d",&n);for(i=1;i<=n;i++){s+=k*fun(i);k=-k;}printf("\ns=%8.6f",s);getch();}【7-17】float f(int m){ float t=1.0;int i;for (i=2;i<=m;i++)t-=1.0/(i*i);return t;}main(){int n;printf("Input n:");scanf("%d",&n);printf("\nt=%8.6f",f(n));getch();}【7-18】#include <math.h>float f(float x){ float z;z=x*x-5*x+4;return z;}main(){float x,y1,y2,y3;printf("Input x: ");scanf("%f",&x);y1=f(2);y2=f(x+15);y3=f(sin(x));printf("y1=%10.4f\n",y1);printf("y2=%10.4f\n",y2);printf("y3=%10.4f\n",y3);printf("\n**** END ****");getch();}【8-17-1】fun(x,y)int *x,*y;{int z1,z2;z1=*x+*y;z2=*x-*y;*x=z1; *y=z2;}main(){int *a,*b,A,B;a=&A,b=&B;printf("input two numbers: ");scanf("%d%d",a,b);printf("a,b=%d,%d\n",*a,*b);printf("before call function:\n"); printf("a=%d b=%d\n",*a,*b);fun(a,b);printf("after call function:\n"); printf("a=%d b=%d\n",*a,*b); getch();}【8-17-2】fun(x,y) float *x,*y;{float z1,z2;z1=*x+*y;z2=*x-*y;*x=z1; *y=z2;}main(){float *a,*b,A,B;a=&A;b=&B;printf("input two real numbers: "); scanf("%f%f",a,b);printf("a,b=%f, %f\n",*a,*b);printf("before call function:\n"); printf("a=%f b=%f\n",*a,*b);fun(a,b);printf("after call function:\n"); printf("a=%f b=%f\n",*a,*b);getch();}【8-18】fun(int *a,int *b,int *c){ int max,min;max=*a;min=*a;if (*b>*a) max=*b;if (*b<*a) min=*b;if (*c>max) max=*c;if (*c<min) min=*c;*a=max;*c=min;}main(){int a,b,c;printf("Input a,b,c: ");scanf("%d%d%d",&a,&b,&c);printf("before call function:\n"); printf("a=%d b=%dc=%d\n",a,b,c);fun(&a,&b,&c);printf("after call function:\n"); printf("max=%d min=%d\n",a,c);getch();}【9-27】#include <ctype.h>main(){ char *s,a[100];int i,k=0,num[10]={0};s=a;printf("Input an number string: "); scanf("%s",s);while(*s!='\0'){ if (isdigit(*s)&&(*s>'0'))num[*s-49]++;if (*s==48) num[9]++;s++;}for (i=0;i<9;i++)printf("%2d-->%3d\n",i+1,num[i]); printf(" 0-->%3d\n",num[9]);printf("***** TOTAL *****\n");for (i=0;i<=9;i++) k+=num[i];printf(" %d ",k);getch();}【9-28】move(int a[10],int n){int i;for (i=n;i<10;i++)a[i-1]=a[i];a[9]=0;}main(){int x[10],i,n;printf("input 10 number:");for(i=0;i<10;i++)scanf("%d",&x[i]);printf("\ninput the N:");scanf("%d",&n);move(x,n);printf("\nAfter move tne member list is :\n");for (i=0;i<10;i++)printf("%d ",x[i]);getch();} 【9-29】main(){int a[100]={0},b[100]={0},i,j;printf("\nInput the number list (end with 32767) :\n ");for(i=0;i<100;i++){scanf("%d",&a[i]);if ( a[i]==32767) break;}i--;odd(a,b,i);printf("\narray B :\n");for (i=0;i<100;i++)if (b[i]!=0) printf("%d ",b[i]); else break;getch();}odd(int a[100],int b[100],int n){ int i,j=0;for(i=0;i<=n;i++)if(a[i]%2!=0) b[j++]=a[i];}【9-30】#include <string.h>sort(char s[],int n){int i,j,p,t;for(j=0;j<(n-1);j++){p=j;for(i=j+1;i<n;i++)if(s[p]<s[i]) p=i;if(p!=j) { t=s[j]; s[j]=s[p];s[p]=t; }}}main(){char *s;int n;printf("\nInput s:");scanf("%s",s);n=strlen(s);sort(s,n);printf("\nthe sorted string is %s\n",s);getch();}【9-31】main(){int a[100],n,*p,i=1;a[0]=-32768; p=a+1;printf("\nInput an number list (end with 32767) :\n");do{scanf("%d",&a[i]);if (a[i]>=a[i-1]) i++;}while(a[i-1]!=32767);printf("\nInput inserted number: "); scanf("%d",&n);p=a;insert(p,n);p=a+1;printf("\nOutput array a:\n");doif (*p!=32767) printf("%d ",*p++); while (*p!=32767);getch();}insert( int *q, int n){ int *k;k=q;while (*q!=32767) q++;*(q+1)=*q;while(q>k){ if (n>*(q-1)) { *q=n; break; }else {q--; *(q+1)=*q; }}}【9-32】main(){int n,a[16]={0},*p;printf("\nInput an number:"); scanf("%d",&n);p=a;change(a,n,p);printf("n=%d\n",n);while(p<=a+15)printf("%d",*p++);}change(x,n,p)int x[16],n,*p;{p=x+15; *p=0;if(n==0) return(0);while(n!=0){*p=n%2;p--;n/=2;}}【9-33】#include <stdio.h>main(){int a[15],*p,i;p=a;frandm(a);printf("\nThe array a is: "); for(i=0;i<15;i++)printf("%d ",*p++);}frandm( a[]);{int k=0,i,x,*q;for (i=0;i<15;i++) a[i]=20; while(k<15){x=rand()%20;for (i=0;i<15;i++)if (a[i]==x ) continue;else a[k++]=x;}}【9-34】#define N 20main(){inta[N][N],x[N]={0},y[N]={0},i,j,m,sum=0 ;printf("\n Input N (<20) :");scanf("%d",&m);printf("\n Input arraya[%d][%d]:\n",m,m);for(i=0;i<m;i++)for(j=0;j<m;j++){ scanf("%d",&a[i][j]);x[i]+=a[i][j]; y[j]+=a[i][j];if (i==j ) sum+=a[i][i];}printf("\n After compute : \n");for (i=0;i<m;i++){ for (j=0;j<m;j++)printf("%5d",a[i][j]);printf("%5d\n",x[i]);}for (i=0;i<m;i++)printf("%5d",y[i]);printf("\n\nSum=%d\n",sum);getch();}【9-35】#define N 20main(){int a[N][N],b[N][N],c[N][N],m,n,i,j; printf("\n Input m,n (<20) :");scanf("%d%d",&m,&n);printf("\n Input arrayA[%d][%d]:\n",m,n);for(i=0;i<m;i++)for(j=0;j<n;j++){ scanf("%d",&a[i][j]);}printf("\n Input arrayB[%d][%d]:\n",m,n);for(i=0;i<m;i++)for(j=0;j<n;j++) { scanf("%d",&b[i][j]);c[i][j]=a[i][j]+b[i][j];}printf("\n After compute array C: \n"); for (i=0;i<m;i++){ for (j=0;j<n;j++)printf("%5d",c[i][j]);printf("\n");}getch();}【9-36】main(){int i,j,k;printf("\n ** A MULTIPLICATION TABLE **\n");printf(" ");for(i=1;i<10;i++)printf("(%3d)",i);printf("\n--------------------------------------------\n");for(i=1;i<10;i++){ for(j=0;j<10;j++)if(j==0) printf("( %d)",i);else printf("%5d",i*j);printf("\n");}printf("\n--------------------------------------------\n");getch();}【9-37】#include "stdio.h"#include "stdlib.h"main(){static int m[5][5]={0},i,j;int k=0;printf("\nBefore :\n");for (i=0;i<5;i++)for(j=0;j<5;j++){ m[i][j]=rand()%100;printf("%4d",m[i][j]);if ((++k)%5==0)printf("\n"); }printf("\nAfter:\n");for (i=0;i<5;i++)for(j=0;j<i;j++){ k=m[i][j]; m[i][j]=m[j][i]; m[j][i]=k; }k=0;for (i=0;i<5;i++)for(j=0;j<5;j++){ printf("%4d",m[i][j]);if ((++k)%5==0)printf("\n"); }printf("\n Program end ! \n");}【9-38】#include "stdio.h"main(){ int m[5][5]={0},i,j;int k=0;printf("\nBefore :\n");for (i=0;i<5;i++)for(j=0;j<5;j++){ m[i][j]=rand()%100;printf("%4d",m[i][j]);if ((++k)%5==0)printf("\n"); }printf("\nAfter:\n");for (i=0;i<5;i++)for(j=0;j<i;j++){ k=m[i][j]; m[i][j]=m[j][i]; m[j][i]=k; }printf("%4d",m[i][j]);if ((++k)%5==0)printf("\n"); }k=0;for (i=0;i<5;i++)for(j=0;j<5;j++) { printf("%4d",m[i][j]);if ((++k)%5==0)printf("\n"); }printf("\n Program end ! \n"); getch();}【10-10】#include <string.h>main(){ char str1[]="string",str2[8],*str3,*str4="string";strcpy(str1,"HELLO1");strcpy(str2,"HELLO2");strcpy(str3,"HELLO3");strcpy(str4,"HELLO4");printf("%s\n%s\n%s\n%s\n",str1,str2,s tr3,str4);getch();}【10-13】main(){char *p[]={"BOOL","OPK","H","SP"}; int i;for(i=3;i>=0;i--,i--)printf("%c",*p[i]);printf("\n");getch();}【10-19】#include <stdio.h>#include <string.h>mygets(s)char *s;{ char ch;ch=getchar();while (ch!=10){*s=ch; s++;ch=getchar();}*s='\0';}myputs(s)char *s;{ int i,n;n=strlen(s);for(i=0;i<n;i++)putchar(*s++);}main(){char *str,s[80];str=s;printf("Input a string:");mygets(str);printf("\nOutput the string:\n"); myputs(str);printf("\n");getch();}【10-20】#include <string.h>main(){ char *str ;str=(char*)malloc(1);printf("Input a string:\n" );gets(str);if (fun(str)) printf("\nThis is back-round-text\n");else printf("\nThis is not back-round-text\n");getch();}fun(char *s){int n,i,j, flag=1;n=strlen(s);for (i=0,j=n-1;i<j;i++,j--)if(*(s+i)!=*(s+j)) flag=0;return(flag);}【10-21】#include <string.h>#include <stdio.h>char deltet(s,n)char *s; int n;{ char ch; if (n>strlen(s)) {printf("\nCANNOT DELETE IT !!! %c",007);return('\0'); }else{ ch=*(s+n);do{ *(s+n)=*(s+n+1); n++; }while(*(s+n-1)!='\0');return(ch);}}main(){char str[80],*p,ch;int n;printf("\nInput a string:\n");p=str+1;gets(p);printf("\nDelete the n'th character: ");scanf("%d",&n);ch=deltet(str,n);printf("\nAfter delete thestring :(ch=%c)\n",ch);for(n=1;n<strlen(str);n++)putchar(*p++);getch();。
《全国计算机等级考试二级教程——C语言程序设计》课后题及参考答案

本文由皓月90贡献 doc文档可能在WAP端浏览体验不佳。
建议您优先选择TXT,或下载源文件到本机查看。
《全国计算机等级考试二级教程——C 语言程序设计》课后题及参考答案 目 录 第一章、第二章、第三章、第四章、第五章、第六章、第七章、第八章、第九章、 第十章、第十一章、第十二章、第十三章、第十四章、第十五章、第十六章 第一章 程序设计基本概念 1.1 在 TRUBO C 环境中用 RUN 命令运行一个 C 程序时,所运行的程序的后缀是。
答案:.exe 1.2 C 语言源程序文件的后缀是,经过编译后,生成文件的后缀是,经过连接后, 生成文件的后缀是。
答案:.c .obj .exe 1.3 结构化程序由、、三种基本结构组成。
答案:顺序、选择、循环 第二章 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,因此可以这样说:实型变量中允许存放整 型值。
计算机二级C语言辅导 10章 字符串

例2. 删除一个字符串中的一个字符。 字符串由标准输入得到,欲删除的字符也由标准输入得到
main ( ) { char s[20],c; int i,j; gets(s); c=getchar( ); for (i=j=0;s[i]!=„\0‟;i++) if (s[i]!=c) s s[j++]=s[i]; s[j]=„\0‟; c=l }
p
i j
i j
i j
i
j
i
W
0
o
1
r
2
l
3
d
4
\0
5
例2. 删除一个字符串中的一个字符。 字符串由标准输入得到,欲删除的字符也由标准输入得到
main ( ) { char s[20],c; int i,j; gets(s); c=getchar( ); for (i=j=0;s[i]!=„\0‟;i++) if (s[i]!=c) s s[j++]=s[i]; s[j]=„\0‟; c=l }
H o w \0
0 1 2 3 4 5 6 7 8 9 10 11 12
字符数组
五、字符串处理函数: 1.puts(字符数组); 将字符串输出到终端,在输出时将字符串结束标记‘\0‟转换成‘\ char str[ ]={“China\nBeijing”}; puts(str); 输出:China Beijing 2.gets(字符数组)--读入全部字符(包括空格),直到遇到回车符为止 从终端输入一个字符串到字符数 如果输入以下12个字符: 组,并且得到一个函数值,该函 数值是字符数组的起始地址。
第十章 字符串
在C语言中,没有字符串这种数据类型, 因此可以将字符串存放在字符数组中。 这是处理字符串的方法之一。
全国计算机等级考试二级教程__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.892343.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#include{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.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;}elsedays += 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); }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.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#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 C二. 填空题8.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.19 9 09.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 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;}。
校园网.新视野教育全国计算机等级考试二级C语言教程课后习题解析

说明:1、原先的少部分题目有错,请用红色的部分替换掉;2、如果红色部分有文字“删除该行”,就直接删除该行;第一章C语言的基础知识第一节对C语言的初步认识习题1. 下列叙述中错误的是BA)任何一个C程序都必须有且仅有一个main函数,C语言总是从main函数开始执行。
B)C语言中的变量,可以先使用后定义。
C)所有的C语言语句最后都必须有一个分号D)C程序书写格式自由,语句可以从任一列开始书写,一行内可以写多个语句。
第二节熟悉Visual C++习题1. C语言源程序名的后缀是 BA).exe B).c C).obj D).cp 2. 下列叙述中错误的是DA)计算机不能直接执行用C语言编写的源程序B)C程序经C编译后,生成后缀为.obj的文件是一个二进制文件C)后缀为.obj的文件,经连接程序生成后缀为.exe的文件是一个二进制文件D)后缀为.obj和.exe的二进制文件都可以直接运行3. 用C语言编写的代码程序BA)可立即执行B)是一个源程序C)经过编译即可执行D)经过编译解释才能执行第三节标识符习题1. 按照C语言规定的用户标识符命名规则,不能出现在标识符中的是BA)大写字母B)连接符C)数字字符D)下划线2. 以下选项中不合法的标识符是 CA)print B)FOR C)&a D)_003. 以下叙述中错误的是AA)用户所定义的标识符允许使用关键字B)用户所定义的标识符应尽量做到“见名知意”C)用户所定义的标识符必须以字母或下划线开头D)用户定义的标识符中,大、小写字母代表不同标识4. 可在C程序中用作用户标识符的一组标识符是AA)and B)Date C)HiD)case_2007 y-m-d Dr.TomBigl5. 以下不合法的用户标识符是CA)j2_KEY B)Double C)4d D)_8_6. 以下不能定义为用户标识符的是DA)Main B)_0 C)_int D)sizeof7. 下列选项中,不能用作标识符的是DA)_1234_ B)_1_2 C)int_2_ D)2_int_8. 以下4组用户定义标识符中,全部合法的一组是AA)_main B)If C)txt D)intenclude -max REAL k_2sin turbo 3COM _001第四节数制转换习题1.十进制整数360的八进制为__________,十六进制为_____________。
二级教材《C语言程序设计》第10章

第十章字符串习题答案一、选择题:题号答案解题思路10.1BA所赋的是一个字符数组,由于没有'\0'作为结束,因此不是字符串;C赋初值时相当于下标越界。
D中S是一个常量不能被改变。
10.2B字符串中的最后一个字符是'\0'。
10.3C每一个'\'后面跟的一个字符与'\'结合构成一个转义字符。
10.4B*c-='A'-'a'即等于*c+=32,把字母的ascii值增加32,大写变小写。
10.5C。
10.6A每循环一次,指针P向高位移动一个单元,打印此单元起的字符串。
10.7D首先,aa是指针数组,一维数组,每个数组元素存放着一个字符串的首地址。
为什么不是C,因为字符数组中含有的字符数应当是5。
10.8A程序中每循环一次取出字符串组中相隔为两个单元的数字组成一个整型数。
10.9Cp[i]是每个字符串的首地址,以%s格式输出时,从首地址的字符起,直到遇到'\0'结束输出,循环进行了4次,输出前四个字符串。
10.10C看似一样,实不一样,其他三个拷贝均有确定的首地址可供寻找,独第三个不确定。
二、填空题:10.11还没输出就出错了-chp表示什么?指针前面是不可以加负号的。
如果没有这个负号,则结果是死循环。
题目有问题。
10.12XYZA函数的功能是把每个字符的ASCII码值加23,超过25的从A开始。
10.13SO循环的结果是每次打印一个字符,地址是p[i]即首地址所指向的元素。
第一次是p[3],第二次是p[1],注意p[1]是第二个元素。
10.1410 函数的功能是把两个字符串连起来并计算总的有效字符数。
10.15Itis函数的功能比较复杂,主要是注意j++中表达式的值以及j的值什么时候变,它与i的值不是同步变化的。
10.16(strlen(str)-1)j—strlen(str)得到字符串长度,再减1得到字符数组的下标号。
《全国计算机等级考试二级教程——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语言全国计算机二级等级考试教程第10章 字符串(章节带习题)

输出时直到遇第一 char str[ ]=“I love you!” , *p=str; 个‘\0‟结束输出。 字符指针变量 printf(“%s\n” , p); 字符数组名
输入项 字符指针变量
转去介绍
10.3 字符串的输入和输出
字符数组名
输入项
字符指针变量 当字符数组名作为输入项: char str[15]; scanf(“%s”,str); 在内存中的形式是: P R 输入形式为: PROGRAMMING<CR> O G R A M M I N Ga赋初值后的存储示意图
10.4 字符串数组
可以定义字符型指针数组并通过赋初值来构成一个类似的字符串数组。 例如:char *pa[3]={“a”, ”bb”, ”ccc”}; 定义了一个字符型指针数组,该数组中含有3个元素pa[0] 、pa[1]、 pa[2],且每一个 元素都是一个指向字符串的指针。
则在str中存入的只是字符串“APROGRAM”,而不是字符串“APROGRAM MING
10.3 字符串的输入和输出
10.3.3 调用gets、puts函数在终端输入或输出一行字符串
调用gets函数来输入字符串,例如: char str[15]; gets( str ); 在内存中的形式是: A P 若从键盘上依次输入: APROGRAM MING<CR> R O G R A M M I N G \0 \0
★ 两个字符串分别占有不同的存储空间。 ★ 指针变量pmark中的地址可以改变而指向另外一个长度 不同的字符串。
10.3 字符串的输入和输出
10.3.1 输入和输出字符串时的必要条件
输入和输出的格式说明符为 %s 对字符串输入输出时的必要条件: 字符串常量 输出项 字符数组名 printf(“%s\n” , ”I love you!”); char str[ ]=“I love you!”; printf(“%s\n” , str); I love you!
全国计算机等级考试二级教程--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语言10练习(字符串含答案)

(1)下列语句中,正确的是A) char *s ; s=”Olympic”; B) char s[7] ; s=”Olympic”;C) char *s ; s={”Olympic”}; D) char s[7] ; s={”Olympic”};2) 。
main(){ printf("%d\n",strlen("IBM\n012\1\\")); }3) 以下程序的输出结果是 54 。
#include <string.h>main(){ char a[]={'\1', '\2', '\3', '\4', '\0'};printf("%d %d\n",sizeof(a),strlen(a));}4) 有以下程序main(){ char s[]={ "aeiou" },*ps;ps=s; printf("%c\n",*ps+4);}程序运行后的输出结果是A)a B)e C)u D)元素s[4]的地址5) 以下语句中存在语法错误的是A)char ss[6][20]; ss[1]= "right?"; B)charss[ ][20]={ "right?"};C)char *ss[6]; ss[1]= "right?"; D)char *ss[ ]={ "right?"};6) 若有定义:char *x="abcdefghi";,以下选项中正确运用了strcpy函数的是A)char y[10]; strcpy(y,x[4]); B)char y[10];strcpy(++y,&x[1]);C)char y[10],*s; strcpy(s=y+5,x); D)char y[10],*s;strcpy(s=y+1,x+1);7) 有以下程序main(){ char s[]="abcde";s+=2;printf("%d\n",s[0]);}执行后的结果是A)输出字符a的ASCII码 B)输出字符c的ASCII码 C)输出字符c D)程序出错(8#include <string.h>char *fun(char *t){ char *p=t;return(p+strlen(t)/2);}main(){ char *str="abcdefgh";str=fun(str);puts(str);}(9)有以下程序#include <stdio.h>void fun(char *a, char *b){ while(*a==’*’) a++;while(*b=*a) {b++;a++;}}main(){ char *s=”****a*b****”,t[80];fun(s,t);puts(t);}程序的运行结果是A) *****a*b B) a*b C) a*b**** D) ab(10) 有以下程序#include <stdio.h>#include <string.h>void fun(char *str){char temp;int n,i;n=strlen(str);temp=str[n-1];for(i=n-1;i>0;i--) str[i]=str[i-1];str[0]=temp;}main(){char s[50];scanf(”%s”,s); fun(s); printf(”%s\n”,s);}程序运行后输入:abcdef<回车>,则输出结果是3. 有以下程序#include<stdio.h>main(){ char a[20],b[20],c[20];scanf(“%s%s”,a,b);gets( c);printf(“%s%s%s”,a,b,c);}程序运行时从第一列开始输入:This is a cat!<回车>则输出结果是A) Thisisacat! B) Thisis a C) Thisis a cat! D) Thisisa cat!。
全国计算机等级测验二级教程C语言程序设计课后习题答案

main()ﻫ{ﻫint a,b; ﻫa=1500;b=350;ﻫprintf("a divb is: %d\n",a/b);
printf("a mod bis :%d\n",a%b);ﻫ}
3.32
#include
main()
{ ﻫdoublea,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 ﻫ#include
}
ﻫ第四章ﻫ一.选择题ﻫ4.1 A4.2 A 4.3 A4.4D 4.5C4.6A4.7 B 4.8C4.9 D4.10 C
二.填空题
4.11非00ﻫ4.12 <> >=<=同级== !=同级ﻫ4.13! && ||
4.15 ! ﻫ4.16a ==b ||a<cx>4|| x <-4 ﻫ4.171 ﻫ4.18x<=01>0ﻫ4.193 22
函数结束处遗失了一个“}”
ﻫ2.29ﻫ#include"stdio.h"
main/*mainfunction*/main后遗失了“()”
{
floata,b,c,v; /*a,b,c aresides, v isvolume ofcube */ ﻫa=2.0; b=3.0; c=4.0行尾遗失了“;”ﻫv=a*b*c;
1.3顺序 选择 循环
ﻫ第二章ﻫ一.选择题ﻫ2.1 B 2.2 D 2.3 B 2.4A 2.5 C2.6A2.7 B
《全国计算机等级考试二级教程——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] 10000000000000002.27 【参考答案】[1]十[2]八[3]十六三、上机改错题2.28 【分析与解答】第1行的错误:(1) include是一个程序行,因此在此行的最后不应当有分号(;)。
C语言全国计算机二级等级考试教程第10章 字符串(章节带习题)

用一维字符数组存放字符串 使指针指向一个字符串 字符串的输入和输出 字符串数组 用于字符串处理的函数 程序举例
10.1 用一维字符数组存放字符串
字符常量:是用单引号括起来的一个字符。 例如 „A‟ „C‟ „t‟ „!‟ „?‟
在内存中占一个字节
字符串常量:是由双引号括起来的一串字符 。
例如 “CHINA” C H 在内存中占6个字节 I N A \0
10.2 使指针指向一个字符串
用字符数组作为字符串和用指针指向的字符串之间的区别
若有以下定义: char mark[ ]= “PROGRAM”; char *pmark= “PROGRAM”; 它们在内存中的存储形式如下: mark pmark P R O G R A M \0
PROGRAM\0
区别
此函数功能是计算出以s为起始地址的字符串的长度,并作为函数返回值。 注意:不包括串尾的结束标志’\0‟。 ★ 字符串比较函数strcmp。调用形式为: strcmp(s1,s2);
此函数功能是来比较s1和s2所指字符串的大小。若s1>s2,函数值为大于0(正数)的数; 若s1==s2,函数值等于0;若s1<s2,函数值为小于0(负数)的数。
该数组中只包含7个元素
10.1 用一维字符数组存放字符串
10.1.1 通过赋初值的方式给一维字符数组赋字符串
也可以直接用字符串常量给一维字符数组赋初值。例如: char str[10]={ “sting!” }; 习惯上均省略花括号 char str[10]=“sting!”; 把一个字符串常量赋值给一个一维字符数组时,数组的元素个数 一定要大于字符串的实际长度 char str[6]=“sting!”;由于字符串的实际长度为6,而数组只有6个存储 单元,‘\0‟将无法存放。 如下的定义形式也可以正确进行: char str[ ]=“sting!”; 数组中包含7个元素 (含系统自动加的’\0‟)
C语言计算机二级考试教程第10章

4
输入字符串的分隔符: scanf()函数的分隔符为:空格和回车键 gets()函数的分隔符为: 仅为回车键
例如:char a[10], b[10],c[10]; scanf(“%s%s”, a, b ); 输入:to ok<enter> a gets(c); 输入:to ok<enter>
t o \0
6
4个字符串函数
1.字符串复制函数strcpy(): strcpy(s1,s2); 将s2指针所指空间的字符串复制到s1所指空间。合法性: s1指针所指的内存空间必须是具体的,足够大的。 a b 例:char a[10]=“ok”,b[10],*p; p o o strcpy(b,a); 正确 k k \0 \0 strcpy (p, a); 错误 p=b; strcpy(p,a); 正确
o k \0
b
8
3.计算字符串长度函数strlen(): strlen(s) strlen(s)函数值为指针s所指串的长度,不包括 ‘\0‟。 例: char a[10]=“ok to”, b[5]=“s\016\t\0end”, char *p=“\101B\103D\n”; strlen(a) 函数值为5 strlen(b)函数值为3 strlen(p)函数值为5 strlen(“\b\n\t12345” )函数值为8
18
例:按字符串大小排序
void sort (char s[ ][5] , int n) { int i,j; char t[5]; strlen(s[i])> strlen(s[j]) for( i=0;i<=n-2;i++) for( j=i+1;j<=n-1;j++) if( strcmp(s[i],s[j])>0 ) { strcpy(t,s[i]); strcpy(s[i,]s[j]); strcpy(s[j] ,t); } } main( ) { char a[3][5]={“ok”,”to”,”yes”} ; sort(a,3); }
二级C语言考试辅导教程第十1.docx

二级C语言考试辅导教程第十章:文件[2]2008-12-12 17:25:49来源:互联网浏览次数:10网友评论0条[收藏此页]复制H分享到i贴吧对于文件使用方式有以下几点说明:1.文件使用方式由r,wat,b, +六个字符拼成,各字符的含义是:r(read):读w(write):写a(append):追加t(text):文本文件,可省略不写b(banary):二进制文件+:读和写2.凡用片,打开一个文件时,该文件必须已经存在,且只能从该文件读出。
3.用“w”打开的文件只能向该文件写入。
若打开的文件不存在,则以指定的文件名建立该文件,若打开的文件已经存在,则将该文件删去,重建一个新文件。
4.若要向一个已存在的文件追加新的信息,只能用“a ”方式打开文件。
但此时该文件必须是存在的,否则将会出错。
5.在打开一个文件吋,如果出错,fopen将返回一个空指针值NULL。
在程序屮可以用这一信息來判别是否完成打开文件的工作,并作相应的处理。
因此常用以下程序段打开文件:if((fp=fopen(,,C:\\hzkl6H;,rb,,)==NULL){printf("\nerror on open C:\\hzkl6 file!");getCh();exit(l);这段程序的意义是,如果返回的指针为空,表示不能打开C盘根目录下的hzkl6文件,则给出提示信息4t error on open C:\hzkl6file!”,下一行getCh()的功能是从键盘输入一个字符, 但不在屏幕上显示。
在这里,该行的作用是等待,只有当用户从键盘敲任一键时,程序才继续执行,因此用户可利用这个等待时间阅读出错提示。
敲键后执行exit(l)退出程序。
6.把一个文本文件读入内存时,要将ASCII码转换成二进制码,而把文件以文本方式写入磁盘吋,也要把二进制码转换成ASCII码,因此文本文件的读写要花费较多的转换时间。
对二进制文件的读写不存在这种转换。