头文件:
#include<stdio.h> #include<string.h> #include <malloc.h> #include <stdlib.h> #include<conio.h> #define LEN sizeof(struct student) typedef struct student { long numb; int chinese; int math; int english; int totalscore; char name[12]; struct student *next; } STU; //声明部 STU* creat(); void output(STU *head); STU* creatByN(int num); STU* findByNumb(STU *head, long num); STU* findByNumbEx(STU *head, long num, STU **ppBefore); STU* findByName(STU *head, char *name); void find(STU *head); STU* del(STU *head); STU* insert(STU *head); void update(STU *head); STU* sort(STU *head); void save_info(STU *head); STU *load_info(); void Backup_info(STU *head); int n;//全局变量
主函数:
#include"head.h" int main() { STU *head; int choice; for(;;) { system("cls");/*清屏*/ printf("\t********************************\t\n"); printf("\t欢迎使用成绩管理系统\t\n"); printf("\t1:输入信息\t\n"); printf("\t2:显示全部信息\t\n"); printf("\t3:删除\t\n"); printf("\t4:查找\t\n"); printf("\t5:插入\t\n"); printf("\t6:修改\t\n"); printf("\t7:排序\t\n"); // printf("\t8:保存文件\t\n"); //printf("\t9:文件加载\t\n"); printf("\t10:备份\t\n"); printf("\t0:退出系统\t\n"); printf("\t********************************\t\n"); printf("*请输入数字(0-9)进行功能选择 :"); scanf("%d",&choice); if(choice==0) break; switch(choice) { case 1: head=creat(); break; case 2: output(head); break; case 3: head=del(head); break; case 4: find(head); break; case 5: head=insert(head); break; case 6: update(head); break; case 7: head=sort(head); break; case 8: save_info(head); break; case 9: head=load_info(); break; case 10: Backup_info(head); break; } printf("按任意键继续....\n"); getch();/*暂停*/ } printf("欢迎使用再见\n"); return 0; }
源代码:
#include"head.h" STU *creat()/*创建链表*/ { STU *head,*pnew,*ptail; head=NULL; n=0;//初始化 printf("现进行信息输入号输入0结束\n"); while(1) { //断申请新节点 pnew=(STU *)malloc(LEN); printf("号\t姓名\t语文\t数\t英语\t总\n"); scanf("%ld",&pnew->numb); if(pnew->numb==0) break; scanf("%s",pnew->name); scanf("%d%d%d",&pnew->chinese,&pnew->math,&pnew->english); pnew->totalscore=0; pnew->totalscore=pnew->chinese+pnew->math+pnew->english; pnew->next=NULL;//节点连接链表尾部 n++; if(n==1) { head=pnew; ptail=pnew; } else { ptail->next=pnew; ptail=pnew; }//算思路让pnew指向新辟结点,ptail指向链表结点,pnew所指结点连接ptail所指结点面,用"ptail->next=pnew"实现. } return head;//能head进行赋值,所返值 } void output(STU *head) { STU *p;//定义指针,使其断移完输 printf("前共%d节点:\n",n); printf("号\t姓名\t语文\t数\t英语\t总\n"); for(p=head; p!=NULL; p=p->next) printf("%ld\t%s\t%d\t%d\t%d\t%d\n",p->numb,p->name,p->chinese,p->math,p->english,p->totalscore); }//变返 struct student *creatByN(int num) { STU *head,*pnew,*ptail; int i; //链表初始化 head=NULL; n=0; //链表创建循环 for(i=0; i<num; i++) { //获节点并赋值节点由pnew指向 pnew=(STU *)malloc(LEN); printf("号\t姓名\t语文\t数\t英语\t总\n"); scanf("%ld",&pnew->numb); scanf("%s",pnew->name); scanf("%d%d%d",&pnew->chinese,&pnew->math,&pnew->english); pnew->next=NULL;//pnew所指向节点插入链表尾部 n++; if(n==1) { head=pnew; ptail=pnew; } else { ptail->next=pnew; ptail=pnew; } } return head; } STU* findByNumb(STU *head, long num) { STU *p,*presult; presult=NULL; for(p=head; p!=NULL; p=p->next) if(p->numb==num) { presult=p;//记录指针位置 break; } return presult; } STU* findByName(STU *head, char *name) { STU *p,*presult; presult=NULL; for(p=head; p!=NULL; p=p->next) if(strcmp(p->name,name)==0) { presult=p; break; } return presult;//位置变更返值 } STU* findByNumbEx(STU *head, long num, STU **ppBefore) { STU *p,*presult,*pBefore; presult=pBefore=NULL; for(p=head; p!=NULL; pBefore=p,p=p->next) //记录指针前位置 if(p->numb==num) { presult=p; break; } *ppBefore=pBefore; return presult; } void find(STU *head) { int num,choice; STU *pr; char name[12]; printf("输入要查找式(1:按号2:按姓名):"); scanf("%d",&choice); if(choice==1) { printf("输入要查找号:"); scanf("%ld",&num); pr=findByNumb(head,num); } else if(choice==2) { printf("输入要查找姓名:"); scanf("%s",name); pr=findByName(head,name); } if(pr==NULL) printf("起,查\n"); else { printf("找该信息:\n"); printf("号\t姓名\t语文\t数\t英语\t总\n"); printf("%ld\t%s\t%d\t%d\t%d\n",pr->numb,pr->name,pr->chinese,pr->math,pr->english,pr->totalscore); } } void update(STU *head) { int num; STU *pr; printf("输入要查找号:"); scanf("%ld",&num); pr=findByNumb(head,num); if(pr==NULL) printf("起,查\n"); else { printf("找该信息:\n"); printf("号\t姓名\t语文\t数\t英语\t总\n"); printf("%ld\t%s\t%d\t%d\t%d\n",pr->numb,pr->name,pr->chinese,pr->math,pr->english); printf("请输入修改信息\n"); printf("号\t姓名\t语文\t数\t英语\t总\n"); scanf("%ld%s%d%d%d%d",&pr->numb,&pr->name,&pr->chinese,&pr->math,&pr->english); pr->totalscore=0; pr->totalscore=pr->chinese+pr->math+pr->english; } } STU* del(STU *head) { long num; STU *pCur,*pBefore; //查找要删除节点用pCur记录该节点 printf("输入要删除号:"); scanf("%ld",&num); pCur=findByNumbEx(head,num,&pBefore); //该节点存进行删除 if(pCur!=NULL) { n--;//记录删除节点数 //根据pCur所指节点位置进行判断:节点或续节点 //删除节点 if(pCur==head) { head=pCur->next; } //删除续节点 else { //获取前向节点指针pBefore //利用pBeforepCur完删除 pBefore->next=pCur->next; } } return head; } STU* insert(STU *head) { STU *pCur,*pnew,*pBefore,*p; //申请新节点并赋值 pnew=(STU *)malloc(LEN); printf("号\t姓名\t语文\t数\t英语\t总\n"); scanf("%d",&pnew->numb); scanf("%s",&pnew->name); scanf("%d%d%d",&pnew->chinese,&pnew->math,&pnew->english); pnew->totalscore=0; pnew->totalscore=pnew->chinese+pnew->math+pnew->english; pnew->next=NULL; if(head==NULL)//链表空 head=pnew; else //链表空 { //找位置即获取pCur for(p=head; p!=NULL; pBefore=p,p=p->next) if(p->numb > pnew->numb) { pCur=p; break; } //没找pCur则应pnew插入尾节点 if(p==NULL) { pBefore->next=pnew; } //找pCur则应pnew插入pCur前根据pCur位置:结点前间节点前两种情况 else { //pCur结点 if(pCur==head) { pnew->next=pCur; head=pnew; } //pCur间结点 else { pnew->next=pCur; pBefore->next=pnew; } } } n++; return head; } STU* sort(STU *head) { STU *first; /*排列序链表指针*/ STU *tail; /*排列序链表尾指针*/ STU *p_min; /*保留键值更节点前驱节点指针*/ STU *min; /*存储节点*/ STU *p; /*前比较节点*/ (STU *)malloc(LEN); first = NULL; while (head != NULL) /*链表找键值节点*/ { /*注意:for语句体现选择排序思想*/ for (p=head,min=head; p->next!=NULL; p=p->next) /*循环遍历链表节点找节点*/ { if (p->next->totalscore < min->totalscore) /*找比前min节点*/ { p_min = p; /*保存找节点前驱节点:显p->next前驱节点p*/ min = p->next; /*保存键值更节点*/ } } /*面for语句结束要做两件事;放入序链表;二根据相应条件判断安排离原链表*/ /*第件事*/ if (first == NULL) /*序链表目前空链表*/ { first = min; /*第找键值节点*/ tail = min; /*注意:尾指针让指向节点*/ } else /*序链表已经节点*/ { tail->next = min; /*刚找节点放即让尾指针next指向*/ tail = min; /*尾指针要指向*/ } /*第二件事*/ if (min == head) /*找节点第节点*/ { head = head->next; /*显让head指向原head->next,即第二节点OK*/ } else /*第节点*/ { p_min->next = min->next; /*前节点next指向前minnext,让min离原链表*/ } } if (first != NULL) /*循环结束序链表first*/ { tail->next = NULL; /*单向链表节点next应该指向NULL*/ } head = first; output( head ); return head; } //保存 void save_info(STU *head) { STU *p; FILE *fp; char filename[20]; printf("输入保存文件文件名:"); scanf("%s",filename); if((fp=fopen(filename,"w"))==NULL) { printf("can not open the file"); exit(0); } fprintf(fp,"前共%d节点:\n",n); fprintf(fp,"号\t姓名\t语文\t数\t英语\t总\n"); for(p=head; p!=NULL; p=p->next) fprintf(fp,"%ld\t%s\t%d\t%d\t%d%\t%d\n",p->numb,p->name,p->chinese,p->math,p->english,p->totalscore); fclose(fp); printf("保存功\n"); } //加载 STU *load_info() { STU *head,*pnew,*ptail; FILE *fp; char filename[20],secondLine[20]; int num,i; printf("输入读取文件文件名:"); scanf("%s",filename); if((fp=fopen(filename,"r"))==NULL) { printf("can not open the file"); exit(0); } fscanf(fp,"前共%d节点:",&num); //滤第二行 for(i=0; i<6; i++) fscanf(fp,"%s",secondLine); //链表初始化 head=NULL; n=0; //链表创建循环 for(i=0; i<num; i++) { //获节点并赋值节点由pnew指向 pnew=(STU *)malloc(LEN); fscanf(fp,"%ld%s%d%d%d%d",&pnew->numb,&pnew->name,&pnew->chinese,&pnew->math,&pnew->english,&pnew->totalscore); pnew->next=NULL; //pnew所指向节点插入链表尾部 n++;//记录加载节点数 if(n==1) { head=pnew; ptail=pnew; } else { ptail->next=pnew; ptail=pnew; } } fclose(fp); printf("读取功\n"); return head; } void Backup_info(STU *head)//备份 { FILE *in,*out; char infile[10],outfile[10]; printf("Enter the inflie name:\n"); scanf("%s",infile); printf("Enter the outflie name:\n"); scanf("%s",outfile); if((in=fopen(infile,"r"))==NULL) { printf("cannot open infile\n"); exit(0); } if((out=fopen(outfile,"w"))==NULL) { printf("cannot open outfile\n"); exit(0); } while(! feof(in)) fputc(fgetc(in),out); fclose(in); fclose(out); printf("备份功\n"); }
运行结果为:
进入界面 如图:
输入界面:
显示全部信息:
删除功能:
查找功能:
插入功能:
修改功能:
排序功能:
退出: