Student information management
（1） Problem description
Student information includes ： Student number , full name , Age , Gender , date of birth , address , Telephone ,E-mail etc . Try to design a student information management system , Enable it to provide the following functions ：
 The system works in menu mode
 Student information entry function （ Student information is saved in files ）— input
 Student information browsing function —— output
 query , Sorting function —— algorithm
1, Query by student number
2, Query by name
 Deletion and modification of student information （ Optional ）
（2） Functional requirements
 The interface is simple and clear ;
 Have certain fault tolerance , For example, the score entered is not in 0～100 between , It's illegal , Re entry required ;
 It's best to use a linked list .
（3） algorithm analysis

first , A student includes so many attributes , You should consider defining a structure , secondly , We should consider the storage form of data ： Is to define an array to store , Or define a linked list ? Here, suppose we store it as an array , Certainly. , But we know , If we define an array , We must first know the approximate number of students , So that we can determine the size of the array , But it is not given in the title , And there are a lot of deletions in the title requirements , Insert operation , Therefore, it is more convenient to use a linked list .

Implementation of menu , In fact, it is also relatively simple , First, we use printf Statement lists the functions of the program , Then wait for user input to execute different functions , After performing a function, return to the menu . For the reading and writing of documents, please refer to the relevant chapters of the book .
#include<stdio.h> #include<string.h> #include<math.h> #include<stdlib.h> #
include<windows.h> #define M 80 char f; FILE *fp;// Defines a pointer to a file struct student {
char num; char name; int age; char x; char year_month_day; char
address; int iphone; char e_mail; int xx,yy,wl,cyy,pe; };// Define structure variables int
jiami(); void input(struct student stu[M]); void output(struct student stu[M]);
void lookfor(struct student stu[M]); void order(struct student stu[M]); void
delete_s(struct student stu[M]); void modify(struct student stu[M]); void xuehao
(struct student stu[M]); void xingming(struct student stu[M]); void fileread(
struct student stu[M]); void filewrite(struct student stu[M]); int count;
// Judge the number of students struct student t; int jiami() { int s=2345,m;//s For password ,m Password entered for int i=0;
// It is used to judge whether the entered password is correct printf(" Please input a password ( Only three chances )："); scanf("%d",&m);// Enter password for the first time if(m!=s) {
printf(" Password error , There are two more opportunities ："); scanf("%d",&m);// Enter the password for the second time if(m!=s) { printf(
" Password error , This is the last chance ："); scanf("%d",&m);// Enter the password for the third time if(m!=s) { printf(
" It seems that you are not my master , The program is coming to an end !\n"); } else i=1; } else i=1; } else i=1; return i; } int
main() { system("color F5");// Change the color of the run box and text int choice,sum,i;
//choice Commands used to determine input ;sum Used to record the number of input errors ;i It is used to judge whether the entered password is correct struct student stu[M]; sum=0;
// initialization sum i=jiami(); if(i==1)// Determine whether the entered password is correct do { printf(
"\t-------------------------------------------------\n"); printf("\t\t+
Student information management system +\n"); printf("\t-------------------------------------------------\n");
printf("\t\t ***************\n"); printf("\t\t 1, Add student information \n"); printf("\t\t
2, Browse student information \n"); printf("\t\t 3, Query student information \n"); printf("\t\t 4, Modify student information \n"); printf(
"\t\t 5, Delete student information \n"); printf("\t\t 6, Sort student information \n"); printf("\t\t 7, Read file student \n"); printf
("\t\t 8, Save to file \n"); printf("\t\t 9, Exit the system \n"); printf("\t\t ***************\n");
printf(" Please enter your choice \n"); // Output menu scanf("%d",&choice); // Input selection fflush(stdin); // Empty buffer
if (choice>9||choice<=0) { sum++; if (sum>=5) { printf(" Too many input errors , The program will restart \n");
system("pause"); system("cls"); // Clear screen } } switch (choice) { case 1: input(stu);
break; // Add student information case 2: output(stu); break;// Browse student information case 3: lookfor(stu); break;
// Looking for student information case 4: modify(stu); break; // Modify student information case 5: delete_s(stu); break;
// Delete student information case 6: order(stu); break; // Sort student information case 7: fileread(stu); break;
// Read student information from file case 8: filewrite(stu); break; // Write student information to file case 9: printf(
" Thank you for using the student information management system , Please turn off the program !\n"); system("pause"); break; default: printf(
" Invalid selection ! Please re-enter !\n"); break; } } while(choice!=9); printf("the program is over!\n")
; return 0; } void input(struct student stu[M])// Custom input function { system("cls");// Clear screen
printf(" Please add the information you want to enter for the student \n"); printf(" Please enter by 11 The student number of a student consisting of digits \n"); scanf("%s",stu[count].
num);// Enter student ID printf(" Please enter the student's name \n"); scanf("%s",stu[count].name);// Enter student name printf(
" Please enter the age of the student \n"); scanf("%d",&stu[count].age);// Enter student name printf(" Please enter the gender of the student \n"); scanf(
"%s",stu[count].x);// Enter student gender printf(" Please enter the date of birth of the student \n"); scanf("%s",stu[count].
// Enter student address printf(" Please enter the student's phone number \n"); scanf("%d",&stu[count].iphone); // Enter student mobile number printf(
" Please enter the student's E-mail\n"); scanf("%s",stu[count].e_mail);// Enter the student's email address printf(
" Please enter five items in sequence, no more than 100 Students' grades in various subjects \n"); scanf("%d%d%d%d%d",&stu[count].xx,&stu[count].yy,&stu
[count].wl,&stu[count].cyy,&stu[count].pe);// Enter the student's grades count++;// Calculate the number of students } void
output(struct student stu[M])// Custom output function { int j; system("cls");// Clear screen if(count==0)
// Judge whether there is student information at present { printf(" The information currently saved to students is 0 individual \n"); return; } else { for(j=0;j<count;j++)
{ printf(" Student number :%s\t\t",stu[j].num);// Output student number printf(" full name :%s\t\t",stu[j].name);// Output name
printf(" Age :%d\t\t",stu[j].age);// Output age printf(" Gender :%s\t\t",stu[j].x);// Output gender printf
// Output address printf(" Telephone :%d\t\t",stu[j].iphone);// Output mobile phone number printf("E-mail:%s\t\t",stu[j].
e_mail);// Output mailbox printf(" mathematics :%d\t\t English :%d\t\t Physics :%d\t\tC language :%d\t\t Sports :%d\n\n",stu[j].xx
,stu[j].yy,stu[j].wl,stu[j].cyy,stu[j].pe);// Output various achievements } } } void lookfor(struct
student stu[M])// Custom query function { int choice;// Define selection system("cls");// Clear screen if(count==0)
// Judge whether there is student information at present { printf(" The currently saved student information is 0 individual , Unable to query !!!\n"); return; } else { printf(
" Please select the way you want to query : 1, Student number 2, full name 3, cancel \n"); scanf("%d",&choice);// Input options switch(choice) {
case 3:return;break; case 2:xingming(stu);break;// Query by name case 1:xuehao(stu);break
; // Query by student number default:printf(" Invalid selection ! Please re-enter !\n");break; } } } void xuehao(struct
student stu[M])// User defined student number query function { int j,flag=0; int k=0;// Determine whether the student information is found char xh;
system("cls"); printf(" Please enter the student number of the student you want to query "); scanf("%s",xh);// Enter student number fflush(stdin);
// Empty buffer for(j=0;j<count;j++) { if(strcmp(stu[j].num,xh)==0)// Find the student information of the student number { printf
(" Student number :%s\t",stu[j].num); printf(" full name :%s\t",stu[j].name); printf(" Age :%d\t",stu[j].
age); printf(" Gender :%s\t",stu[j].x); printf(" date of birth :%s\t",stu[j].year_month_day);
"E-mail:%s\t",stu[j].e_mail); printf(" mathematics :%d\t English :%d\t Physics :%d\tC language :%d\t Sports :%d\n",stu[
j].xx,stu[j].yy,stu[j].wl,stu[j].cyy,stu[j].pe);// Output the student's information k=1; } } if(k==0)
// The student number was not found { printf(" I'm sorry , There is no student information you need \n"); } } void xingming(struct student stu[M]
)// Custom name query function { int n=0; int j,flag=0; char xm; system("cls"); printf(
" Please enter the name of the student you want to query "); scanf("%s",xm); fflush(stdin); for(j=0;j<count;j++) { if(
strcmp(stu[j].name,xm)==0) { printf(" Student number :%s\t",stu[j].num); printf(" full name :%s\t",stu[
j].name); printf(" Age :%d\t",stu[j].age); printf(" Gender :%s\t",stu[j].x); printf(
" Telephone :%d\t",stu[j].iphone); printf("E-mail:%s\t",stu[j].e_mail); printf(
" mathematics :%d\t English :%d\t Physics :%d\tC language :%d\t Sports :%d\n",stu[j].xx,stu[j].yy,stu[j].wl,stu[j].cyy,
stu[j].pe); n=1; } } if(n==0) { printf(" I'm sorry , There is no student information you need \n"); } } void order(
struct student stu[M])// Custom sort function { int j,k,choice,max; system("cls"); printf(
" Please enter the way you want to sort \n"); printf("1, mathematics 2, English 3, Physics 4,C language 5, Sports \n"); scanf("%d",&choice);
// Input options switch(choice) { case 1: for(j=0;j<count;j++) { max=j; for(k=j+1;k<count;
k++) if (stu[k].xx>stu[max].xx) { t=stu[k]; stu[k]=stu[max]; stu[max]=t; } };
break; // Sort by math score case 2: for(j=0;j<count;j++) { max=j; for (k=j+1;k<count;k++)
if (stu[k].yy>stu[max].yy) { t=stu[k]; stu[k]=stu[max]; stu[max]=t; } }; break;
// Sort by English score case 3: for(j=0;j<count;j++) { max=j; for (k=j+1;k<count;k++) if (stu[
k].wl>stu[max].wl) { t=stu[k]; stu[k]=stu[max]; stu[max]=t; } }; break;
// Sort by physics score case 4: for(j=0;j<count;j++) { max=j; for (k=j+1;k<count;k++) if (stu[
k].cyy>stu[max].cyy) { t=stu[k]; stu[k]=stu[max]; stu[max]=t; } }; break;
// Press C Language achievement ranking case 5: for(j=0;j<count;j++) { max=j; for (k=j+1;k<count;k++) if (stu
[k].pe>stu[max].pe) { t=stu[k]; stu[k]=stu[max]; stu[max]=t; } }; break;
// Sort by sports performance default: printf(" Invalid selection ! Please re-enter !\n"); break; } if(choice>=1&&choice<=5)
printf(" Sorting succeeded !!!"); } void delete_s(struct student stu[M])// Custom delete function ( Delete by student number ) { int
j,index=0,k=count; char xh; system("cls"); if(count==0)// Judge whether the number of students is 0 { printf
(" The currently saved student information is 0 individual , Cannot delete !!!\n"); return; } else { printf(" Please enter the student ID you want to delete \n"); scanf(
"%s",xh);// Enter the student number of the deleted student fflush(stdin); // Empty buffer for(j=0;j<count;j++) { if(strcmp(stu
[j].num,xh)==0) // Find the student { for(j=index;j<count;j++) stu[j]=stu[j+1]; count--; if(
count<k) // Judge whether the deletion is successful printf(" You have been deleted successfully \n"); } index++; } if(j==count) // Determine whether there is information about the student
printf(" i 'm sorry !!! There is no student information you need to delete !*_*!\n"); } } void modify(struct student stu[M])
// Custom modification function { int j,flag=0,course; int i=0; char xh; system("cls"); if (count
==0) // Judge whether there is student information { printf(" The currently saved student information is 0 individual , Cannot modify !!!\n"); return; } else { printf(
" Please enter the student number you want to modify \n"); scanf("%s",&xh); // Enter the student number of the student to be modified fflush(stdin); for(j=0;j<count
;j++) { if (strcmp(stu[j].num,xh)==0) { i=1; printf(
" Are you sure you want to modify the student information ? If you're not sure , Please close this program !\n"); printf(" Select course : 1, mathematics 2, English 3, Physics 4,C language 5, Sports
6, skip \n"); scanf("%d",&course); // Select a part of student information to modify switch(course) { case 1:printf(
" Please enter the student grade you want to modify \n");scanf("%d",&stu[j].xx);break; // Modify math scores case 2:printf(
" Please enter the student grade you want to modify \n");scanf("%d",&stu[j].yy);break;// Revise English scores case 3:printf(
" Please enter the student grade you want to modify \n");scanf("%d",&stu[j].wl);break; // Modify physics scores case 4:printf(
" Please enter the student grade you want to modify \n");scanf("%d",&stu[j].cyy);break; // modify C language achievement case 5:printf(
" Please enter the student grade you want to modify \n");scanf("%d",&stu[j].pe);break; // Modify sports results case 6:break; default
:printf(" Invalid selection ! Please re-enter !\n");break; } printf(" Select information : 1, Student number 2, full name 3, Age 4, Gender 5, date of birth
6, address 7, Telephone 8,E-mail 9, skip \n"); scanf("%d",&course); // Select a part of student information to modify switch(course)
{ case 1:scanf("%s",stu[j].num);break;// Modify student number case 2:printf(" Please enter the modified information ：");scanf(
"%s",stu[j].name);break;// Modify name case 3:printf(" Please enter the modified information ：");scanf("%d",&stu[j].
age);break;// Modify age case 4:printf(" Please enter the modified information ：");scanf("%s",stu[j].x);break;// Modify gender
case 5:printf(" Please enter the modified information ：");scanf("%s",&stu[j].year_month_day);break;// Modify birthday case
" Please enter the modified information ：");scanf("%d",&stu[j].iphone);break;// Modify phone case 8:printf(" Please enter the modified information ："
);scanf("%s",stu[j].e_mail);break;// Modify mailbox case 9:break; default:printf(
" Invalid selection ! Please re-enter !\n");break; } } } } if(i==0) printf(" No student information !!\n"); return; } void
filewrite(struct student stu[M])// Custom write file function { int j=0; printf(
" Please select whether to save the entered student information :'y' still 'n'???\n"); scanf("%c",&f); // Determine whether to write to the file fflush(stdin);
// Empty buffer while(f!='y'&&f!='n') { if (f!='y'&&f!='n') printf(" Input error , Please re-enter \n");
printf(" The following actions will overwrite the stored data , OK, please enter 'y' or 'n'?\n"); scanf("%c",&f); fflush(stdin); } if (f
=='y') { if((fp=fopen("stu.txt","w"))==NULL) { printf("cannot open this file\n")
; exit(0); } for(j=0;j<count;j++) { fwrite(&stu[j],sizeof(struct student),1,fp);
// Write student information to file } fclose(fp); if(count==0)// Determine whether the number of students is 0 printf(" No files , can't save \n"); else
printf(" Data storage completed \n"); system("pause"); } else return; } void fileread(struct
student stu[M])// Custom read file information function { int j=0; system("cls"); printf(
" Please select whether you want to read the entered student information :'y' still 'n'?\n"); scanf("%c",&f); // Determine whether to read student information from the file fflush(stdin);
// Empty buffer if (f=='y') { if((fp=fopen("stu.txt","r"))==NULL) { printf("cannot open
this file\n"); exit(0); } fread(&stu[j],sizeof(struct student),1,fp);