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

上传人:w****2 文档编号:6537937 上传时间:2020-02-28 格式:DOC 页数:11 大小:37.50KB
返回 下载 相关 举报
C语言课后题答案(北邮出版社).doc_第1页
第1页 / 共11页
C语言课后题答案(北邮出版社).doc_第2页
第2页 / 共11页
C语言课后题答案(北邮出版社).doc_第3页
第3页 / 共11页
点击查看更多>>
资源描述
1、li 45 300.0chang 30 200.0chang2、#includestruct students char 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,&student.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_language;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;do n+; printf(nnPlease input %d student message: nn,n); printf(t%d student sid: ,n); p1=(student *)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-next=p1;p2=p1; else p2-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();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(nnaverage english is : %dnaverage math is : %dnaverage c_language is : %dt,temp1/n,temp2/n,temp3/n);student *sort(student *head)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=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;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(head); printf(nnnttPress eny key back.);getch();void main()student *head,*p1,*p2;int i=0,j=1;head=input();do clrscr();printf(nn(1): average1.nn(2): average2.nn(3): sort.nn(4): output.nnn Please choose: );scanf(%d,&i);switch(i)case 1: average1(head); break;case 2: clrscr();average2(head); printf(nnnPress eny key retuen.);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(nnnPress eny key back.);getch(); break;case 4: output(head); break;default: printf(nYour choose is not right.);break;while(i!=-1);4、#include#include#define NULL 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 message : ,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);else q-next=p; if(p-moneymax) max=p-money;strcpy(a,p-name); if(p-moneymoney;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 struct 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;do n+; 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 average: ,n); scanf(%d,&p1-average); if(n=1) head-next=p1;p2=p1; else p2-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;clrscr();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!); else while(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-next; 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;else for(i=1;inext; return p;stu *delete(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);/*得到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 ch1;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(head,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); else printf(nNo this data.); printf(nnnPress eny key back.);getch(); break; case 3: clrscr();printf(nPlease input you want delete sid: ); scanf(%d,&n); head=delete(head,n); printf(nnnPress eny key back.);getch(); break; case 4: 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、#include#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; else p1=(L *)malloc(F); p1-data=c; p2-next = 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();
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 图纸专区 > 小学资料


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


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