(完整版)C语言程序设计第四版第九章答案谭浩强_第1页
(完整版)C语言程序设计第四版第九章答案谭浩强_第2页
(完整版)C语言程序设计第四版第九章答案谭浩强_第3页
(完整版)C语言程序设计第四版第九章答案谭浩强_第4页
(完整版)C语言程序设计第四版第九章答案谭浩强_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第九章9.1定义一个结构体变量 (包括年、月、日)。计算该日在本年中是第几天,

注意闰年问题。解:Struct{intyear;intmonth;intday;}date;main(){intdays;printf( “Inputyear,month,day: ”);scanf(“%d,%D,%d”,&date.year,&date.month,&date.day);switch(date.month){case1:days=date.day; break;case2:days=date.day+31; break;case3:days=date.day+59; break;case4:days=date.day+90; break;case5:days=date.day+120; break;case6:days=date.day+31; break;case7:days=date.day+181; break;case8:days=date.day+212; break;case9:days=date.day+243; break;case10:days=date.day+273; break;case11:days=date.day+304; break;case12:days=date.day+334; break;}if((date.year%4==0&&date.year%100!=0||date.year%400==0)&&date.month>=3)days+=1;printf(

n%d/%d“isthe%dthdayin%d.

”,date.month,data.day,days,date,year);}9.2写一个函数

days,实现上面的计算。由主函数将年、月、日传递给

days

函数,计算后将日数传回主函数输出。解:structy_m_d{intyear:intmonth;intday;}date;intdays(structy_m_ddate1){intsum;switch(data.month){case1:sum=date1.day;case2:sum=date1.day+31;case3:sum=date1.day+59;case4:sum=date1.day+90;case5:sum=date1.day+120;case6:sum=date1.day+151;

break;break;break;break;break;break;case7:sum=date1.day+181; break;case8:sum=date1.day+212; break;case9:sum=date1.day+243; breakcase10:sum=date1.day+243; breakcase11:sum=date1.day+243; breakcase12:sum=date1.day+243; break}};9.3编写一个函数 print,打印一个学生的成绩数,该数组中有录包括num、name、sore[3],用主函数输入这些记录,用解:

5个学生的数据记录,每个记print 函数输出这些记录。#defineN5structstudent{charnum[6];charname[8];intscore[4];}stu[N];main(){intI,j;for(I=0;I<N;I++){printf(

n”,I+1);printf(

“no.:

”);scanf(

“%s”,stu[i].num);printf(

“name:”);scanf( “%s”,stu[i].name);for(j=0;j<3;j++){printf( “score%d:”j+1);scanf( “%d”,&stu[i].score[j]);}printf( n“”);}print(stu);}print(structstudentstu[6]){intI,j;printf( “%5s%10s”,stu[i].num,stu[i].name);for(j=0;j<3;j++)printf(

“%9d”,stu[i].score[j]);print(

“n”);}9.4在上题的基础上,编写一个函数

input,

用来输入

5个学生的数据记录。解:#defineN5structstudent{charnum[6];charname[8];intscore[4]}stu[N];input(structstudentstu[]){intI,j;for(I=0;I<N;I++){printf( “inputscoresofstudent n”,I+1);printf( “NO.:”);scanf( “%s”,stu[i].num);printf( “name: ”);scanf( “%s”,stu[i].name);for(j=0;j<3;j++){printf( “score%d:”,j++);scanf( “%d”,&stu[i].score[j]);}}printf( n“”);}}9.5有10个学生,每个学生的数据包括学号、姓名、 3门课的成绩,从键盘输入 10个学生的数据,要求打印出 3门课的总平均成绩,以及最高分的学生的数据(包括学号、姓名、 3门课成绩)解:#defineN10structstudent{charnum[6]charname[8]intscore[4]floatavr;}stu[N];main(){intI,j,max,maxi,sum;floataverage;for(I=0;I<N;I++){printf(

