您的位置:首页 > 其它

C写的学生成绩管理系统,可是文件读出的时候多输出了一行,请各位高手看看。能不能解决哦。

2008-04-23 20:09 639 查看
#include <stdio.h>#include <stdlib.h>#include <string.h>#define LEN sizeof(STUDENT)typedef struct student { long num; /*学号*/ char name[20]; /*名字*/ int score[3]; /*三个科目的分数*/ float average; /*平均分*/ int sum; /*总分*/ int order; /*名次*/ struct student *next; }STUDENT;static int m = 0; /*统计记录条数*/int menu_select(); /*菜单选择*/STUDENT *create(); /*创建链表*/void print(STUDENT *head); /*显示记录*/void search(STUDENT *head); /*根据姓名查找记录*/STUDENT *del(STUDENT *head); /*删除记录*/STUDENT *insert(STUDENT *head); /*插入记录*/STUDENT *sort(STUDENT *head); /*根据它的平均分排序*/void save(STUDENT *head); /*保存记录到文件中*/STUDENT *load(); /*读取文件*/void main(){ STUDENT *head = NULL; for( ; ; ) { switch(menu_select()) { case 1: head=create(); break; case 2: print(head); break; case 3: search(head); break; case 4: head=del(head); break; case 5: head=insert(head); break; case 6: head=sort(head); break; case 7: save(head); break; case 8: head=load(); break; case 9: exit(0); break; } } }/*菜单选择*/int menu_select(){ int n; printf("Welcome to my /"STUDENT SCORE MANAGE SYSTEM/"./n"); printf("Please select the item(1-9):/n"); printf("1.Please input the record./n"); printf("2.Show the record./n"); printf("3.Search the record by your name./n"); printf("4.Delete a record./n"); printf("5.Insert a record./n"); printf("6.Sort by your average./n"); printf("7.Save the record to a file./n"); printf("8.Read the record from a file./n"); printf("9.Exit./n"); do { scanf("%d",&n); if(n<1 || n>9) { printf("Should select 1~9, input again/n"); } }while(n<1 || n>9); return n;}/*输入函数*/STUDENT *create(){ int i; int sum; STUDENT *head; STUDENT *p; sum=0; head=NULL; p=(STUDENT *)malloc(LEN); if(!p) { printf("Memory Overflow!/n"); return(head); } printf("Please enter your num(0:list end):/n"); scanf("%ld",&p->num); printf("Please enter your name:/n"); scanf("%s",&p->name); printf("Please enter your score(0-2):/n"); for(i = 0; i < 3; i++) { scanf("%d",&p->score[i]); sum=sum+p->score[i]; /*求总分*/ } p->sum=sum; p->average=(float)sum/3; /*求平均分*/ p->order=0; m++; p->next=head; head=p; return(head);}/*显示函数*/void print(STUDENT *head){ int i=1; STUDENT *p; p=head; printf("Item num name score1 score2 score3 sum average order/n"); while(p!=NULL) { printf("%3d %ld %s %3d %3d %3d %3d %5.2f %3d/n", i++, p->num,p->name,p->score[0],p->score[1],p->score[2],p->sum,p->average,p->order); p=p->next; }}/*查找函数*/void search(STUDENT *head){ char s[20]; STUDENT *p; p=head; printf("Please enter the name:/n"); scanf("%s",s); while(p && strcmp(s,p->name)!=0 ) { p=p->next; } if(p==NULL) { printf("NO %s student in the list./n",s); } else { printf("num name score1 score2 score3 sum average order/n"); printf("%ld %s %3d %3d %3d %3d %5.2f %3d/n" ,p->num,p->name,p->score[0],p->score[1],p->score[2],p->sum,p->average,p->order); }}/*删除函数*/STUDENT *del(STUDENT *head){ long nm; STUDENT *pre=NULL,*p; p=head; printf("Please enter your num:/n"); scanf("%ld",&nm); while(nm!=p->num && p) { pre=p; p=p->next; } if(!pre && p->num==nm) { head=head->next; } else { pre->next=p->next; } free(p); return(head);}/*插入函数*/STUDENT *insert(STUDENT *head){ int i; int sum = 0; STUDENT *nw=(STUDENT *)malloc(LEN); STUDENT *p; p=head; printf("Please enter your num(0:list end):/n"); scanf("%ld", &nw->num); printf("Please enter your name:/n"); scanf("%s", nw->name); printf("Please enter your score(0-2):/n"); for(i = 0; i < 3; i++) { scanf("%d",&nw->score[i]); sum=sum+nw->score[i]; } nw->sum=sum; nw->average=(float)nw->sum/3; nw->order=0; if(head==NULL) /*原来的链表是空表*/ { head=nw; nw->next=NULL; } /*是p0指向的结点作为头节点*/ else { while( p->next!=NULL) { p=p->next; } p->next=nw; nw->next=NULL; } m=m+1; printf("Item num name score1 score2 score3 sum average order/n"); printf("%3d %ld %s %3d %3d %3d %3d %5.2f %3d/n", m,nw->num,nw->name,nw->score[0],nw->score[1],nw->score[2],nw->sum,nw->average,nw->order); head=sort(head); return(head);} /*排序函数*/STUDENT *sort(STUDENT *head){ int i=0; STUDENT *p1,*p2,*t,*temp; temp=head->next; head->next=NULL; while(temp!=NULL) { t=temp; temp=temp->next; p1=head; p2=head; while( p1!=NULL && t->average < p1->average) { p2=p1; p1=p1->next; } if(p1==p2) { t->next=p1; head=t; } else { t->next=p1; p2->next=t; } } p1=head; while(p1!=NULL) { i++; p1->order=i; p1=p1->next; } printf("Sorting is sucessful./n"); return head;} /*保存函数*/void save(STUDENT *head){ int i=1; FILE *fp; /*定义指向文件的指针*/ STUDENT *p; /* 定义移动指针*/ char outfile[10]; printf("Enter outfile name:/n"); scanf("%s",outfile); if((fp=fopen(outfile,"wb"))==NULL) { printf("Cannot open the file/n"); return; /*若打不开则返回菜单*/ } p=head; /*移动指针从头指针开始*/ while(p!=NULL) /*如p不为空*/ { fwrite(p, LEN, 1, fp); /*写入一条记录*/ p=p->next; /*指针后移*/ } fclose(fp); /*关闭文件*/}/*读取函数*/STUDENT *load(){ int i=0; STUDENT *p1=NULL,*head=NULL; /*定义记录指针变量*/ FILE *fp; /* 定义指向文件的指针*/ char infile[10]; printf("Enter infile name:/n"); scanf("%s",infile); if((fp=fopen(infile,"rb"))==NULL) { printf("Can not open the file./n"); return(head); } p1=(STUDENT *)malloc(LEN); /*开辟一个新单元*/ if(!p1) { printf("Out of memory!/n"); return(head); } head=p1; while(!feof(fp)) { memset(p1, 0, LEN); fread(p1, LEN, 1, fp); printf("num=%ld,name=%s,score1=%d,score2=%d,score3=%d,sum=%d,average=%5.2f,order=%d/n", p1->num,p1->name,p1->score[0],p1->score[1],p1->score[2],p1->sum,p1->average,p1->order); } fclose(fp); return(head);}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: