C语言 课后题答案北邮出版社Word格式.docx
《C语言 课后题答案北邮出版社Word格式.docx》由会员分享,可在线阅读,更多相关《C语言 课后题答案北邮出版社Word格式.docx(15页珍藏版)》请在冰豆网上搜索。
}TP;
typedefstructstudents
{charsid[15];
charname[15];
TPscore;
structstudents*next;
student*input()
{student*head,*p1,*p2;
intn=0;
charch;
clrscr();
head=(student*)malloc(F);
head->
next=NULL;
do
{n++;
\n\nPleaseinput%dstudentmessage:
\n\n"
n);
\t%dstudentsid:
p1=(student*)malloc(F);
p1->
p1->
sid);
\n\t%dstudentname:
name);
\n\t%dstudentscores(englesh,math,c_language):
%d,%d,%d"
;
p1->
=p1->
+p1->
if(n==1)
{head->
next=p1;
p2=p1;
}
else
{p2->
p2=p1;
\n\n\t\t\tContinueorback(pressy/n):
ch=getch();
}while(ch=='
y'
||ch=='
Y'
returnhead;
voidaverage1(student*head)
{student*p;
intj;
p=head->
next;
while(p)
{j=p->
3;
printf("
\n\nname:
%s\taverage:
%d"
p->
name,j);
p=p->
\n\n\nPressenykeyreturn."
voidaverage2(student*head)
student*p;
intn=0,temp1=0,temp2=0,temp3=0;
{temp1+=p->
temp2+=p->
temp3+=p->
n++;
\n\naverageenglishis:
%d\naveragemathis:
%d\naveragec_languageis:
%d\t"
temp1/n,temp2/n,temp3/n);
student*sort(student*head)
{student*head1,*p,*q,*r;
inttemp1=0,temp2=0,temp3=0,temp4;
chars[15],n[15];
head1=head;
for(p=head1->
p->
next!
=NULL;
p=p->
next)
{r=p;
for(q=p->
q;
q=q->
if(q->
r->
r=q;
if(r!
=p)
{strcpy(s,p->
strcpy(n,p->
temp1=p->
temp2=p->
temp3=p->
temp4=p->
strcpy(p->
sid,r->
strcpy(p->
name,r->
p->
=r->
strcpy(r->
sid,s);
strcpy(r->
name,n);
r->
=temp1;
=temp2;
=temp3;
=temp4;
}
returnhead1;
voidoutput(student*head)
{student*head2,*p;
inti=1;
head2=sort(head);
for(p=head2->
p!
%s\tsid:
%s\tenglish:
%d\tmath:
%d\tc_language:
%d\taverage:
%d\tmingci:
name,p->
sid,p->
3,i++);
average2(head);
\n\n\n\t\tPressenykeyback."
inti=0,j=1;
head=input();
{clrscr();
\n\n
(1):
average1.\n\n
(2):
average2.\n\n(3):
sort.\n\n(4):
output.\n\n\nPleasechoose:
scanf("
%d"
i);
switch(i)
{case1:
average1(head);
break;
case2:
average2(head);
\n\n\nPressenykeyretuen."
getch();
case3:
p1=sort(head);
for(p2=p1->
p2!
p2=p2->
next)printf("
\n\t\tname:
%s\tmingci:
p2->
name,j++);
printf("
\n\n\nPressenykeyback."
case4:
output(head);
default:
\nYourchooseisnotright."
break;
}while(i!
=-1);
4、
#defineFsizeof(worker)
typedefstructwork
intmoney;
structwork*next;
}worker;
intmin=0,max=0;
chara[15],b[15];
worker*input()
{worker*head,*p,*q;
intn=0;
head=(worker*)malloc(F);
head->
next=0;
p=(worker*)malloc(F);
p->
\n\n\tPleaseinput%dworkermessage:
\n%dworkersid:
scanf("
\n%dworkername:
\n%dworkermoney:
money);
if(n==1)
{head->
next=p;
q=p;
max=p->
money;
strcpy(a,p->
min=p->
strcpy(b,p->
else
{q->
if(p->
money>
max){max=p->
money<
min){min=p->
q=p;
\n\t\ty/n"
ch=getch();
voidoutput()
{clrscr();
\nThemaxmoneyis:
%d\t\tnameis:
%s\n\n"
max,a);
\nTheminmoneyis:
%s"
min,b);
{
input();
output();
5、6、
#include"
"
#defineFsizeof(stu)
typedefstructstudent
intsid;
intaverage;
structstudent*next;
}stu;
stu*head;
stu*create()
{stu*p1,*p2;
head=(stu*)malloc(F);
p1=(stu*)malloc(F);
\n\t%dstudentaverage:
average);
stu*select(stu*head,intx)
stu*s;
s=head->
while(s)
{
if(s->
sid==x)
s=s->
returns;
stu*insert(stu*head,intx,inty)
stu*p,*r,*q;
r=(stu*)malloc(sizeof(stu));
r->
sid=x;
average=y;
if(p==NULL)/*如果插入空表*/
p=r;
\ninsertsuccess!
!
{while(x>
sid)/*找到插入的位置,按学号大小。
(找到位置或者到了表尾都会跳出循环)*/
{if(p->
next==NULL)break;
p=p->
if(x<
sid)/*插到中间位置*/
{r->
sid=p->
sid;
average=p->
average;
next=p->
next=r;
elseif(x==p->
sid)/*学号不能相同*/
\nError--->
yourinputthissamesid."
else/*插到末尾*/
{p->
stu*get(stu*head,intn)/*得到位置为n的结点的指针*/
{stu*p;
inti;
if(n==0)returnhead;
else
{for(i=1;
i<
n;
i++)
returnp;
stu*delete(stu*head,intsid)
stu*p,*q;
inttemp=0,i=0;
if(!
p)
{printf("
\nlistisempty.pressenykeyback."
returnhead;
}/*表空*/
{while(p)/*查找学号为sid的结点的指针*/
{i++;
/*标记学号为sid的结点的位置*/
sid==sid)
{temp=1;
}/*temp=1标记找到了*/
if(temp==1)/*如果有学号为sid的结点*/
{q=get(head,i-1);
/*得到sid的前一个结点的指针*/
q->
free(p);
printf("
\n\ndeletesucess!
returnhead;
else/*没有找到*/
{printf("
\n\nNOthisdata.\n"
voidprint(stu*head)
stu*p;
p){printf("
\n%d:
\t%d"
main()
stu*p1,*p2;
charch1;
intn,i=0,j=0;
head=create();
{clrscr();
\n1.insert."
\n2.select."
\n3.delect."
\n4.printlist."
\n5.EXIT"
\n............choice(1-5)............."
ch1=getch();
switch(ch1)
case'
1'
:
{clrscr();
\npleaseinputinsertsid.andaverage(like1,1):
%d,%d"
i,&
j);
head=insert(head,i,j);
break;
2'
\ninputyouwanttoseletesid:
n);
p1=select(head,n);
{if(p1)printf("
\nsid:
%d\taverage:
sid,p1->
elseprintf("
\nNothisdata."
3'
\nPleaseinputyouwantdeletesid:
head=delete(head,n);
4'
Allinformation:
print(head);
5'
return;
default:
\n\nYourenterisnotright.pressenykeyback."
}while(n);
}
7、
#defineFsizeof(L)
typedefstructlist
{chardata;
structlist*next;
}L;
L*set_list()
{L*head,*p1,*p2;
charc;
head=(L*)malloc(F);
/*建立链表*/
p1=p2=head;
\nPleaseinputchar(press*finish):
%c"
c);
while(c!
='
*'
)
data=c;
{p1=(L*)malloc(F);
p2->
next=p1;
p2=p1;
next=0;
p1=head;
while(p1)
%c"
data);
p1=p1->
\n\n\n"
voidchange_list(L*head1)/*算法:
p2指向最后一个元素,p1指向第一个元素。
交换他们的值,p1,p2同时往中间靠拢。
*/
{L*p1,*p2,*p3;
inti,j,k,n=1;
chartemp;
p1=head1;
p2=head1;
p3=head1;
while(p3->
{p3=p3->
}/*求链长*/
for(i=n;
i>
(n/2);
i--)/*外循环使p1后移,p2前移。
{p2=head1;
for(j=1;
j<
i;
j++)
p2=p2->
/*p2指向最后一个元素*/
temp=p1->
data;
data=p2->
p2->
data=temp;
/*交换他们的值*/
p1=p1->
/*p1向后移*/
while(head1)
head1->
head1=head1->
{L*head;
head=set_list();
change_list(head);