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

上传人:李医生 文档编号:6305337 上传时间:2020-10-23 格式:DOCX 页数:11 大小:26.23KB
返回 下载 相关 举报
C语言课后题答案(北邮出版社).docx_第1页
第1页 / 共11页
C语言课后题答案(北邮出版社).docx_第2页
第2页 / 共11页
C语言课后题答案(北邮出版社).docx_第3页
第3页 / 共11页
C语言课后题答案(北邮出版社).docx_第4页
第4页 / 共11页
C语言课后题答案(北邮出版社).docx_第5页
第5页 / 共11页
点击查看更多>>
资源描述

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

1、,.1、li45300.0chang 30200.0chang2、#includestruct studentschar sid100;char name100;float score3;student;void main()int i; float j;printf(nPlease input sid:);scanf(%s,student.sid);printf(nPlease input name:);scanf(%s,student.name);printf(nPlease input 3 score:(like1,1,1);/* 输入逗号隔开*/scanf(%f,%f,%f,&stud

2、ent.score0,&student.score1,&student.score2);printf(nsid = %s,student.sid);printf(nname = %s,student.name);j=(student.score0+student.score1+student.score2)/3.0;printf(naverage = %.2f,j);getch();3、#include#include#define F sizeof(student)#define NULL 0typedef struct scoresint english; int math;int c_l

