#include <stdio.h>#include <mem.h>#include <stdlib.h>#include <dos.h>#include <ctype.h>#include <alloc.h>#include <string.h>#define LEN sizeof(struct student)struct student{char num[6]; char name[6]; int score[3]; int sum; float average; int order; struct student *next;}head;int select(){int n;printf("********************************Welcome**************************************\n");printf("These below are your choices:\n");printf("\t\t1:Creat a text.\n");printf("\t\t2:Show the contents of the text.\n");printf("\t\t3:Search what you want in the system.\n");printf("\t\t4:Delete what you do not want to reserve.\n");printf("\t\t5:Insert the dates you insert.\n");printf("\t\t6:Sort these dates.\n");printf("\t\t7:Save these dates you insert.\n");printf("\t\t8:Load the dates from the file.\n");printf("\t\t9:Quit.\n");printf("********************************END*******************************************\n");printf("\t\tPlease input the choice you make:\n");scanf("%d",&n);while(n<0||n>9){printf("Please input the choice you make again:\n"); scanf("%d",&n);}return(n);} struct student *creat(){struct student *head,*p,*q;int i;float s=0;head=NULL;q=(struct student *)malloc(LEN);for(;;){ printf("Please input the student number:"); p=(struct student *)malloc(LEN); scanf("%s",p->num); if(p->num[0]=='0')break; printf("Please input the student's name:"); scanf("%s",p->name); printf("Please input the student's three scores:"); for(i=0;i<3;i++) { scanf("%d",&p->score[i]); if(p->score[i]<0||p->score[i]>100) { printf("Date error,please input again:"); scanf("%d",&p->score[i]); } s=s+p->score[i]; } p->sum=(int)s; p->average=s/3; p->order=0; if(head==NULL)head=p; else q->next=p; q=p;}q->next=NULL;return(head);} void show(struct student *head){struct student *p;printf("**********************Show the scores***************************************\n"); p=head; while(p) {printf("%6s,%6s,%3d,%3d,%3d,%3d,%3.2f,%3d\n",p->num,p->name,p->score[0],p->score[1],p->score[2],p->sum,p->average,p->order); p=p->next; }} void search(struct student *head){struct student *p; char s[6]; printf("Please input the name you want to find:"); scanf("%s",s); p=head; while(strcmp(p->name,s)!=0&&p!=NULL) p=p->next; if(p!=NULL) {printf("Successfully find the student:"); printf("%s",s); } else printf("There is not the student .\n");} struct student *delete(struct student *head){struct student *p,*q;char new[10]; printf("Please input the name you want to delete:"); scanf("%s",new); if(head==NULL)printf("the list is null.\n"); else p=head; while(p->name!=new&&p->next!=NULL) {q=p;p=p->next;} if(p->name==new) {if(p==head)head=p->next; else q->next=p->next; printf("The student's information has benn deletes.\n"); } else printf("There is not the student you want to delete.\n"); return(head);} struct student *insert(struct student *head){struct student *p;for(;;){p=(struct student *)malloc(LEN); printf("Please input the student's number:"); scanf("%s",p->name); printf("Please input the student's name:"); scanf("%s",p->name); printf("Please input the student's three scores:"); scanf("%d%d%d",p->score[0],p->score[1],p->score[2]); p->sum=p->score[0]+p->score[1]+p->score[2]; p->average=(float)p->sum/3; p->order=0; if(head!=NULL)p->next=head; head=p; return(head);}} struct student *sort(struct student *head){struct student *p,*q,*h;h=head;p=q=h;while((head=head->next)!=NULL) {while((p->average>head->average)&&(p->next!=NULL)) {q=p;p=p->next;} if(p->average<=head->average) {if(p==h)h=head; else q->next=head; head->next=p; } else p->next=head; }return(h);} void save(struct student *head){FILE *fp;char fname[10];printf("Please input the name you want to save:");scanf("%s",fname);if((fp=fopen(fname,"wb"))==NULL) {printf("File cannot open.\n"); return; } while(head) {if(fwrite(head,LEN,1,fp)!=1) printf("File write error.\n"); head=head->next; }fclose(fp);} struct student *load(){struct student *head=NULL,*p;FILE *fp;char fname[10];printf("Please input the name you want to load:");scanf("%s",fname);if((fp=fopen(fname,"rb"))==NULL) {printf("File cannot be loaded.\n"); return(head); }p=head;while(!feof(fp)) {p=(struct student *)malloc(LEN); if(fread(p,LEN,1,fp)!=1) {printf("File load error.\n"); return(head); } p=p->next; } fclose(fp);return(head); } main(){struct student *head=NULL;for(;;){switch(select()) { case 1:head=creat();break; case 2:show(head);break; case 3:search(head);break; case 4:delete(head);break; case 5:insert(head);break; case 6:sort(head);break; case 7:save(head);break; case 8:load();break; case 9:exit(0); }}}

评论