C语言课后题答案北邮出版社.docx

上传人:b****8 文档编号:29577169 上传时间:2023-07-24 格式:DOCX 页数:17 大小:17.65KB
下载 相关 举报
C语言课后题答案北邮出版社.docx_第1页
第1页 / 共17页
C语言课后题答案北邮出版社.docx_第2页
第2页 / 共17页
C语言课后题答案北邮出版社.docx_第3页
第3页 / 共17页
C语言课后题答案北邮出版社.docx_第4页
第4页 / 共17页
C语言课后题答案北邮出版社.docx_第5页
第5页 / 共17页
点击查看更多>>
下载资源
资源描述

C语言课后题答案北邮出版社.docx

《C语言课后题答案北邮出版社.docx》由会员分享,可在线阅读,更多相关《C语言课后题答案北邮出版社.docx(17页珍藏版)》请在冰豆网上搜索。

C语言课后题答案北邮出版社.docx

C语言课后题答案北邮出版社

1、

li45300.0

chang30200.0

chang

2、

#include

structstudents

{

charsid[100];

charname[100];

floatscore[3];

}student;

voidmain()

{inti;floatj;

printf("\nPleaseinputsid:

");

scanf("%s",student.sid);

printf("\nPleaseinputname:

");

scanf("%s",student.name);

printf("\nPleaseinput3score:

(like1,1,1)");/*输入逗号隔开*/

scanf("%f,%f,%f",&student.score[0],&student.score[1],&student.score[2]);printf("\nsid=%s",student.sid);

printf("\nname=%s",student.name);

j=(student.score[0]+student.score[1]+student.score[2])/3.0;

printf("\naverage=%.2f",j);

getch();

}

3、

#include

#include

#defineFsizeof(student)

#defineNULL0

typedefstructscores

{intenglish;

intmath;

intc_language;

intall;

}TP;

typedefstructstudents

{charsid[15];

charname[15];

TPscore;

structstudents*next;

}student;

student*input()

{student*head,*p1,*p2;

intn=0;charch;

clrscr();

head=(student*)malloc(F);head->next=NULL;

do

{n++;

printf("\n\nPleaseinput%dstudentmessage:

\n\n”,n);

printf("\t%dstudentsid:

”,n);

p1=(student*)malloc(F);p1->next=NULL;

scanf("%s",p1->sid);

printf("\n\t%dstudentname:

”,n);

scanf("%s",p1->name);

printf("\n\t%dstudentscores(englesh,math,c_language):

",n);

scanf("%d,%d,%d",&p1->score.english,&p1->score.math,&p1->score.c_language);p1->score.all=p1->score.english+p1->score.math+p1->score.c_language;

if(n==1)

{head->next=p1;p2=p1;}

else

{p2->next=p1;

p2=p1;

}

printf("\n\n\t\t\tContinueorback(pressy/n):

");

ch=getch();

}while(ch=='y'||ch=='Y');

returnhead;

}

voidaverage1(student*head)

{student*p;intj;

clrscr();

p=head->next;

while(p)

{j=p->score.all/3;

printf("\n\nname:

%s\taverage:

%d",p->name,j);

p=p->next;

}

printf("\n\n\nPressenykeyreturn.");

getch();

}

voidaverage2(student*head)

{

student*p;intn=0,temp1=0,temp2=0,temp3=0;

p=head->next;

while(p)

{temp1+=p->score.english;

temp2+=p->score.math;

temp3+=p->score.c_language;

p=p->next;n++;

}

printf("\n\naverageenglishis:

%d\naveragemathis:

%d\naveragec_language

is:

%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];

head仁head;

for(p=head1->next;p->next!

=NULL;p=p->next)

{r=p;

for(q=p->next;q;q=q_>next)

if(q->score.all>r->score.all)

r=q;

if(r!

=p)

{strcpy(s,p->sid);strcpy(n,p_>name);

temp1=p->score.english;

temp2=p->score.math;

temp3=p->score.c_language;

temp4=p->score.all;

strcpy(p->sid,r->sid);strcpy(p->name,r->name);

p->score.english=r->score.english;

p->score.math=r->score.math;

p->score.c_language=r->score.c_language;

p->score.all=r->score.all;

strcpy(r->sid,s);strcpy(r->name,n);

r->score.english=temp1;

r->score.math=temp2;

r->score.c_language=temp3;

r->score.all=temp4;

}

}

returnhead1;

}

voidoutput(student*head)

{student*head2,*p;inti=1;

clrscr();

head2=sort(head);

for(p=head2->next;p!

=NULL;p=p->next)

printf("\n\nname:

%s\tsid:

%s\tenglish:

%d\tmath:

%d\tc_language:

%d\taverage:

%d\tmingci:

%d

",p->name,p->sid,p->score.english,p->score.math,p->score.c_language,p->score.all/3,i++);average2(head);

printf("\n\n\n\t\tPressenykeyback.");

getch();

}

voidmain()

{student*head,*p1,*p2;

inti=0,j=1;

head=input();

do

{clrscr();

printf("\n\n

(1):

average1.\n\n

(2):

average2.\n\n(3):

sort.\n\n(4):

output.\n\n\n

Pleasechoose:

");

scanf("%d",&i);

switch(i)

{case1:

average1(head);break;

case2:

clrscr();average2(head);printf("\n\n\nPressenykeyretuen.");getch();

break;

case3:

clrscr();p1=sort(head);for(p2=p1->next;p2!

=NULL;p2=p2->next)

printf("\n\t\tname:

%s\tmingci:

%d",p2->name,j++);printf("\n\n\nPressenykeyback.");getch();

break;

case4:

output(head);break;

default:

printf("\nYourchooseisnotright.");break;

}

}while(i!

=-1);

}

4、

#include

#include

#defineNULL0

#defineFsizeof(worker)

typedefstructwork

{charsid[15];

charname[15];

intmoney;

structwork*next;

}worker;

intmin=0,max=0;

chara[15],b[15];

worker*input()

{worker*head,*p,*q;intn=0;

charch;

head=(worker*)malloc(F);head->next=0;

do

{n++;

p=(worker*)malloc(F);p->next=0;

printf("\n\n\tPleaseinput%dworkermessage:

",n);

printf("\n%dworkersid:

”,n);scanf("%s",p->sid);

printf("\n%dworkername:

”,n);scanf("%s",p->name);

printf("\n%dworkermoney:

",n);scanf("%d",&p->money);

if(n==1)

{head->next=p;q=p;

max=p->money;strcpy(a,p->name);

min=p_>money;strcpy(b,p->name);

}

else

{q_>next=p;

if(p->money>max){max=p->money;strcpy(a,p->name);}if(p->moneymoney;strcpy(b,p->name);}q=p;

}

printf("\n\t\ty/n");ch=getch();

}while(ch=='y'||ch=='Y');

returnhead;

}

voidoutput()

{clrscr();

printf("\nThemaxmoneyis:

%d\t\tnameis:

%s\n\n",max,a);

printf("\nTheminmoneyis:

%d\t\tnameis:

%s",min,b);

}

voidmain()

{

input();

output();getch();

}

5、6、

#include"stdio.h"

#defineFsizeof(stu)

#defineNULL0

typedefstructstudent

{

intsid;

intaverage;

structstudent*next;

}stu;stu*head;

stu*create()

{stu*p1,*p2;

intn=0;charch;

head=(stu*)malloc(F);head->next=NULL;

do

\n\n”,n);

");

{n++;

printf("\n\nPleaseinput%dstudentmessage:

printf("\t%dstudentsid:

”,n);

p1=(stu*)malloc(F);p1->next=NULL;

scanf("%d",&p1->sid);

printf("\n\t%dstudentaverage:

",n);

scanf("%d",&p1->average);

if(n==1)

{head->next=p1;p2=p1;}

else

{p2->next=p1;

p2=p1;

}

printf("\n\n\t\t\tContinueorback(pressy/n):

ch=getch();

}while(ch=='y'||ch=='Y');

returnhead;

}

stu*select(stu*head,intx)

{

stu*s;

s=head->next;

while(s)

{

if(s->sid==x)

break;

s=s->next;

}

returns;

}stu*insert(stu*head,intx,inty)

{

stu*p,*r,*q;

clrscr();

p=head->next;

r=(stu*)malloc(sizeof(stu));r->sid=x;

r->average=y;

if(p==NULL)/*如果插入空表*/{

p=r;

r->next=NULL;

printf("\ninsertsuccess!

!

");

}

else

{while(x>p->sid)/*找到插入的位置,按学号大小。

(找到位置或者到了表尾都

会跳出循环)*/

{if(p->next==NULL)break;p=p->next;}

if(xsid)/*插到中间位置*/

{r->sid=p->sid;

r->average=p_>average;

p->sid=x;

p->average=y;

r->next=p->next;

p_>next=r;

printf("\ninsertsuccess!

!

");

}

elseif(x==p->sid)/*学号不能相同*/

printf("\nError--->yourinputthissamesid.");

else/*插到末尾*/

{p_>next=r;

r->next=NULL;

printf("\ninsertsuccess!

!

");

}

}

returnhead;

}

stu*get(stu*head,intn)/*得到位置为n的结点的指针*/

{stu*p;inti;

p=head->next;

if(n==0)returnhead;

else

{for(i=1;i

p=p->next;

returnp;

}

}

stu*delete(stu*head,intsid)

{

stu*p,*q;inttemp=0,i=0;

p=head->next;

if(!

p)

{printf("\nlistisempty.pressenykeyback.");getch();returnhead;}/*表空*/

else

{while(p)/*查找学号为sid的结点的指针*/

{i++;/*标记学号为sid的结点的位置*/

if(p->sid==sid)

{temp=1;break;}/*temp=1标记找到了*/p=p->next;

}

if(temp==1)/*如果有学号为sid的结点*/

{q=get(head,i-1);/*得到sid的前一个结点的指针*/q_>next=p->next;

free(p);

printf("\n\ndeletesucess!

!

!

");returnhead;

}

else/*没有找到*/

{printf("\n\nNOthisdata.'n");

returnhead;

}

}

}

voidprint(stu*head)

{

stu*p;

p=head->next;

if(!

p){printf("\nlistisempty.pressenykeyback.");getch();}while(p)

{

printf("\n%d:

\t%d",p->sid,p->average);

p=p->next;

}

}

main()

{

stu*p1,*p2;

charch1;

intn,i=0,j=0;

head=create();

do

{clrscr();

printf("\n1.insert.");

printf("\n2.select.");

printf("\n3.delect.");

printf("\n4.printlist.");

printf("\n5.EXIT");

printf("\nchoice(1-5)");

ch1=getch();

switch(ch1)

{

case'1':

{clrscr();

printf("\npleaseinputinsertsid.andaverage(like1,1):

");

scanf("%d,%d",&i,&j);

head=insert(head,i,j);

printf("\n\n\nPressenykeyback.");getch();

break;

}

case2:

{clrscr();

printf("\ninputyouwanttoseletesid:

");

scanf("%d",&n);

p1=select(head,n);

{if(p1)printf("\nsid:

%d\taverage:

%d",p1->sid,p1->average);elseprintf("\nNothisdata.");

}

printf("\n\n\nPressenykeyback.");getch();

break;

}

case3:

{clrscr();printf("\nPleaseinputyouwantdeletesid:

");

scanf("%d",&n);

head=delete(head,n);

printf("\n\n\nPressenykeyback.");getch();

break;

}

case'4':

{clrscr();

printf("Allinformation:

");

print(head);

printf("\n\n\nPressenykeyback.");getch();

break;

}

case'5':

return;

default:

printf("\n\nYourenterisnotright.pressenykeyback.");getch();}

}while(n);

}

7、

#include

#defineFsizeof(L)

typedefstructlist

{chardata;

structlist*next;

}L;

L*set_list()

{L*head,*p1,*p2;

charc;

intn=0;

head=(L*)malloc(F);head->next=O;/*建立链表*/

p1=p2=head;

printf("\nPleaseinputchar(press*finish):

");

scanf("%c",&c);

while(c!

='*')

{n++;

if(n==1)

p1->data=c;

else

{p1=(L*)malloc(F);

p1->data=c;

p2->next=p1;

p2=p1;

p1->next=0;

}

scanf("%c",&c);

}

p1=head;

while(pl)

{printf("%c",p1->data);p1=p1->next;}

printf("\n\n\n");

returnhead;

}

voidchange_list(L*head1)/*算法:

p2指向最后一个元素,pl指向第一个元素。

交换他

们的值,p1,p2同时往中间靠拢。

*/

{L*p1,*p2,*p3;

inti,j,k,n=1;

chartemp;

p1=head1;p2=head1;p3=head1;

while(p3->next)

{p3=p3->next;n++;}/*求链长*/

for(i=n;i>(n/2);i--)/*外循环使pl后移,p2前移。

*/

{p2=head1;

for(j=1;j

p2=p2->next;/*p2指向最后一个元素*/

temp=p1->data;p1->data=p2->data;p2->data=temp;/*交换他们的值*/p1=p1->next;/*p1向后移*/

}

while(headl)

{printf("%c”,head1->data);head仁head1->next;}

}

voidmain()

{L*head;

head=set_list();

change_list(head);

getch();

}

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 外语学习 > 其它语言学习

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1