完整版C语言程序设计第四版第九章答案谭浩强Word文档下载推荐.docx
《完整版C语言程序设计第四版第九章答案谭浩强Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《完整版C语言程序设计第四版第九章答案谭浩强Word文档下载推荐.docx(16页珍藏版)》请在冰豆网上搜索。
![完整版C语言程序设计第四版第九章答案谭浩强Word文档下载推荐.docx](https://file1.bdocx.com/fileroot1/2022-12/16/eaad0420-5fb5-4910-9513-499d05b28da1/eaad0420-5fb5-4910-9513-499d05b28da11.gif)
&
date.year%100!
=0||date.year%400==0)&
date.month>
=3)days+=1;
printf(“\n%d/%disthe%dthdayin%d.”,date.month,data.day,days,date,year);
9.2写一个函数days,实现上面的计算。
由主函数将年、月、日传递给days函数,计算后将日数传回主函数输出。
structy_m_d
{intyear:
intdays(structy_m_ddate1)
{intsum;
switch(data.month)
sum=date1.day;
case2:
sum=date1.day+31;
case3:
sum=date1.day+59;
case4:
sum=date1.day+90;
case5:
sum=date1.day+120;
sum=date1.day+151;
case7:
sum=date1.day+181;
case8:
sum=date1.day+212;
sum=date1.day+243;
break
};
9.3编写一个函数print,打印一个学生的成绩数,该数组中有5个学生的数据记录,每个记录包括num、name、sore[3],用主函数输入这些记录,用print函数输出这些记录。
#defineN5
structstudent
{charnum[6];
charname[8];
intscore[4];
}stu[N];
{intI,j;
for(I=0;
I<
N;
I++)
{printf(“\Inputscoreofstudent%d:
\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);
printf(“%9d”,stu[i].score[j]);
print(“\n”);
9.4在上题的基础上,编写一个函数input,用来输入5个学生的数据记录。
#defineN5
structstudent
{charnum[6];
intscore[4]
input(structstudentstu[])
for(I=0;
{printf(“inputscoresofstudent%d:
printf(“NO.:
scanf(“%s”,stu[i].num);
printf(“name:
”);
scanf(“%s”,stu[i].name);
for(j=0;
{printf(“score%d:
”,j++);
scanf(“%d”,&
9.5有10个学生,每个学生的数据包括学号、姓名、3门课的成绩,从键盘输入10个学生的数据,要求打印出3门课的总平均成绩,以及最高分的学生的数据(包括学号、姓名、3门课成绩)
#defineN10
{charnum[6]
charname[8]
floatavr;
{intI,j,max,maxi,sum;
floataverage;
{printf(“\nInputscoresofstudent%d:
printf(“NO.:
printf(“name”);
{printf(“score%d:
”,j+1);
average=0;
max=0;
maxi=0;
for(i=0;
i<
i++)
{sum=0;
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.namescore1score2score3average\n”);
{printf(“%5s%10s”,stu[i].num,stu[i].name);
printf(“%9d”,stu[i].score[j]);
printf(“%8.2f\n”,stu[i].avr);
printf(“average=%6.2f\n”,average);
printf(“Thehighestscoreis:
%s,scoretotal:
%d.”stu[maxi].name,max);
9.6编写一个函数new,对n个字符开辟连续的存储空间,此函数应返回一个指针,指向字符串开始的空间。
New(n)表示分配n个字节的内存空间。
new函数如下:
#defineNULL0
#defineNEWSIZE1000
charnewbuf[NEWSIZE];
char*newp=newbuf;
char*new(intn)
{if(newp+n<
=newbuf+NEWSIZE)
{newp=newp+n;
return(newp-n);
else
return(NULL);
9.7写一函数free,将上题用new函数占用的空间释放。
Free(p)表示将p指向的单元以后的内存段释放。
#defineNullo
#defineNEWSIZE1000
free(char*p)
{if((p>
=newbuf)&
(p<
newbuf+NEWSIZE))
newp=p;
9.8已有a、b亮光链表,每个链表中的结点包括学好、成绩。
要求把两个链表合并,按学号升序排列。
#include<
stdio.h>
#defineLENsizeof(structstudent)
strutstudent
{longnum;
intscor;
structstudent*next
structstudentlistA,listB;
intn,sum=0;
{structstudent*creat(void);
structstudent*insert(structstudent*,structstudent*);
voidprint(structstudent*);
stuctstudent*ahead,*bhead,*abh;
printf(“\ninputlista:
\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:
printf(“ifnumberIs0,stopinputing.\n”);
scanf(“%ld,%d”,&
p1->
num,&
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”,&
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->
pb2->
next=pa1;
pa2=pb2;
pb2=pb1;
while((pa1->
=NULL)||(pa1==NULL&
pb1!
=NULL));
if((pb1->
next==NULl))
ap1->
return(ah);
voidprint(structstudent*head)
{structstudent*p;
printf(“%ld%d\n”,p->
num,p->
p=p->
while(p!
=NULL);
9.913个人围成一圈,从第1个人开始顺序报号1、2、3。
凡报到“3”者退出圈子。
找出最后留在圈子中的人原来的序号。
#defineN13
structperson
{intnumber;
intnextop;
}link[N+1];
{intI,count,h;
for(I=1;
=N;
{if(I==N)
link[i].nextp=1;
else
link[i].nextp=I+1;
link[i].number=I;
count=0;
h=N;
printf(“sequencethatperson2leavethecircle:
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(“\nThelastoneis”);
ii<
if(link[i].number)
printf(“%3d”,lin[i].number);
9.10有两个链表a和b,设结点中包含学号、姓名。
从1链表中删去与b链表中有相同学号的那些结点。
#defineLA4
#defineLB5
{charnump[6];
structstudent*next;
}A[LA],b[LB];
{structstudenta[LA]={{“101”,”Wang”},{“102”,”LI”},{“105”,”zhang”},{“106”,”Wei”}};
structstudentb[LB]={{“103”,”Zhang”},{“104”,”Ma”},{“105”,”Chen”},{“107”,”Guo”},
{“108”,”Lui”}};
intI,j;
structstudent*p,*p1,*p2,*pt,*head1,*head2;
head1=a;
head2=b;
printf(“lista:
for(p1=head1,i=1;
p1<
a=LA;
{p=p1;
next=a+I;
printf(“%8s%8s\n”,p1->
num,p1->
name);
p1=p1->
p->
printf(“\nlistb:
for(p2=head2,I=1;
p2<
b+LB;
{p=p2;
next=b+I;
printf(“%8s%8s\n”,p2->
num,p2->
p2=pa->
p1=head1;
while(p1!
=NULL)
{p2=head2;
while(p2!
=NULL&
strcmp(p1->
num)!
p2=p2->
if(strcmp(p1->
num==0))
if(p1==head1)
head1=p1->
else
p->
next=p1->
p=p1;
p1=hedad1;
printf{“\nresult:
\n”};
{printf(“%7s%7s\n”,p1->
9.11建立一个链表,每个结点包括:
学号、姓名、性别、年龄。
输入一个年龄,如果链表中的结点所包含的年龄等于此年龄,则将此结点删去。
#defineLENsizeof(structstudent)
charsex[2];
intage;
stuctstudent*next;
}stu[10];
{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;
lenth;
{p=(structstudent*)malloc(LEN);
if(I==0)
head=pt=p;
pt->
next=p;
pt=p;
ptintf(“NO:
scanf(“%s”,p->
num);
prntf(“name:
scanf(“%s”,p->
printf(“sex:
sex);
printf(“age:
age);
p=head;
printf(“\nNO.namesexage\n”);
{printf(“%4s%8s%6s%6d\n”,p->
num,p->
name,p->
sex,p->
p=p->
printf(“Inputage:
iage);
pt=head;
p=pt;
if(pt->
age==iage)
{p=pt->
head=pt=p;
find=1;
else
pt=pt->
while(pt!
{if(pt->
{p->
next=pt->
elsep=pt;
if(!
find)
printf(“Notfound%d.”,iage);
p=head;
printf(“\nNO.namesexage\n”);
while(p!
{
printf(“%4s%8s”,p->
printf(“%6s%6d”,p->
sex,p->
9.12将一个链表按逆序排列,即将链头当链尾,链尾当链头。
#defineNULL0
structstu
{intnum;
structstu*next;
{intlen=1l
structstu*p1,*p2,*head,*new,*newhead;
p1=p2=head=(structstu*)malloc(sizeof(strctstu));
printf(“inputnumber(0:
listend):
=o)
{p1=(structstu*)malloc(sizeof(structstu));
printf(“inputnumber(n:
listend):
if(p1->
num==0)
p2->
next=null;
{p2=>
next=p1;
len++;
p1=head;
pritnf(“\ntheoriginallist:
do
{printf(“%4d”,p1->
{p2=p1;
if(I==0)
newhead=new=p1;
new=nes->
printf(\n\nThenewlistL\n);
p1=newhead;
for(I=0l;
len;
{pritf(“4d,p1->
num”);
next,