n”,I+1);printf( “NO.:”);scanf( “%s”,stu[i].num);printf( “name”);scanf( “%s”,stu[i].name);for(j=0;j<3;j++){printf( “score%d: ”,j+1);scanf( “%d”,&stu[i].score[j]);}}average=0;max=0;maxi=0;for(i=0;i<3;i++){sum=0;for(j=0;j<3;j++)sum+=stu[i].score[j];stu[i].avr=sum/3.0;average+=stu[i].avr;if(sum>max){max=sum;maxi=I;}}average/=N;printf( “NO.namescore1score2score3averagen”);for(I=0;I<N;I++){printf( “%5s%10s”,stu[i].num,stu[i].name);for(j=0;j<3;j++)printf(printf(

“%9d”,stu[i].score[j]);“%8.n2f”,stu[i].avr);}printf(printf(

“average=%6.2fn”,average);“Thehighestscoreis:%s,scoretotal:%d.

”stu[maxi].name,max);}9.6编写一个函数new,对n个字符开辟连续的存储空间,此函数应返回一个指针,指向字符串开始的空间。New(n)表示分配n个字节的内存空间。解:new函数如下:#defineNULL0#defineNEWSIZE1000charnewbuf[NEWSIZE];char*newp=newbuf;char*new(intn){if(newp+n<=newbuf+NEWSIZE){newp=newp+n;return(newp-n);}elsereturn(NULL);}9.7写一函数 free,将上题用 new函数占用的空间释放。 Free(p)表示将p指向的单元以后的内存段释放。解:#defineNullo#defineNEWSIZE1000charnewbuf[NEWSIZE];char*newp=newbuf;free(char*p){if((p>=newbuf)&&(p<newbuf+NEWSIZE))newp=p;}9.8已有a、b亮光链表,每个链表中的结点包括学好、成绩。要求把两个链表合并,按学号升序排列。解:#include<stdio.h>#defineNULL0#defineLENsizeof(structstudent)strutstudent{longnum;intscor;structstudent*next};structstudentlistA,listB;intn,sum=0;main(){structstudent*creat(void);structstudent*insert(structstudent*,structstudent*);voidprint(structstudent*);stuctstudent*ahead,*bhead,*abh;printf( “ n”);ahead=creat();sum=sum+|n;abh=insert(ahead,bhead);print(abh);}structstudent*creat(void){structstudent*p1,*p2,*head;n=0;p1=p2=(structstudent*)malloc(LEN);printf( “inputnumber&scoresofstudent:n”);printf( “ifnumberIs0,stopinputing.n”);scanf( “%ld,%d”,&p1->num,&p1->score);head=NULL;while(p1->num!=0){n=n+1;if(n==1)head=p1;elsep2->next=p1;p2=p1;p1=(structstudent*)malloc(LEN);scanf( “%ld,,%d”,&p1->num,&p1->score);}p2->next=NULL;return(head);}structstudent*insert(structstudent*ah,structstudent*bh){structstudent*pa1,*pa2,*pb1,*pb2;pa2=pa1=ah;pb2=pb1=bh;do{while((pb1->num>pa1->num)&&(pa1->next!=NULL)){pa2=pa1;pa1=pa1->next;}if(pb->num<=pa1->num){if(ah=pa1)ah=pb1;elsepa2->next=pb1;pb1=pb1->next;pb2->next=pa1;pa2=pb2;pb2=pb1;}}while((pa1->next!=NULL)||(pa1==NULL&&pb1!=NULL));if((pb1->num>pa1->num)&&(pa1->next==NULl))ap1->next=pb1;return(ah);}voidprint(structstudent*head){structstudent*p;printf( “%ld%dn”,p->num,p->score);p=p->next;while(p!=NULL);}9.913个人围成一圈,从第

1个人开始顺序报号

1、2、3。凡报到“

3”者退出圈子。找出最后留在圈子中的人原来的序号。解:#defineN13structperson{intnumber;intnextop;}link[N+1];main(){intI,count,h;for(I=1;I<=N;I++){if(I==N)link[i].nextp=1;elselink[i].nextp=I+1;link[i].number=I;}printf( n“”);count=0;h=N;printf( “sequencethatperson2leavethecircle:n”);while(count<N-1){I=0;while(I!=3){h=link[h].nextp;if(link[h].number)I++;}printf( “%4d”,link[h].number);link[h].number=0;count++;}printf( \nThe“lastoneis”);for(I=1;ii<=N;I++)if(link[i].number)printf( “%3d”,lin[i].number);}9.10有两个链表

a和

b,设结点中包含学号、姓名。从

1链表中删去与

b链表中有相同学号的那些结点。解:#defineLA4#defineLB5#defineNULL0structstudent{charnump[6];charname[8];structstudent*next;}A[LA],b[LB];main(){structstudenta[LA]={{structstudentb[LB]={{

“101”,”Wang”},{“103”,”Zhang”},{

“102”,”LI”},{“104”,”Ma”},{

“105”,”zhang”},{ “106”,”“105”,”Chen”},{ “107”,”Gu{“108”,”Lui”}};intI,j;structstudent*p,*p1,*p2,*pt,*head1,*head2;head1=a;head2=b;printf( “listan”:);for(p1=head1,i=1;p1<a=LA;i++){p=p1;p1->next=a+I;printf( “%8s%8sn”,p1->num,p1->name);p1=p1->next;}p->next=NULL;printf( “ n”);for(p2=head2,I=1;p2<b+LB;I++){p=p2;p2->next=b+I;printf(“%8s%8sn”,p2->num,p2->name);p2=pa->next;}p->next=NULL;printf( n“”);p1=head1;while(p1!=NULL){p2=head2;while(p2!=NULL&&strcmp(p1->num,p2->num)!=0)p2=p2->next;if(strcmp(p1->num,p2->num==0))if(p1==head1)head1=p1->next;elsep->next=p1->next;p=p1;p1=p1->next;}p1=hedad1;printf{ “ n”};while(p1!=NULL){printf( “%7s%7sn”,p1->num,p1->name);p1=p1->next;}}9.11建立一个链表,每个结点包括:学号、姓名、性别、年龄。输入一个年龄,如果链表中的结点所包含的年龄等于此年龄,则将此结点删去。解:#defineNULL0#defineLENsizeof(structstudent)structstudent{charnum[6];charname[8];charsex[2];intage;stuctstudent*next;}stu[10];main(){structstudent*p,*pt,*head;intI,length,iage,flag=1;intfind=0;while(flag==1){printf(“inputlengthoflist (<10):”);scanf( “%d”,&length);if(length<10)flag=0;}for(I=0;I<lenth;I++){p=(structstudent*)malloc(LEN);if(I==0)head=pt=p;elsept->next=p;pt=p;ptintf( “NO:”);scanf( “%s”->num);,pprntf( “name:”);scanf( “%s”->name);,pprintf( “sex:”);scanf( “%s”->sex);,pprintf( “age:”);scanf( “%s”->age);,p}p->next=NULL;p=head;printf( “ n”);while(p!=NULL){printf( “%4s%8s%6s%6dn”,p->num,p->name,p->sex,p->age);p=p->next;}printf( “Inputage: ”);scanf( “%d”,&iage);pt=head;p=pt;if(pt->age==iage){p=pt->next;head=pt=p;find=1;}elsept=pt->next;while(pt!=NULL){if(pt->age==iage){p->next=pt->next;find=1;}elsep=pt;pt=pt->next;}if(!find)printf( “Notfound%d. ”,iage);p=head;printf( /r/

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论