C语言程序设计第四版第九章答案 谭浩强.docx

上传人:飞****2 文档编号:78789570 上传时间:2023-03-19 格式:DOCX 页数:11 大小:15.15KB
返回 下载 相关 举报
C语言程序设计第四版第九章答案 谭浩强.docx_第1页
第1页 / 共11页
C语言程序设计第四版第九章答案 谭浩强.docx_第2页
第2页 / 共11页
点击查看更多>>
资源描述

《C语言程序设计第四版第九章答案 谭浩强.docx》由会员分享,可在线阅读,更多相关《C语言程序设计第四版第九章答案 谭浩强.docx(11页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、第九章91定义一个结构体变量(包括年、月、日)。计算该日在本年中是第几天,注意闰年问题。解:Structint year;int month;int day;date;main()int days; printf(“Input year,month,day:”); scanf(“%d,%D,%d”,&date.year,&date.month,&date.day); switch(date.month)case 1: days=date.day; break; case 2: days=date.day+31; break; case 3: days=date.day+59; break; c

2、ase 4: days=date.day+90; break; case 5: days=date.day+120; break;case 6: days=date.day+31; break; case 7: days=date.day+181; break; case 8: days=date.day+212; break;case 9: days=date.day+243; break;case 10: days=date.day+273; break;case 11: days=date.day+304; break;case 12: days=date.day+334; break;

3、if(date.year%4=0&date.year%100!=0|date.year%400=0)&date.month=3)days+=1;printf(“n%d/%d is the %dth day in%d.”,date.month,data.day,days,date,year);9.2写一个函数days,实现上面的计算。由主函数将年、月、日传递给days 函数,计算后将日数传回主函数输出。解:struct y_m_dint year:int month;int day;date;intdays(struct y_m_d date1)int sum;switch(data.month

4、)case 1:sum=date1.day; break;case 2:sum=date1.day+31; break;case 3:sum=date1.day+59; break;case 4:sum=date1.day+90; break;case 5:sum=date1.day+120; break;case 6:sum=date1.day+151; break;case 7:sum=date1.day+181; break;case 8:sum=date1.day+212; break;case 9:sum=date1.day+243; breakcase 10:sum=date1.d

5、ay+243; breakcase 11:sum=date1.day+243; breakcase 12:sum=date1.day+243; break;9.3编写一个函数print,打印一个学生的成绩数,该数组中有5个学生的数据记录,每个记录包括num、name、sore3,用主函数输入这些记录,用print函数输出这些记录。解:#define N 5struct studentchar num6; char name8; int score4;stuN;main()int I,j ; for(I=0;IN;I+)printf(“Input score of student %d:n”,I

6、+1); printf(“no.:”); scanf(“%s”,stui.num); printf(“name:”); scanf(“%s”,stui.name); for(j=0;j3;j+) printf(“score%d:”j+1); scanf(“%d”,&stui.scorej);printf(“n”);print(stu);print(struct student stu6)int I,j; printf(“%5s%10s”,stui.num,stui.name); for(j=0;j3;j+) printf(“%9d”,stui.scorej); print(“n”);9.4在上

7、题的基础上,编写一个函数input,用来输入5个学生的数据记录。解: #define N 5 struct student char num6; char name8;int score4stuN;input(struct student stu)int I,j;for(I=0;IN;I+)printf(“input scores of student %d:n”,I+1);printf(“NO.:”);scanf(“%s”,stui.num);printf(“name: ”);scanf(“%s”, stui.name);for(j=0;j3;j+)printf(“score%d:”,j+)

8、; scanf(“%d”, &stui.scorej);printf(“n”);9.5 有10个学生,每个学生的数据包括学号、姓名、3门课的成绩,从键盘输入10个学生的数据,要求打印出3门课的总平均成绩,以及最高分的学生的数据(包括学号、姓名、3门课成绩)解:#define N 10struct studentchar num6char name8int score4float avr;stuN;main()int I,j,max,maxi,sum;float average;for(I=0;IN;I+)printf(“nInput scores of student %d:n”,I+1);

9、printf(“NO.:”); scanf(“%s”,stui.num); printf(“name”); scanf(“%s”,stui.name);for(j=0;j3;j+)printf(“score %d:”,j+1); scanf(“%d”, &stui.scorej);average=0;max=0;maxi=0;for(i=0;i3;i+)sum=0;for(j=0;jmax)max=sum;maxi=I;average/=N;printf(“NO. name score1 score2 score3 averagen”);for(I=0;IN;I+)printf(“%5s%10

10、s”,stui.num, stui.name);for(j=0;j3;j+)printf(“%9d”,stui.scorej);printf(“%8.2fn”,stui.avr);printf(“average=%6.2fn”,average);printf(“The highest score is:%s,score total:%d.”stumaxi.name,max);9.6 编写一个函数new,对n个字符开辟连续的存储空间,此函数应返回一个指针,指向字符串开始的空间。New(n)表示分配n个字节的内存空间。解:new函数如下:#define NULL 0#define NEWSIZE

11、1000char newbufNEWSIZE;char *newp=newbuf;char *new(int n)if (newp+n=newbuf)&(pnewbuf+NEWSIZE) newp=p;9.8已有a、b亮光链表,每个链表中的结点包括学好、成绩。要求把两个链表合并,按学号升序排列。解: #include#define NULL 0#define LENsizeof(struct student)strut studentlong num;int scor;struct student *next ;struct student listA,listB;int n,sum=0;ma

12、in()struct student *creat(void);struct student *insert(struct student *,struct student *);void print(struct student *);stuct student *ahead , *bhead,*abh;printf(“ninput list a:n”);ahead=creat();sum=sum+|n;abh=insert(ahead,bhead);print(abh);struct student *creat(void)struct student *p1,*p2,*head;n=0;

