Link to this article: https://blog.csdn.net/sakurakider/article/details/72823 six 62
It took me a month to learn the linked list, file and multi file programming, and then I began to write the student management system. The simpler one realized the functions of adding, adding, changing, searching, sorting, limiting the time of input, and analyzing the scores. I have encountered many problems and tried to solve them. Here are some points to pay attention to in the student management system, hoping to help others.
We must first write the framework of the main function, and then add something to it. This is a bit general. I put my main function below, which can be used for reference. My main function is an infinite loop of while(1), in which a switch is selected, a menu function is used to make its return value as the value selected by the switch, and then enter the main functions of different functions. The main functions of other functions follow the same routine as the main functions.
copyint main(int argc, char *argv[]) { login(); //This is a function to introduce yourself Sleep(1000); //Sleep function while(1) { switch(menu()) //Main menu select function { case 1: system("CLS"); //Screen clearing function printf("\t\t________Student grade addition_______"); head=luru(); cr(head); int choice; scanf("%d",&choice); if(choice==0) break; case 2: system("CLS"); //printf("\t\t\\t\\t\\t\t student grade sorting and deleting \\d"); paxu(); int end; scanf("%d",&end); if(end==0) break; case 3: system("CLS"); printf("\t\t__________Revision of grades__________"); xg(); int a; scanf("%d",&a); if(a==0) break; case 4: system("CLS"); printf("\t\t___________Student information analysis_______"); fx(); int b; scanf("%d",&b); if(b==0) break; case 5: system("CLS"); printf("\t\t___________Thanks for using ha_______\n"); Sleep(1000); exit(-1); break; } system("PAUSE"); } return 0; }


2. the input module must pay attention to the input format. I use the tail interpolation method. After each node is processed, the user will decide whether to continue to input. In terms of format control, when the user inputs once, I will determine whether the symbol is standard. If not, I will input again. Finally, it is stored in the file.


3. make sure to write functions with a higher utilization rate, so that you can reduce the repeated writing of code. This is all based on your own style of writing code. I haven't done very well in this regard. In reflection, when I first started learning c, some people said that the main body of c language is a function, so I didn't understand it. After writing this, I felt that I was really right. After writing c language functions, I can directly reference them when they should be used, which is very convenient.






Full code
Main function
main.c
copy#include <stdio.h> #include <stdlib.h> #include "student.h" #include <string.h> /* run this program using the console pauser or add your own getch, system("pause") or input loop */ int main(int argc, char *argv[]) { login(); //struct student *head; Sleep(1000); while(1) { switch(menu()) { case 1: system("CLS"); printf("\t\t________Student grade addition_______"); head=luru(); cr(head); int choice; scanf("%d",&choice); if(choice==0) break; case 2: system("CLS"); //printf("\t\t\\t\\t\\t\t student grade sorting and deleting \\d"); paxu(); int end; scanf("%d",&end); if(end==0) break; case 3: system("CLS"); printf("\t\t__________Revision of scores__________"); xg(); int a; scanf("%d",&a); if(a==0) break; case 4: system("CLS"); printf("\t\t___________Student information analysis_______"); fx(); int b; scanf("%d",&b); if(b==0) break; case 5: system("CLS"); printf("\t\t___________Thanks for using ha_______\n"); Sleep(1000); exit(-1); break; } system("PAUSE"); } return 0; }
Function declaration
student.h
copy#include <stdio.h> struct student *temp; struct student *head; struct student{ int num; char name[6]; int yw; int sx; int yy; struct student*next; }; int menu(); void login(); struct student* luru(); void cr(struct student *h); struct student*cc(); int menu1(); void paxu(); void px1(); void px2(); void px3(); void px4(); void sanch(); void tj(); void xg(); void fx();
Function definition
student.c
copy#include <stdio.h> #include "student.h" #include <stdlib.h> #include <string.h> int menu() { char n; do{ system("cls"); printf("\t\t\t|-------------------------------------------------|\n"); printf("\t\t\t| *****Student management system***** |\n"); printf("\t\t\t|-------------------------------------------------|\n"); printf("\t\t\t| 1.Student score entry |\n"); printf("\t\t\t| 2.Sorting and deleting student scores |\n"); printf("\t\t\t| 3.Revision of scores |\n"); printf("\t\t\t| 4.Student performance analysis |\n"); printf("\t\t\t| 5.Exit program |\n"); printf("\t\t\t---------------------------------------------------\n"); printf("Please select 1-5: "); n=getch(); }while(n<'0'||n>'5'); return(n-48); } void login() { printf("\n\n\n\t\t\t Student information management system\n\n"); printf("\t\t\t Version number: 0.2\n\n"); printf("\n\n\n\n\t\t\t 2017 May 10\n\n"); printf("\n\n\t\t\t sakurakid\n"); } struct student *luru() { struct student*rhead ,*r,*t,*stu; rhead=(struct student*)malloc(sizeof(struct student)); t=rhead; rhead->next=NULL; char xx; int flag=1; printf("\t\t_____________________________\n"); printf("\t\t| Student management system |\n"); printf("\t\t_____________________________\n"); printf("\t\t| |\n"); printf("\t\t| Input 1 add |\n"); printf("\t\t| Enter 0 to exit |\n"); printf("\t\t|___________________________|\n"); while(xx!='1'&&xx!='0') { xx=getch(); } if(xx=='1') { system("CLS"); do{ system("CLS"); stu=(struct student*)malloc(sizeof(struct student)); printf("\t\t_____________________________\n"); printf("\t\t| Student management system |\n"); printf("\t\t|___________________________|\n"); printf("\t\t| |\n"); printf("\t\t| Input 1 add |\n"); printf("\t\t| Enter 0 to exit |\n"); printf("\t\t|___________________________|\n"); printf("\t\t Student ID: ");scanf("%d",&stu->num); if(stu->num > 99999999||stu->num < 10000000) { printf("\t\t______________________\n"); printf("\t\t_Please enter an 8-bit non negative number__\n"); printf("\t\t______________________\n"); printf("\t\t Re enter student ID: ");scanf("%d",&stu->num); } printf("\t\t name: ");scanf("%s",stu->name); if(stu->name[0] > 0) { printf("\t\t______________________\n"); printf("\t\t______Name please enter Chinese characters__\n"); printf("\t\t______________________\n"); printf("\t\t Re enter name: ");scanf("%s",stu->name); } printf("\t\t language: ");scanf("%d",&stu->yw); printf("\t\t mathematics: ");scanf("%d",&stu->sx); printf("\t\t English: ");scanf("%d",&stu->yy); if(stu->sx<0||stu->sx>100||stu->yw<0||stu->yw>100||stu->yy<0||stu->yy>100) { printf("\t\t______________________\n"); printf("\t\t_Please enter 0 for grade-100 between__\n"); printf("\t\t______________________\n"); printf("\t\t Reenter grades\n"); printf("\t\t language: ");scanf("%d",&stu->yw); printf("\t\t mathematics: ");scanf("%d",&stu->sx); printf("\t\t English: ");scanf("%d",&stu->yy); } t->next=stu; t=stu; xx = getch(); printf("To continue, press 1, to exit, press 0:"); while(xx!='1'&&xx!='0') { xx=getch(); } }while(xx=='1'); t->next=NULL; } return (rhead); } void cr(struct student *h) { struct student *stu; FILE*fp; if((fp=fopen("Student information.txt","wt"))==NULL) { printf("File error 233, press any key to exit!"); getch(); exit(1); } for(stu=h->next;stu!=NULL;stu=stu->next) { fprintf(fp,"%d %s %d %d %d\n",stu->num,stu->name,stu->yw,stu->sx,stu->yy); } fclose(fp); } struct student*cc() { struct student*ahead,*r,*stu; FILE*f1; f1=fopen("Student information.txt","rt"); ahead=(struct student*)malloc(sizeof(struct student)); ahead->next=NULL; r=ahead; while(!feof(f1)) { stu=(struct student*)malloc(sizeof(struct student)); fscanf(f1,"%d %s %d %d %d\n",&stu->num,stu->name,&stu->yw,&stu->sx,&stu->yy); r->next=stu; r=stu; } r->next=NULL; fclose(f1); return ahead; } int menu1() { system("CLS"); char n; do{ system("CLS"); printf("\t\t__________________________________________________\n"); printf("\t\t| |\n"); printf("\t\t| 1.Sort order 2.Sort by English3.Sort by language |\n"); printf("\t\t| 4.Sort by math 5.Delete student 6.Add student |\n"); printf("\t\t| |\n"); printf("\t\t|______________Press 0 to exit___________________________|\n"); printf("\t\t Please select 0-6: "); n=getch(); }while(n<'0'||n>'6'); return (n-48); } void paxu() { system("CLS"); char n; do{ switch(menu1()) { case 1: px1(); break; case 2: system("CLS"); px2(); break; case 3: system("CLS"); px3(); break; case 4: system("CLS"); px4(); break; case 5: system("CLS"); sanch(); break; case 6: system("CLS"); tj(); break; } printf("To continue, press 1, to exit, press 0:"); n=getch(); while(n!='1'&&n!='0') { n=getch(); } }while(n=='1'); if(n=='0') return; } void px1() { struct student *t,*h; h=cc(); printf("\n"); printf("\t\t%10s%10s%10s%10s%10s\n","Student ID","name","language","mathematics","English","Average score","Total score"); printf("\t\t--------------------------------------------------------------\n"); for(t=h->next;t!=NULL;t=t->next) { printf("\t\t%10d%10s%10d%10d%10d%\n",t->num,t->name,t->yw,t->sx,t->yy); } } void px2() { struct student *p,*q,*head,*t; head=cc(); int tnum; char tname[6]; int tyw; int tsx; int tyy; for(p = head->next;p->next!=NULL;p=p->next){ for(q=p->next;q!=NULL;q=q->next){ if(p->yy < q->yy) { tnum=p->num; p->num=q->num; q->num=tnum; strcpy(tname,p->name); strcpy(p->name,q->name); strcpy(q->name,tname); tyw=p->yw; p->yw=q->yw; q->yw=tyw; tsx=p->sx; p->sx=q->sx; q->sx=tsx; tyy=p->yy; p->yy=q->yy; q->yy=tyy; } } } printf("\t\t English sorting \n"); printf("\t\t%10s%10s%10s%10s%10s\n","Student ID","name","language","mathematics","English","Average score","Total score"); printf("\t\t--------------------------------------------------------------\n"); for(t=head->next;t!=NULL;t=t->next) { printf("\t\t%10d%10s%10d%10d%10d%\n",t->num,t->name,t->yw,t->sx,t->yy); } } void px3() { struct student *p,*q,*head,*t; head=cc(); int tnum; char tname[6]; int tyw; int tsx; int tyy; for(p = head->next;p->next!=NULL;p=p->next){ for(q=p->next;q!=NULL;q=q->next){ if(p->yw < q->yw) { tnum=p->num; p->num=q->num; q->num=tnum; strcpy(tname,p->name); strcpy(p->name,q->name); strcpy(q->name,tname); tyw=p->yw; p->yw=q->yw; q->yw=tyw; tsx=p->sx; p->sx=q->sx; q->sx=tsx; tyy=p->yy; p->yy=q->yy; q->yy=tyy; } } } printf("\t\t Chinese sorting \n"); printf("\t\t%10s%10s%10s%10s%10s\n","Student ID","name","language","mathematics","English","Average score","Total score"); printf("\t\t--------------------------------------------------------------\n"); for(t=head->next;t!=NULL;t=t->next) { printf("\t\t%10d%10s%10d%10d%10d%\n",t->num,t->name,t->yw,t->sx,t->yy); } } void px4() { struct student *p,*q,*head,*t; head=cc(); int tnum; char tname[6]; int tyw; int tsx; int tyy; for(p = head->next;p->next!=NULL;p=p->next){ for(q=p->next;q!=NULL;q=q->next){ if(p->sx < q->sx) { tnum=p->num; p->num=q->num; q->num=tnum; strcpy(tname,p->name); strcpy(p->name,q->name); strcpy(q->name,tname); tyw=p->yw; p->yw=q->yw; q->yw=tyw; tsx=p->sx; p->sx=q->sx; q->sx=tsx; tyy=p->yy; p->yy=q->yy; q->yy=tyy; } } } printf("\t\t Mathematical sorting \n"); printf("\t\t%10s%10s%10s%10s%10s\n","Student ID","name","language","mathematics","English","Average score","Total score"); printf("\t\t--------------------------------------------------------------\n"); for(t=head->next;t!=NULL;t=t->next) { printf("\t\t%10d%10s%10d%10d%10d%\n",t->num,t->name,t->yw,t->sx,t->yy); } } void sanch() { px1(); struct student *p,*q,*phead,*t,*m; phead=cc(); int n; int flag=0; printf("\t\t____________________________________________________\n"); printf("\t\t|______________Delete student information_________________________|\n"); printf("\t\t|___________________________________________________|\n"); printf("\t\t Please enter the student ID to delete:"); scanf("%d",&n); p=phead; for(m=phead;m!=NULL;m=m->next) { if(m->num==n) { printf("\t\t Information about the students has been found\n"); flag=0; break; } else flag=1; } if(flag==1) printf("\t\t Sorry, no information about this student can be found\n"); if(flag==0) { if(phead->num==n) { phead=phead->next; } else { while(p->num!=n && p->next!=NULL) { t=p; p=p->next; } if(p->num==n) { t->next=p->next; } } printf("\t\t The student information has been deleted\n"); } cr(phead); } void tj() { struct student *phead,*stu; phead=cc(); printf("\t\t____________________________________________________\n"); printf("\t\t|______________Add student information_________________________|\n"); printf("\t\t|___________________________________________________|\n"); stu=(struct student*)malloc(sizeof(struct student)); printf("\t\t Student ID: ");scanf("%d",&stu->num); if(stu->num > 99999999||stu->num < 10000000) { printf("\t\t______________________\n"); printf("\t\t_Please enter an 8-bit non negative number__\n"); printf("\t\t______________________\n"); printf("\t\t Re enter student ID: ");scanf("%d",&stu->num); } printf("\t\t name: ");scanf("%s",stu->name); if(stu->name[0] > 0) { printf("\t\t______________________\n"); printf("\t\t______Name please enter Chinese characters__\n"); printf("\t\t______________________\n"); printf("\t\t Re enter name: ");scanf("%s",stu->name); } printf("\t\t language: ");scanf("%d",&stu->yw); printf("\t\t mathematics: ");scanf("%d",&stu->sx); printf("\t\t English: ");scanf("%d",&stu->yy); if(stu->sx<0||stu->sx>100||stu->yw<0||stu->yw>100||stu->yy<0||stu->yy>100) { printf("\t\t______________________\n"); printf("\t\t_Please enter 0 for grade-100 between__\n"); printf("\t\t______________________\n"); printf("\t\t Reenter grades\n"); printf("\t\t language: ");scanf("%d",&stu->yw); printf("\t\t mathematics: ");scanf("%d",&stu->sx); printf("\t\t English: ");scanf("%d",&stu->yy); } stu->next=phead->next; phead->next=stu; printf("\t\t Student information has been saved\n"); cr(phead); } void xg() { system("CLS"); char n; do{ system("CLS"); px1(); struct student *p,*q,*phead,*t,*m; int flag=0; int timp; phead=cc(); printf("\t\t____________________________________________________\n"); printf("\t\t|______________Modify student information_________________________|\n"); printf("\t\t|___________________________________________________|\n"); printf("\t\t Please enter the student ID to be modified:"); scanf("%d",&timp); p=phead; for(m=phead;m!=NULL;m=m->next) { if(m->num==timp) { printf("\t\t Information about the students has been found\n"); flag=0; break; } else flag=1; } if(flag==1) printf("\t\t Sorry, no information about this student can be found\n"); if(flag==0) { printf("\t\t The student's original information is as follows\n"); printf("\t\t Student No.:%d\n",m->num); printf("\t\t Name:%s\n",m->name); printf("\t\t Language:%d\n",m->yw); printf("\t\t Mathematics:%d\n",m->sx); printf("\t\t English:%d\n\n",m->yy); printf("\t\t Please re-enter the student's information\n"); printf("\t\t Student ID: ");scanf("%d",&m->num); if(m->num > 99999999||m->num < 10000000) { printf("\t\t______________________\n"); printf("\t\t_Please enter an 8-bit non negative number__\n"); printf("\t\t______________________\n"); printf("\t\t Re enter student ID: ");scanf("%d",&m->num); } printf("\t\t name: ");scanf("%s",m->name); if(m->name[0] > 0) { printf("\t\t______________________\n"); printf("\t\t______Name please enter Chinese characters__\n"); printf("\t\t______________________\n"); printf("\t\t Re enter name: ");scanf("%s",m->name); } printf("\t\t language: ");scanf("%d",&m->yw); printf("\t\t mathematics: ");scanf("%d",&m->sx); printf("\t\t English: ");scanf("%d",&m->yy); if(m->sx<0||m->sx>100||m->yw<0||m->yw>100||m->yy<0||m->yy>100) { printf("\t\t______________________\n"); printf("\t\t_Please enter 0 for grade-100 between__\n"); printf("\t\t______________________\n"); printf("\t\t Reenter grades\n"); printf("\t\t language: ");scanf("%d",&m->yw); printf("\t\t mathematics: ");scanf("%d",&m->sx); printf("\t\t English: ");scanf("%d",&m->yy); } printf("\t\t The student information has been modified\n"); cr(phead); } printf("Press 1 to continue modifying or 0 to exit:"); n=getch(); while(n!='1'&&n!='0') { n=getch(); } }while(n=='1'); if(n=='0') return; } void fx() { struct student *p,*q,*phead,*t,*m; int flag=0; int jyw=0; int jsx=0; int jyy=0; int timp; int n=0; phead=cc(); p=phead; for(t=p->next;t!=NULL;t=t->next) { n++; if(t->sx>=60) jsx++; if(t->yw>=60) jyw++; if(t->yy>=60) jyy++; } printf("\t\t____________________________________________________\n"); printf("\t\t|______________Student performance analysis_________________________|\n"); printf("\t\t|___________________________________________________|\n"); printf("\t\t Totally entered%d Student scores\n",n); printf("\t\t The number of people who have passed the language test is%d\n",jyw); printf("\t\t The number of people who have passed mathematics is%d\n",jsx); printf("\t\t The number of people who have passed English is%d\n",jyy); printf("\t\t Analysis completed\n"); printf("To exit, press 0:"); }