C语言考试复习题(重点课后习题及答案).pdf
课后作业题第三 章:2、分析如下:如 果 r 为年利率,n 为存款年数,则计算本息和的公式为:1 年期本息和:p=本 金 x(l+r)n 年期本息和:/)=本金乂(1 +入 )存 n 次 1 年期的本息和:p=本 金*(1+r)”活期本息和(按照季度计息):p=本金 X(1+)44则程序如下:#include#includemain()(float r5zr3,r2,rl,r0,p,plzp2,p4,p5;p=1000;r5=0.0585;r3=0.054;r2=0.0468;rl=0.0414;r0=0.0072;pl=p*(l+r5)*5);p2=p*(l+2*r2)*(l+3*r3);p4=p*pow(l+rl,5);p5=p*pow(l+r0/4,4*5);Printf(“pl=%fn”,pl);Printf(p2=%fn,/p2);Printf(”p3=%fn”,p3);Printf(p4=%fn”,p4);Printf(p5=%fn,/p5);)3、include ffinclude int main()float d=300000,p=6000,r=0.01,m;m=loglO(p/(p-d*r)/loglO(l+r);printf(,m=%6.2fn,m);return 0;7、#include#define PI 3.1415926main()(float h,r,l,s,sq,vq,vz;printf(请输入半径r 及 高 h:);scanf(%f%f,&r,&h);p3=p*(l+3*r3)*(l+2*r2);l=2*PI*r;vz=PI*r*r*h;s=PI*r*r;(3)1sq=4*PI*r*r;(4)0vq=3.0/4*PI*r*r*r;(5)1printf(%dn,(!(ab)&!c|11);printf(“圆周长为 l=%fnJ);8、#include printf(“圆面积为 s=%fn/s);int main()printf(“圆球表面积为 sq=%fn,sq);float score;printf(圆球体积为 vq=%fnq);char grade;printf(圆柱体积为 vz二fn,vz);printf(”请输入学生成绩:“);)scanf(%f,&score);while(score100|score0)printf(n输入有误,请重输”);第 四 章:scanf(%f,&score);3、#include main()(switch(int)(score/10)case 10:int a=3,b=4,c=5,x,y;case 9:grade=A;break;printf(%dnz(a+bc&b=c);case 8:grade=B;break;printf(%dn,(a|b+c&b-c);case 7:grade=C;break;printf(%dn,(!(a+b)+c-l&b+c/2);printf(%dn,(!(x=a)&(y=b)&0);case 6:grade=D;break;case 5:(2)1)case 4:0case 3:case 2:case 1:while(cl)case 0:grade=E;)printf(”成绩 是%5.lf,相应的等级%c n score,grade);return 0;)9#include main()(long azb=0zc=l;int i=0,m;printf(输入整数(0-99999):“);scanfC%ld;&a);b=a;while(b)(b=b/10;i=i+l;c=c*10;printf(n%ld的位数为5 陛 声 川;printf(“ld 的各位数字分别为(从高位到底为)a);b=a;c=c/10;m=b/c;b=b%c;printf(%3dzm);i=i+l;)printf(n%ld 的逆序数为:za);b=0;while(a)(m=a%10;b=b*10+m;a=a/10;)printf(%ldn,b);)第 五 章:4、Sinclude int main()(char c;int letters=0,space=0,digit=0,other=0;printf(请输入一行字符:n);while(c=getchar()!=n)(if(c=a&c=A&c=0&c=9)digit+;elseother+;)printf(字母数:dn空格数:dn数字数:dn其它字符:%dn,letters,space,digit,other);return 0;F7、#include int main()(int nl=100,n2=50,n3=10;double k,sl=0,s2=0,s3=0;for(k=l;k=nl;k+)/*计算 1 到 100 的和*/for(k=l;k=n2;k+)/*计算 1 到 50 各数的平方和*/s2=s2+k*k;for(k=l;k=n3;k+)/*计算 1 到 10 的各倒数和*/s3=s3+l/k;printf(sum=%15.6fn,sl+s2+s3);return 0;8、#includemain()(intsl,s2,il,i2,i3;for(il=l;il10;il+)(for(i2=0;i210;i2+)(for(i3=0;i310;i3+)(Sl=il*il*il+i2*i2*i2+i3*i3*i3;s2=il*100+i2*10+i3;if(sl=s2)printf(%5d,sl);sl=sl+k;)int i,j;)al=l;for(i=2;i=N;i+)10、#include(if(ai=0)i=i+l;main()t(printf(%5d”,i);float fl=l,f2=2,f3,s=0;a0+;int i=0;for(j=i+l;j=N;j+=i)doaj=l;()s+=f2/fl;)f2=fl+f2;printf(nNUM=%d,aO);fl=f2-fl;)int aN+l=0;while(i20);2、从大到小选择排序)#define N 10#include main()第 六 章:(1、#define N 100int aN,i,j/k,temp;#include for(i=0;iN;i+)main()iscanf(%d,&ai);for(i=0;iN;i+)printf(%5dzai);printf(n);for(i=0;iN-l;i+)(k=i;for(j=k+l;jak)k=j;temp=ai;ai=ak;ak=temp;for(i=0;iN;i+)print%5dprintf(n);10、#include int main()int ijzupp,low,dig,spa,oth;char text380;upp=low=dig=spa=oth=0;for(i=0;i3;i+)printf(please input line%d:nJ+l);gets(texti);for(j=0;j=a&textij=,0,&textij=,9,)dig+;else if(textij=-)spa+;elseoth+;)printf(Hnupper case:%dn,upp);printf(lower case:%dnJow);printf(digit:%dn,dig);printf(space:%dn,spa);printf(other:%dn,oth);return 0;13、#include int main()char sl80,s240;int i=0,j=0;printf(input stringl:);if(textij=A&textiju)while(s2j!=0)t=u;u=v;v=t;sli+=s2j+;while(r=u%v)!=0)sli=O;u=V;printf(nThe new string is:%sn,sl);v=r;return 0;return(v);第七章:1、int lcd(int u,int v,int h)l.#include(int main()return(u*v/h);int hcf(int,int);int lcd(int,int,int);2.include int u,v,h,l;int Hcf,Lcd;scanf(%d,%d,&u,&v);int main()h=hcf(u,v);void hcf(intjnt);printf(H.C.F=%dn,h);void lcd(intjnt);l=lcd(u,v,h);int u,v;printf(L.C.D=%dn,l);scanf(%d,%d”,&u,&v);return 0;hcf(u,v);lcd(u,v);printf(H.C.F=%dn,Hcf);float xl,x2zdisc,p,q;printf(,LC.D=%dnzLcd);return 0;void hcf(int uzint v)int t,r;if(vu)t=u;u=v;v=t;while(r=u%v)!=0)u=v;v=r;Hcf=v;)void lcd(int u,int v)(Lcd=u*v/Hcf;)2、#include include int main()void greater_than_zeroffloatjloat);void equal_to_zero(float,float);void smaller_than_zero(float,float);float a,b,c;printf(input a,b,c:);scanf(%f,%f,%f”,&a,&b,&c);printf(equation:%5.2f*x*x+%5.2f*x+%5.2f=0n,a,b,c);disc=b*b-4*a*c;printf(root:n);if(disc0)(g reate r_t h a n_z e r o(a,b);printf(xl=%fttx2=%fn,xl/x2);else if(disc=0)equal_to_zero(a,b);printf(xl=%fttx2=%fn,xl/x2);elsesmaller_than_zero(a,b);printf(xl=%f+%fitx2=%f-%fin,p,q,p,q);return 0;)void greater_than_zero(float a,float b)xl=(-b+sqrt(disc)/(2*a);x2=(-b-sqrt(disc)/(2*a);)void equal_to_zero(float a,float b)(xl=x2=(-b)/(2*a);)void smaller_than_zero(float a,float b)#define N 10char strN;int main()void sort(char);int i,flag;for(flag=l;flag=l;)printf(input string:n);scanf(%s,&str);if(strlen(str)N)printf(string too longjnput again!);elseflag=O;sort(str);printf(string sorted:n);for(i=0;iN;i+)printf(%czstri);printf(Hn);p=-b/(2*a);return 0;q=sqrt(-disc)/(2*a);void sort(char str)11、include int i,j;/include char t;for(j=l;jN;j+)for(i=O;(istri+l)t=stri;stri=stri+l;stri+l=t;)第八章:l#include int main()void swap(int*pl,int*p2);int nlzn2,n3;int*pl,*p2,*p3;printf(input three integer nl,n2,n3:);scanf(%d,%d,%cT,&nl,&n2,&n3);pl=&nl;p2=&n2;p3=&n3;if(nln2)swap(pl,p2);if(nln3)swap(pl,p3);if(n2n3)swap(p2,p3);printf(Now,the orderis:%dz%d,%dn/n l/n2/n3);return 0;)void swap(int*pl,int*p2)int p;p=*pl;*pl=*p2;*p2=p;15、#include int main()void avsco(float*,float*);void avcourl(char(*)10,float*);void fali2(char course510Jntnum,float*pscore,float aver4);void good(char course510,intnum4,float*pscore,float aver4);int i,j,*pnum,num4;float score45,aver4,*pscore,*paver;char course510,(*pcourse)10;printf(input course:n);pcourse=course;for(i=0;i5;i+)scanf(%s,coursei);printf(input NO.and scores:n);printf(NO.);for(i=0;i5;i+)printf(,%s,coursei);printf(n);pscore=&score0 0;pnum=&num0;for(i=0;i4;i+)scanf(d”,pnum+i);for(j=0;j5;j+)scanf(%f,pscore+5*i+j);paver=&aver0;printf(nn);avsco(pscore,paver);求出每个学生的平均成绩avcou rl(pcou rse,pscore);/求出第一门课的平均成绩printf(nn);fali2(pcourse,pnum,pscore,paver);/找出2 门课不及格的学生printf(nn);good(pcourse,pnum,pscore,paver);/找出成绩好的学生return 0;void avsco(float*pscore,float*paver)/求每个学生的平均成绩的函数int i,j;float sum,average;for(i=0;i4;i+)sum=0.0;for(j=0;j5;j+)sum=sum+(*(pscore+5*i+j);累计每个学生的各科成绩average=sum/5;计算平均成绩*(paver+i)=average;)void avcourl(char(*pcourse)10,float*pscore)求第一课程的平均成绩的函数int i;float sumzaveragel;sum=0.0;for(i=0;i4;i+)sum=sum+(*(pscore+5*i);累计每个学生的得分averagel=sum/4;均成绩计算平printf(course l:%s averagescore:%7.2fn/*pcourse,averagel);)void fali2(char course510,intnum,float*pscore,float aver4)找两门以上课程不及格的学生的函数int i,j,k,label;printf(=Student who isfail in two courses=n);printf(HNO.);for(i=0;i5;i+)printf(%lls,coursei);printf(averagen);for(i=0;i4;i+)label=0;for(j=0;j5;j+)if(*(pscore+5*i+j)=2)printf(”d,numi);for(k=0;k5;k+)printf(%11.2f,*(pscore+5*i+k);void good(char course510,intnum4,float*pscore,float aver4)找成绩优秀学生(各门8 5 以上或平均 90分以上)的函数int i,j,k,n;printf(=Students whosescore is good=n);printf(NO.);for(i=0;i5;i+)printf(%llszcoursei);printf(averagen);for(i=0;i4;i+)n=0;for(j=0;j85.0)n+;if(n=5)1 1(averi=90)printf(%d,numi);for(k=0;k5;k+)printf(%11.2f,*(pscore+5*i+k);printf(%11.2fn,averi);printf(%11.2fn,averi);)char strN;16、#include#include#define N 80void f(char*str)(long aN=0;int i=-l,j;while(*str)(if(*str=,0)&(*str=9)(if(*(str-l),9)i=i+l;*(a+i)=*(a+i)*10+(*str-0);str=str+l;)printf(N=%dn,i+l);for(j=0;j=i;j+)printf(%ldnz*(a+j);)printf(lnput String:);gets(str);f(str);)第九章:3、include ttdefine N 5struct student char num6;char name8;int score4;stuN;int main()void print(struct student stu6);int i,j;for(i=0;iN;i+)printf(ninput score ofstudent%d:n,i+l);printf(NO.:);void main()scanf(%szstui.num);printf(name:);#define N 3scanf(%s,stui.name);for(j=O;j3;j+)printf(score%d:J+l);scanf(%d”,&stui.scorej);)printf(n);)print(stu);return 0;void print(struct student stu6)int ij;printf(n NO.name scorelscore2 score3n);for(i=0;iN;i+)printf(%5s%10s/stui.num/stui.name);for(j=0;j3;j+)printf(H%9dzstui.scorej);printf(n);struct stu(int num;char name10;int score3;float aver;);main()(struct stu studentN;int ij,maxnum;float sum,max=0;for(i=0;iN;i+)(printf(lnput No:);scanf(%d%*c,&studenti.num);printf(lnput Name:);gets(studenti.name);printf(lnput Score:);sum=0;for(j=0;j3;j+)(scanf(%dz&(studenti.scorej);sum=sum+studenti.scorej;5、#include struct nodestudenti.aver=sum/3;if(studenti.avermax)(max=studenti.aver;maxnum=i;)for(i=0;iN;i+)(printf(%5d%10s,studenti.numzstudenti.name);for(j=0;j3;j+)printf(%5d,studenti.scorej);printff%lOfXnstudentin.aver);)printf(HMaxnum=%d/maxnum);printf(,%5d%10s/studentmaxnum.num,studentmaxnum.name);for(j=0;j3;j+)printf(%5d”,studentmaxnum.score。);printf(%10fn,studentmaxnum.aver);12、/include int num;char name10;char sex;int age;struct node*next;);main()(int num,age;struct node*head,*p,*temp;void print(struct node*head);void f(struct node*head,int age);head=(struct node*)malloc(sizeof(structnode);head-num=0;head-next=NULL;p=head;scanf(,%d%*c/&num);while(num)(temp=(struct node*)malloc(sizeof(structnode);temp-num=num;#include gets(temp-name);scanf(%c,&(temp-sex);while(head-next)scanf(H%dz&temp-age);temp-next=p-next;p-next=temp;p=p-next;scanf(%d%*c,&num);)print(head);printf(lnput age:);scanf(”%d”,&age);f(head,age);print(head);)void print(struct node*head)(head=head-next;while(head)printf(%5d%10s%3c%3d,head-num,head-name/head-sex/head-age);head=head-next;)void f(struct node*headjnt age)if(head-next-age=age)(p=head-next;head-next=p-next;free(p);)elsehead=head-next;)第十章3、include include#include#define N 80main()(FILE*fp;char strN;int i=0;printf(lnput string:);struct node*p;while(stri=getchar()!=!*)if(stri=a&stri=z)stri=stri-32;i+;stri=O;if(fp=fopen(,test,;w)=NULL)(printf(can not open filen);exit(O);)fputs(str,fp);fclose(fp);9、#include#include#include struct emploeechar num6;char name10;char sex2;int age;int salary;char health8;char class10;em10;struct empchar name10;int salary;em_case10;int main()FILE*fpl,*fp2;int ij;if(fpl=fopen(emploeezr)=NULL)printf(can not open file.n);exit(O);)printf(n NO.name sex age addrsalary health classn);for(i=0;fread(&emi,sizeof(structemploee),l,fpl)!=0;i+)printf(n%4s%8s%4s%6d%10s%6d%10s%8snum,emi.name,emi.sex,emi.age,emi.addr,emi.salary,emi.health,emi.class);char addr20;strcpy(em_casei.name,emi.name);em_casei.salary=emi.salary;printf(nn*if(fp2=fopen(emp_salaryzwb)=NULL)printf(can not open filen);exit(O);)for(j=O;ji;j+)if(fwrite(&em_casej,sizeof(structemp),fp2)!=l)printf(error!);printf(n%12s%10d,em_casej.namezem_casej.salary);)printf(Hn*ii j,fclose(fpl);fclose(fp2);return 0;