13、p1=p2=(struct student *)malloc(LEN);printf(“input number&scores of student:n”);printf(“if number Is 0,stop inputing.n”);scanf(“%ld,%d”,&p1-num,&p1-score);head=NULL;while(p1-num!=0)n=n+1;if(n=1)head=p1;else p2-next =p1;p2=p1;p1=(struct student *)malloc(LEN);scanf(“%ld,%d”,&p1-num,&p1-score);p2-next=N

14、ULL;return(head);struct student *insert(struct student *ah,struct student *bh)struct student *pa1 , *pa2,*pb1,*pb2;pa2=pa1=ah;pb2=pb1=bh;do while(pb1-numpa1-num)&(pa1-next!=NULL)pa2=pa1;pa1=pa1-next;if(pb-numnum)if(ah=pa1)ah=pb1;else pa2-next=pb1;pb1=pb1-next;pb2-next=pa1;pa2=pb2;pb2=pb1;while(pa1-n

15、ext!=NULL)|(pa1=NULL&pb1!=NULL);if(pb1-numpa1-num)&(pa1-next=NULl) ap1-next=pb1;return(ah);void print(struct student *head)struct student *p;printf(“%ld%dn”,p-num,p-score);p=p-next;while(p!=NULL);9.9 13个人围成一圈,从第1个人开始顺序报号1、2、3。凡报到“3”者退出圈子。找出最后留在圈子中的人原来的序号。解:#define N 13struct personint number;int nex

16、top;linkN+1;main()int I,count,h;for(I=1;I=N;I+)if(I=N) linki.nextp=1; else linki.nextp=I+1; linki.number=I;printf(“n”);count=0;h=N;printf(“sequence that person2 leave the circle:n”);while(countN-1)I=0; while(I!=3) h=linkh.nextp; if(linkh.number) I+;printf(“%4d”,linkh.number);linkh.number=0;count+;pr

17、intf(“nThe last one is”);for(I=1;ii=N;I+) if(linki.number)printf(“%3d”,lini.number);9.10有两个链表a和b,设结点中包含学号、姓名。从1链表中删去与b链表中有相同学号的那些结点。解:#define LA 4#define LB 5#define NULL 0struct studentchar nump6; char name8; struct student *next;ALA,bLB;main()struct student aLA=“101”,”Wang”,“102”,”LI”,“105”,”zhang

18、”,“106”,”Wei”;struct studentbLB=“103”,”Zhang”,“104”,”Ma”,“105”,”Chen”,“107”,”Guo”,“108”,”Lui”;int I,j;struct student *p, *p1,*p2,*pt,*head1,*head2; head1=a;head2=b;printf(“list a :n”);for(p1=head1,i=1;p1next=a+I;printf(“%8s%8sn”,p1-num,p1-name);p1=p1-next;p-next=NULL;printf(“n list b:n”);for(p2=head

19、2,I=1;p2next=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; else p-next=p1-next;p=p1;p1=p1-next;p1=hedad1;printf“n result:n”;while(p1!=N

20、ULL)printf(“%7s %7sn”,p1-num,p1-name);p1=p1-next;9.11建立一个链表,每个结点包括:学号、姓名、性别、年龄。输入一个年龄,如果链表中的结点所包含的年龄等于此年龄,则将此结点删去。解:#define NULL 0 #define LEN sizeof(struct student) struct student char num6; char name8; char sex2; int age; stuct student *next;stu10;main()struct student *p,*pt,*head;int I,length,iag

21、e,flag=1;int find=0;while(flag=1)printf(“input length of list(10):”);scanf(“%d”,&length);if(length10)flag=0;for(I=0;Inext=p; pt=p; ptintf(“NO:”); scanf(“%s”,p-num);prntf(“name:”);scanf(“%s”,p-name);printf(“sex:”);scanf(“%s”,p-sex);printf(“age:”);scanf(“%s”,p-age);p-next=NULL;p=head;printf(“n NO. nam

22、e sex agen”);while(p!=NULL)printf(“%4s%8s%6s%6dn”,p-num, p-name, p-sex, p-age); p=p-next; printf(“Input age:”);scanf(“%d”,&iage);pt=head;p=pt;if(pt-age=iage)p=pt-next;head=pt=p;find=1;else pt=pt-next;while(pt!=NULL)if(pt-age=iage)p-next=pt-next;find=1;else p=pt;pt=pt-next; if(!find)printf(“Not found

23、%d.”,iage); p=head; printf(“n NO.name sex agen”); while(p!=NULL) printf(“%4s%8s”,p-num,p-name);printf(“%6s%6d”,p-sex,p-age);p=p-next;9.12将一个链表按逆序排列,即将链头当链尾,链尾当链头。解:# define NULL 0struct stuint num; struct stu *next;main()int len=1lstruct stu *p1,*p2,*head,*new,*newhead;p1=p2=head=(struct stu * )mall

24、oc(sizeof(strct stu);printf(“input number(0:list end):”);scanf(“%d”,&p1-num);while(p1-num!=o)p1=(struct stu*)malloc(sizeof(struct stu);printf(“input number(n:listend):”);scanf(“%d”,&p1-num);if(p1-num=0) p2-next=null;elsep2=next=p1;p2=p1;len+;p1=head;pritnf(“n the original list:n”);doprintf(“%4d”,p1-num);if(p1-next!=NULL)p1=p1-next;while(p1-next!=NULL)p2=p1;p1=p1-next;if(I=0)newhead=new=p1;elsenew=nes-next=p1;p2-next=NULL;printf(nn The new listLn);p1=newhead;for(I=0l;Inum”);p1=p1-next,printf(“n”);

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 教案示例

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