C语言程序设计习题集沈国荣-参考答案.pdf
.C C 语言程序设计习题解答语言程序设计习题解答沈国荣沈国荣 隋雪莉隋雪莉 闵芳闵芳目录目录第 1 章 C 语言程序设计概述.2第 2 章 数据类型及其运算.3第 3 章 语句与输入输出.4第 4 章 选择结构程序设计.6第 5 章 循环结构程序设计.9第 6 章 数组.12第 7 章 函数.15第 8 章 编译预处理.18第 9 章 指针.19第 10 章 结构体与共用体.22第 11 章 位运算.26第 12 章 文件操作.27.第 1 章 C 语言程序设计概述一、选择题一、选择题1C2B3A4C5C6B7D8B9D10A二、填空题二、填空题1.函数、main()函数2./*、*/3.C、.OBJ、.EXE4.顺序结构、选择结构、循环结构三、编程题三、编程题1.【参考代码】#includeint main()printf((学校名称)n);printf((姓名)n);return 0;2.【参考代码】#includeint main()printf((学校名称)n(姓名)n);return 0;.第 2 章 数据类型及其运算一、选择题一、选择题1C11C2C12A3C13D4A14D5B15D6A16C7D17D8D18C9C、B1910B20注:第 5 题 B 选项为:017t二、填空题二、填空题1.字母、数字、下划线2.1、4、4、83.-164.3.55.06.97.68.-609.y%2=110.1、0、1三、程序阅读题三、程序阅读题1.b2.03.04.10,25.9,10,9,106.3,1,0,07.3,20,30,1.第 3 章 语句与输入输出一、选择题一、选择题1B2C3C4C5C6B7B8B9D10A二、填空题二、填空题1.123.472.D3.回车4.10,2三、程序阅读题三、程序阅读题1.2612.203.201,104.y=4630y=46305.*3.140000,3.142*6.c:dec=120,oct=170,hex=78,ASCII=x7.x=1 y=2*sum*=310 squared is:1008.2 48 20.2 20.29.x+y+z=4810.55,A四、编程题四、编程题1.【参考代码】#includeint main()char ch;printf(请输入一个字符:n);scanf(%c,&ch);printf(%c 的 ASCII 码为:%dn,ch,ch);return 0;2.【参考代码】#include#define PI 3.1416int main()double r,h;double cl,cs,cv;.printf(请输入圆的半径:);scanf(%lf,&r);printf(请输入圆柱高:);scanf(%lf,&h);cl=2*PI*r;cs=PI*r*r;cv=PI*r*r*h;printf(圆的周长为:%.4lfn,cl);printf(圆的面积为:%.4lfn,cs);printf(圆柱的体积为:%.4lfn,cv);return 0;3.【参考代码】#includeint main()int splitInt,one,ten,hundred;printf(输入要处理的整数:);scanf(%d,&splitInt);hundred=splitInt/100;ten=splitInt%100/10;one=splitInt%10;printf(个位:%d,十位:%d,百位:%dn,one,ten,hundred);return 0;.第 4 章 选择结构程序设计一、选择题一、选择题1D2C3C4B5B6D二、程序阅读题二、程序阅读题1.102.2,2,23.64.97,b5.c=-16.88887.20,08.2,19.1,12,22,1-2,210.a=1,b=3三、程序完善题三、程序完善题1.ab、cx四、编程题四、编程题1.【参考代码】#includeint main()int a,b,c,d,min;printf(输入 4 个整数:);scanf(%d%d%d%d,&a,&b,&c,&d);if(a b)min=a;else min=b;if(c min)min=c;if(dmin)min=d;printf(%dn,min);return 0;2.【参考代码】#includeint main()int num,a,b,c,d;printf(请输入一个四位整数:);.scanf(%d,&num);a=num/1000;b=num%1000/100;c=num%100/10;d=num%10;printf(各位数字之和为:%dn,a+b+c+d);return 0;3.【参考代码】#includeint main()int x;printf(请输入 x:);scanf(%d,&x);printf(y 的值为:);if(x0)printf(%dn,x);else if(x50)printf(%dn,3*x-2);else if(x100)printf(%dn,4*x+1);elseprintf(%dn,5*x);return 0;4.【参考代码】#includeint main()int dj;float zl,je,yfk;printf(请输入等级(14):);scanf(%d,&dj);if(dj4|dj1)printf(无此等级的苹果!n);return 0;printf(请输入重量(公斤):);scanf(%f,&zl);printf(n);switch(dj).case 1:je=5.5*zl;break;case 2:je=4.3*zl;break;case 3:je=3.0*zl;break;case 4:je=2.5*zl;break;printf(您选择苹果级别:%d 级n,dj);printf(您购买苹果重量:%.2f 公斤n,zl);printf(您应付金额为:%.2f 元n,je);printf(n);printf(顾客所付金额:);scanf(%f,&yfk);if(yfk Z&ch z)2.kk/103.i+t*10s=s+t4.fabs(t)=1e-6f=-f5.i10j%3!=0四、编程题四、编程题1.【参考代码】#includeint main()int n,i,j,k;printf(Output:n);for(n=100;n 1000;n+)i=n%10;/*个位*/j=(n/10)%10;/*十位*/k=n/100;/*百位*/if(n=i*i*i+j*j*j+k*k*k)printf(%dn,n);return 0;2.【参考代码】#includeint main().ch-26continue.int i,m,n,t,p,k;printf(Please input:);scanf(%d,%d,&m,&n);if(m n)t=n;n=m;m=t;p=m*n;while(n!=0)/*余数不为 0,继续相除,直到余数为0i=m%n;m=n;n=i;k=p/m;printf(%d,%dn,m,k);return 0;3.【参考代码】#includeint main()int i,n,t,sum;t=1;sum=0;printf(Please input:n=);scanf(%d,&n);for(i=1;i=n;i+)t=t*i;sum=sum+t;printf(1!+2!+%d!=%dn,n,sum);return 0;4.【参考代码】#includeint main()inti,m;double sum=0,k=1;printf(Please input:m=);.*/.scanf(%d,&m);for(i=1;i=m;i+)sum=sum+k/i;k=-k;printf(sum=%4.2fn,sum);return 0;.第 6 章 数组一、选择题一、选择题1D2D3D4B5C6C7B8C9C10D11D1213141516BDDCD二、程序阅读题二、程序阅读题182430,24125t*M6mo7fwo三、程序完善题三、程序完善题1k=ij=iak=maxaj=min2sum+=scoreiscoreiavg3si=si+aijprintf(n);4j=strlen(str)1strj=k5(c=getchar()!=#numc-A+=1四、编程题四、编程题1.【参考代码】#include#define N 5int main()int aN,i,j,r,temp;printf(Please input%d numbersn,N);for(i=0;i N;i+)scanf(%d,&ai);for(i=0;i N-1;i+)r=i;for(j=i+1;j N;j+)if(aj ar)r=j;if(r!=i)temp=ar;ar=ai;ai=temp;.printf(The array after sort:n);for(i=0;i N;i+)printf(%5d,ai);printf(n);return 0;2.【参考代码】#includeint main()int a10=1,2,3,6,7,8,9,10 ;int x,j,k=0;printf(Please input:x=);scanf(%d,&x);if(x a7)a8=x;elsefor(j=0;j 8;j+)if(x j;k-)ak=ak-1;aj=x;for(j=0;j 9;j+)printf(%5d,aj);printf(n);return 0;3.【参考代码】#includeint main()int a55=0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24;int i,j,sum=0;for(i=0;i 5;i+)for(j=0;j 5;j+)printf(%4d,aij);printf(n);for(i=0;i 5;i+).sum+=aii;printf(sum=%4dn,sum);return 0;4.【参考代码】#includeint main()char s1100,s230;inti,j;printf(Please input s1:);gets(s1);printf(Please input s2:);gets(s2);for(i=0;s1i!=0;i+);for(j=0;s2j!=0;j+,i+)s1i=s2j;s1i=0;printf(Outputns1:);puts(s1);return 0;5.【参考代码】#includeint main()char s1100;inti;printf(Please input s1:);gets(s1);for(i=0;s1i!=0;i+);printf(The length of s1 is%dn,i);return 0;.第 7 章 函数一、选择题一、选择题1B2D3D4A5B6B7C8B9D10D11A12A13D14D15A二、程序阅读题二、程序阅读题1max is 22a=1,b=231114a=11,b=12,c=2566789781780 1 2 0 1 2三、程序完善题三、程序完善题1float area(float r)return s2z=fun(x,y)z=z*x3count=fun(score)count+四、编程题四、编程题1.【参考代码】#includeint main()void f(int n);int n;printf(Please input:n=);scanf(%d,&n);if(n=0)printf(Wrong number!n);elsef(n);return 0;void f(int n)if(n%2=1)printf(%d is a odd number.n,n);elseprintf(%d is a even number.n,n);2.【参考代码】#include.#include int main()void f(int m);int m;printf(Please input:m=);scanf(%d,&m);f(m);return 0;void f(int m)int i,k;k=sqrt(m);for(i=2;i=k+1)printf(%d is a Prime Number.n,m);elseprintf(%d is not a Prime Number.n,m);3.【参考代码】#includeint gys(int m,int n)int r;r=m%n;while(r!=0)m=n;n=r;r=m%n;return n;int gbs(int m,int n,int r)return m*n/r;int main()int m,n,t;printf(Please input(m,n):);.scanf(%d%d,&m,&n);if(m n)t=m;m=n;n=t;t=gys(m,n);printf(gys=%dn,t);t=gbs(m,n,t);printf(gbs=%dn,t);return 0;4.【参考代码】#includeint main()void mystrcat(char s1100,char s230);char s1100,s230;printf(Please input s1:);gets(s1);printf(Please input s2:);gets(s2);mystrcat(s1,s2);printf(Outputns1:);puts(s1);return 0;void mystrcat(char s1100,char s230)int i,j;for(i=0;s1i!=0;i+);for(j=0;s2j!=0;j+,i+)s1i=s2j;s1i=0;.第 8 章 编译预处理一、选择题一、选择题1A2D3D4A5B6A二、程序阅读题二、程序阅读题16,1821535.第 9 章 指针一、选择题一、选择题1D13C2B14D3B15D4C16A5D17C6C7C8C9D10B11C12C二、填空题二、填空题1.地址,NULL(或 0)2.char a,*p;,scanf(%c,&a);,p=&a;3.*m4.for(k=0;k10;k+)5.*(p+i),pi,*(x+i)6.stri或*(str+i),i三、程序阅读题三、程序阅读题1.gae2.bcdABCD3.7,8,84.845.3 14 26.efgh7.w,one8.7四、程序完善题四、程序完善题1.ai或*(a+i)2.*p!=0*p-03.p1p2-x4.max(int a,int b);p=max四、编程题四、编程题1.【参考代码】#includevoid sort(int*a,int*b,int*c);int main()int m,n,t;printf(Please input(m n t):);scanf(%d%d%d,&m,&n,&t);sort(&m,&n,&t);printf(The result is:%dt%dt%dn,m,n,t);return 0;.void sort(int*a,int*b,int*c)int temp;if(*a *b)temp=*a;*a=*b;*b=temp;if(*a *c)temp=*a;*a=*c;*c=temp;if(*b *c)temp=*b;*b=*c;*c=temp;2.【参考代码】程序 1:#include intstrcompare(char*str1,char*str2);int main()int m;char s120,s220,*p1,*p2;printf(Please input(string1):);scanf(%s,s1);printf(Please input(string2):);scanf(%s,s2);m=strcompare(s1,s2);printf(The result of strcompare is:%dn,m);return 0;intstrcompare(char*str1,char*str2)int i=0;while(*(str1+i)=*(str2+i)&(*(str1+i)!=0)i+;return(*(str1+i)-*(str2+i);程序 2:#include intstrcompare(char*str1,char*str2);int main()int m;char s120,s220,*p1,*p2;printf(Please input(string1):);scanf(%s,s1);printf(Please input(string2):);scanf(%s,s2);p1=s1;p2=s2;.m=strcompare(p1,p2);printf(The result of strcompare is:%dn,m);return 0;intstrcompare(char*str1,char*str2)int i=0;while(*(str1+i)=*(str2+i)&(*(str1+i)!=0)i+;return(*(str1+i)-*(str2+i);程序 3:#include intstrcompare(char str1,char str2);int main()int m;char s120,s220,*p1,*p2;printf(Please input(string1):);scanf(%s,s1);printf(Please input(string2):);scanf(%s,s2);p1=s1;p2=s2;m=strcompare(p1,p2);printf(The result of strcompare is:%dn,m);return 0;intstrcompare(char*str1,char*str2)int i=0;while(str1i=str2i)&(str1i!=0)i+;return(str1i-str2i);.第 10 章 结构体与共用体一、选择题一、选择题1C2B3A4C5C6D7D8D9C10B11D二、填空题二、填空题1.112.p-next=head-nexthead-next=p3.p-next三、程序阅读题三、程序阅读题1.51,60,212.163.1001,ChangRong,1098.0四、程序完善题四、程序完善题1.sizeof(struct ps)或sizeof(bt)2.p=p-next3.personi.sex五、编程题五、编程题1.【参考代码】#define N 3#include struct studentchar num6;char name8;int score2;float ave;void input(struct student stuN);void average(struct student stuN);int max(struct student stuN);int main()int i,j;struct student stuN;input(stu);average(stu);printf(NotNametScore1tScore2tAveragen);for(i=0;i N;i+)printf(%st%st,stui.num,stui.name);for(j=0;j 2;j+).printf(%dt,stui.scorej);printf(%8.2fn,stui.ave);i=max(stu);printf(nThe max is:n);printf(NotNametScore1tScore2tAveragen);printf(%st%st,stui.num,stui.name);for(j=0;j 2;j+)printf(%dt,stui.scorej);printf(%5.2fn,stui.ave);return 0;void input(struct student stuN)int i,j;for(i=0;iN;i+)printf(nPlease input No%d student:n,i+1);printf(No:);scanf(%s,stui.num);printf(Name:);scanf(%s,stui.name);for(j=0;j2;j+)printf(score%d:,j+1);scanf(%d,&stui.scorej);void average(struct student stuN)int i,j,sum;for(i=0;iN;i+)for(j=0,stui.ave=0;j2;j+)stui.ave+=stui.scorej;stui.ave=stui.ave/2;int max(struct student stuN)int i,max,index;max=stu0.ave;index=0;.for(i=1;i N;i+)if(max stui.ave)max=stui.ave;index=i;return index;2.【参考代码】#include#includestruct nodeshort int data;struct node*next;typedef struct nodeNODE;struct node*CreatLink();void PrintLink(NODE*head);intmax(NODE*head);int main()NODE*head;int max_value;head=CreatLink();PrintLink(head);max_value=max(head);printf(The max is:%dn,max_value);return 0;struct node*CreatLink()NODE*head,*p,*q;short int num;head=(NODE*)malloc(sizeof(NODE);head-next=NULL;p=head;printf(Please input(end of-1)!:n);scanf(%d,&num);while(num!=-1).q=(NODE*)malloc(sizeof(NODE);q-data=num;p-next=q;p=q;scanf(%d,&num);p-next=NULL;return head;void PrintLink(NODE*head)NODE*p;p=head-next;printf(The data is:n);while(p!=NULL)printf(%4d,p-data);p=p-next;printf(n);intmax(NODE*head)NODE*p;short int max=-32768;p=head-next;while(p!=NULL)if(max data)max=p-data;p=p-next;return max;.第 11 章 位运算一、选择题一、选择题1D2B3C4A二、填空题二、填空题1.0000 11112.x|ff003.43三、程序阅读题三、程序阅读题1.02.11 223.0四、编程题四、编程题1.【参考代码】#includeint main()short int data,low,high;printf(Please input(short int):);scanf(%d,&data);low=data&0 x00ff;/*0 x00ff表示低字节全 1*/high=data&0 xff00;/*0 xff00表示高字节全 1*/printf(data:0 x%x,the value of low byte is:0 x%xn,data,high);printf(data:0 x%x,the value of high byte is:0 x%xn,data,low);return 0;2.【参考代码】#include int main()short int data,result;printf(Please intput(short int):);scanf(%d,&data);result=data 0 x000f;/*0 x000f表示低 4 位全 1,高 12 位全 0*/printf(The data is 0 x%x nThe result is:0 x%xn,data,result);return 0;.第 12 章 文件操作一、选择题一、选择题1D13C2B、C14D3C15C4B5B6A7B8C9D10B11C12C二、填空题二、填空题1.二进制ASCII(文本)2.FILE*fp#include 3.n 1buf 的首地址4.15.用以获得文件读写位置标记指针的位置,函数返回值为当前文件读写位置标记指针相对于文件开头的字节数6.使文件读写位置标记指针重新返回文件的开头三、程序阅读题三、程序阅读题1.1 22.end3.34.hello,四、程序完善题四、程序完善题1.!feof(fp)fgetc(fp)2.fopen(“num.dat”,”r”)fp,”%d”,&tempz+3.(ch=getchar()ch,fp五、编程题五、编程题1.【参考代码】#include#include#includeint main()FILE*fp;char str100,filename10;int i=0;if(fp=fopen(upper.txt,w+)=NULL)printf(Cannot open file!n);exit(0);printf(Please input(string):n);gets(str);.while(stri!=!)if(stri=a&stri=z)stri=stri-32;fputc(stri,fp);i+;rewind(fp);fgets(str,strlen(str)+1,fp);printf(nThe result is:n);printf(%sn,str);fclose(fp);return 0;2.【参考代码】#include#include#define N 5struct studentchar num10;char name8;int score3;float ave;stuN;int main()int i,j,sum;FILE*fp;for(i=0;i N;i+)printf(nPlease input student information:n,i+1);printf(No:);scanf(%s,stui.num);printf(Name:);scanf(%s,stui.name);sum=0;for(j=0;j 3;j+)printf(Score%d:,j+1);scanf(%d,&stui.scorej);sum+=stui.scorej;stui.ave=sum/3.0;.if(fp=fopen(stud.dat,w)=NULL)printf(cannot open stud for write!n);exit(0);for(i=0;i N;i+)fwrite(&stui,sizeof(struct student),1,fp);fclose(fp);if(fp=fopen(stud.dat,r)=NULL)printf(cannot open stud for read!n);exit(0);printf(nNotNametScore1tScore2tAveragen);for(i=0;i N;i+)fread(&stui,sizeof(struct student),1,fp);printf(n%st%st%dt%dt%5.2fn,stui.num,stui.name,stui.score0,stui.score1,stui.score2,stui.ave);fclose(fp);return 0;3.【参考代码】#include#include#define N 5struct studentchar num10;char name8;int score3;float ave;stuN;int main()int i,j,min,index;FILE*fp;struct student temp;/*从 stud.dat 文件中读入数据,存放在stu 数组中*/.if(fp=fopen(stud.dat,r)=NULL)printf(cannot open stud for read!n);exit(0);printf(nThe data is:);printf(nNotNametScore1tScore2tAveragen);for(i=0;i N;i+)fread(&stui,sizeof(struct student),1,fp);printf(n%st%st%dt%dt%5.2fn,stui.num,stui.name,stui.score0,stui.score1,stui.score2,stui.ave);fclose(fp);/*对 stu 数组中数组元素按其平均值数据域排序*/for(i=0;i N-1;i+)min=stui.ave;index=i;for(j=i+1;j stuj.ave)index=j;if(index!=i)temp=stui;stui=stuindex;stuindex=temp;/*将排序之后的 stu 数组存放在 stu_sort 文件中*/if(fp=fopen(stu_sort.dat,w)=NULL)printf(cannot open stud for write!n);exit(0);for(i=0;i N;i+)fwrite(&stui,sizeof(struct student),1,fp);fclose(fp);/*将 stu_sort 文件中的数据,读出存放在 stu 数组中*/.if(fp=fopen(stu_sort.dat,r)=NULL)printf(cannot open stud for read!n);exit(0);printf(nThe result is:);/*将 stu 数组中的数据输出*/printf(nNotNametScore1tScore2tAveragen);for(i=0;i N;i+)fread(&stui,sizeof(struct student),1,fp);printf(n%st%st%dt%dt%5.2fn,stui.num,stui.name,stui.score0,stui.score1,stui.score2,stui.ave);fclose(fp);return 0;.