3、anguage;int all;TP;typedef struct studentschar sid15; char name15; TP score;struct students *next;student;student *input();.,.student *head,*p1,*p2;int n=0;char ch;clrscr();head=(student *)malloc(F);head-next=NULL;don+;printf(nnPlease input %d student message:nn,n);printf(t%d student sid:,n);p1=(stu

4、dent *)malloc(F);p1-next=NULL;scanf(%s,p1-sid);printf(nt%d student name:,n);scanf(%s,p1-name);printf(nt%d student scores(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-n

5、ext=p1;p2=p1; elsep2-next=p1;p2=p1;printf(nntttContinue or back (press y/n):);ch=getch();while(ch=y|ch=Y);return head;void average1(student *head)student *p;int j;clrscr();p=head-next;while(p) j=p-score.all/3;printf(nnname: %staverage: %d,p-name,j); p=p-next;printf(nnnPress eny key return.);getch();

6、void average2(student *head)student *p;int n=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(nnaverageenglishis:%dnaveragemathis:%dnaveragec_languageis : %dt,temp1/n,temp2/n,temp3/n);student *sort(student *head)

7、student *head1,*p,*q,*r; int temp1=0,temp2=0,temp3=0,temp4; char s15,n15; head1=head;for(p=head1-next;p-next!=NULL;p=p-next)r=p; for(q=p-next;q;q=q-next) if(q-score.allr-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

8、=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;

9、return head1;void output(student *head)student *head2,*p;int i=1; clrscr(); head2=sort(head);for(p=head2-next;p!=NULL;p=p-next)printf(nnname: %stsid: %stenglish: %dtmath: %dtc_language: %dtaverage: %dtmingci: %d ,p-name,p-sid,p-score.english,p-score.math,p-score.c_language,p-score.all/3,i+);average2

10、(head);.,.printf(nnnttPress eny key back.);getch();void main()student *head,*p1,*p2; int i=0,j=1; head=input();doclrscr();printf(nn(1):average1.nn(2):average2.nn(3):sort.nn(4):output.nnnPlease choose:);scanf(%d,&i);switch(i)case 1: average1(head);break;case 2: clrscr();average2(head);printf(nnnPress

11、eny keyretuen.);getch();break;case 3: clrscr();p1=sort(head);for(p2=p1-next;p2!=NULL;p2=p2-next)printf(nttname:%stmingci:%d,p2-name,j+);printf(nnnPresseny keyback.);getch();break;case 4: output(head);break;default: printf(nYour choose is not right.);break;while(i!=-1);4、#include#include#define NULL

12、0#define F sizeof(worker)typedef struct workchar sid15; char name15; int money; struct work *next;worker;int min=0,max=0;char a15,b15;worker *input()worker *head,*p,*q;int n=0; char ch;head=(worker *)malloc(F); head-next=0; don+;p=(worker *)malloc(F); p-next=0;.,.printf(nntPlease input %d worker mes

13、sage :,n);printf(n%d worker sid:,n);scanf(%s,p-sid);printf(n%d worker name:,n);scanf(%s,p-name);printf(n%d worker money:,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);elseq-next=p;if(p-moneymax) max=p-money;strcpy(a,p-name); if(p-moneymoney;

14、strcpy(b,p-name); q=p;printf(ntty/n);ch=getch();while(ch=y|ch=Y);return head;void output()clrscr();printf(nThe max money is:%dttname is: %snn,max,a);printf(nThe min money is:%dttname is: %s,min,b);void main()input();output();getch();5、 6、#includestdio.h#define F sizeof(stu)#define NULL 0typedef stru

15、ct studentint sid;int average;struct student *next;stu;stu *head;stu *create()stu *p1,*p2;int n=0;char ch;head=(stu *)malloc(F);head-next=NULL;.,.don+;printf(nnPlease input %d student message:nn,n);printf(t%d student sid:,n);p1=(stu *)malloc(F);p1-next=NULL;scanf(%d,&p1-sid);printf(nt%d student aver

16、age:,n);scanf(%d,&p1-average);if(n=1) head-next=p1;p2=p1; elsep2-next=p1;p2=p1;printf(nntttContinue or back (press y/n):);ch=getch();while(ch=y|ch=Y);return head;stu *select(stu *head,int x)stu *s;s=head-next;while(s)if(s-sid=x)break;s=s-next;return s;stu *insert(stu *head,int x,int y)stu *p,*r,*q;c

17、lrscr();p=head-next;r=(stu *)malloc(sizeof(stu);r-sid=x;r-average=y;if(p=NULL)/*如果插入空表*/p=r;r-next=NULL;.,.printf(ninsert success!);elsewhile(xp-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-n

18、ext;p-next=r;printf(ninsert success!);else if(x=p-sid)/* 学号不能相同*/printf(nError-your input this same sid.);else/* 插到末尾 */p-next=r;r-next=NULL;printf(ninsert success!);return head;stu *get(stu *head,int n)/* 得到位置为n 的结点的指针*/stu *p;int i;p=head-next;if(n=0)return head;elsefor(i=1;inext; return p;stu *de

19、lete(stu *head,int sid)stu *p,*q;int temp=0,i=0;p=head-next;if(!p)printf(nlist is empty.press eny key back.);getch();return head;/* 表空 */elsewhile(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);/* 得到

20、sid 的前一个结点的指针*/q-next=p-next;free(p);printf(nndelete sucess !);return head;else/* 没有找到 */printf(nnNO this data.n); return head;void print(stu *head)stu *p;p=head-next;if(!p)printf(nlist is empty.press eny key back.);getch();while(p)printf(n%d :t%d ,p-sid,p-average);p=p-next;main()stu *p1,*p2;char ch

21、1;int n,i=0,j=0;head=create();doclrscr();printf(n1. insert.);printf(n2. select.);printf(n3. delect.);printf(n4. print list.);printf(n5. EXIT);printf(n.choice (1-5).);ch1=getch();switch(ch1);.,.case 1: clrscr();printf(nplease input insert sid. and average(like 1,1):);scanf(%d,%d,&i,&j);head=insert(he

22、ad,i,j);printf(nnnPress eny key back.);getch();break;case 2: clrscr();printf(ninput you want to selete sid:);scanf(%d,&n);p1=select(head,n);if(p1) printf(nsid:%dtaverage:%d,p1-sid,p1-average);elseprintf(nNo this data.);printf(nnnPress eny key back.);getch();break;case3:clrscr();printf(nPlease input

23、you want delete sid:);scanf(%d,&n);head=delete(head,n);printf(nnnPress eny key back.);getch();break;case4: clrscr();printf(All information :);print(head);printf(nnnPress eny key back.);getch();break;case 5: return;default: printf(nnYour enter is not right.press eny key back.);getch();while(n);7、#inc

24、lude#define F sizeof(L)typedef struct list;.,.char data; struct list *next;L;L *set_list()L *head,*p1,*p2;char c;int n=0;head=(L *)malloc(F); head-next=0;/* 建立链表 */p1=p2=head;printf(nPlease input char(press * finish):);scanf(%c,&c);while(c!=*)n+;if(n=1)p1-data=c;elsep1=(L *)malloc(F);p1-data=c;p2-ne

25、xt = p1;p2 = p1;p1-next = 0;scanf(%c,&c);p1=head;while(p1)printf(%c,p1-data);p1=p1-next;printf(nnn);return head;void change_list(L *head1)/* 算法: p2 指向最后一个元素,p1 指向第一个元素。交换他们的值, p1,p2 同时往中间靠拢。*/L *p1,*p2,*p3;int i,j,k,n=1;char temp;p1=head1;p2=head1;p3=head1;while(p3-next)p3=p3-next;n+;/* 求链长 */for(i=n;i(n/2);i-)/* 外循环使p1 后移, p2 前移。 */ p2=head1; for(j=1;jnext; /*p2 指向最后一个元素 */ temp=p1-data;p1-data=p2-data;p2-data=temp;/* 交换他们的值 */ p1=p1-next;/*p1 向后移 */;.,.while(head1)printf(%c,head1-data); head1=head1-next;void main()L *head; head=set_list(); change_list(head);getch();.

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

当前位置:首页 > 科普知识


经营许可证编号:宁ICP备18001539号-1