● Title
C Language student information management system
● Topic analysis
Introducing files into student information management system , Perfect the processing of students' information data , Including the reading of information , preservation , Refresh . Writing procedures related to documents , You need to consider how the file will be manipulated , Can the file be opened , Where is the file . For the readability of the program , In general, read the file , save , Operations are defined as functions .
● Algorithm construction
●● Refresh student information update() function
<1> Open the specified file as read-only , Determine whether the file is empty , If it is empty , Then prompt : The file is empty !
<2> Read in students' information circularly , And add up the number of students currently recorded n
<3> Output prompt : Refresh succeeded !
<4> Close the file
●● Query student information seek() function
<1> The user enters the operation to be performed :1. Query by student number ;2. Search by name ;3. Exit this menu
<2> choice 1: Student number to input ; Judge that the input student number is consistent with the saved one ; If it is consistent, the information of students will be output , Inconsistent output : The student number does not exist !
<3> choice 2: Enter the name of the student you want to query ; Judge that the input name is consistent with the saved name ; If it is consistent, the information of students will be output , Inconsistent output : The name does not exist !
●● Modify student information modify() function
<1> Enter the student number of the student to be modified
<2> Compare the input student number with the saved student number , Select the operation number to be performed :1. Change name ;2. Modify age ;3. Modify gender ;4. modify C language achievement ;5. Revision of higher mathematics scores ;6. Revision of College English scores ;7. Exit this menu
<3> choice 1: Enter a new name s2; New name s2 Replace the original name ; Save new name
<4> choice 2: Enter a new age age; New name age Replace the original age ; Save the new age
<5> choice 3: Enter new gender sex1; New gender sex1 Replace the original gender ; Save the new gender
<6> choice 4: Enter new C language achievement score1; new C language achievement score1 Replace the original C language achievement ; Save new C language achievement
<7> choice 5: Enter new advanced mathematics scores score1; New achievements in Advanced Mathematics score1 Replace the original higher mathematics score ; Save new advanced mathematics scores
<8> choice 6: Enter new college English scores score1; New college English scores score1 Replace the original name ; Save new college English scores
<9> choice 7: Return to main menu
●● Insert student information function insert() function
<1> Enter the number of students to be added n
<2> Enter the student number of each student in turn , full name , Gender , Age ,C language achievement , Advanced mathematics achievement , College English scores
<3> Write the information to the file in turn
<4> Tips : Added successfully !
<5> Call sort by student number function , Sort the information well
<5> Close the file
●● Delete information by student number del() function
<1> Enter the student number of the student you want to delete s1
<2> initialization flag=0, Enter the cycle , Compare the input student number with the student number recorded in the file , If they are consistent, they will be ordered flag=1, The loop moves the following student record forward
<3> judge flag=0, Tips : The student number does not exist !
<4> judge flag=1, Number of students -1 And save it , Tips : Successfully deleted !
<5> Close the file
●● Display current information display() function
<1> Open file
<2> Read file information ; If the file is empty , Then prompt : The file is empty ! If the file is not empty , Then output the student number of each student in turn , full name , Gender , Age ,C language achievement , Advanced mathematics achievement , College English scores
<3> Close the file
●● Save current student information fileWrite() function
<1> Open file
<2> Read file information ; If the file is empty , Then prompt : The file is empty ! If the file is not empty , Then write the student number of each student in turn , full name , Gender , Age ,C language achievement , Advanced mathematics achievement , College English scores
<3> Close the file
● source code
#include<stdio.h> /*I/O function */ #include<stdlib.h> /* Other instructions */ #include<string.h>
/* String function */ #define max 100 #define LEN 15 /* Maximum characters of student number and name , Please change the actual situation */ #define N 100 /*
Maximum number of students , Please change the actual situation */ int k=1,n=0, m=0;/* n Represents the number of students currently recorded */ // Student information structure struct student {
char no[LEN];// Student number char name[LEN];// full name char sex[LEN];// Gender int age;// Age float
C;//c language achievement float M;// Mathematics achievement float E;// English achievement }stu[N]; void fileWrite(); // System help and description
void help() { printf("\n0. Welcome to system help !\n"); printf("\n1. After entering the system for the first time , Please select add student information first ;\n");
printf("\n2. Type the number code according to the menu prompt ;\n"); printf("\n3. After adding student information , Remember to save ;\n");
printf("\n4. Thank you for your use !\n"); } // Refresh student information void update() { int j=0; FILE * fp;
if((fp=fopen("student.txt","r"))==NULL) printf(" The file is empty !\n");
while(fscanf(fp,"%s,%s,%s,%d,%f,%f,%f\n",&stu[j].no,&stu[j].name,&stu[j].sex,&stu[j].age,&stu[j].C,&stu[j].M,&stu[j].E)==7)
// Reading information from a file { j++; } n=j; printf("\n Tips : Refresh succeeded !!! \n"); fclose(fp); } // Query student information
void seek() { int j=0; FILE * fp; if((fp=fopen("student.txt","r"))==NULL)
return ; while(fscanf(fp,"%s %s %s %d %f %f
%f",&stu[j].no,&stu[j].name,&stu[j].sex,&stu[j].age,&stu[j].C,&stu[j].M,&stu[j].E)==7)
{ j++; } n=j; int i=0,item,flag; char s1[LEN+1]; /* The longest length is given by name and student number +1 Subject to */
printf("----------------------\n"); printf("-----1. Query by student number -----\n");
printf("-----2. Search by name -----\n"); printf("-----3. Exit this menu -----\n");
printf("----------------------\n"); while(1) { printf(" Please select submenu number :");
scanf("%d",&item); // The user enters the operation to be performed flag=0; switch(item) { case 1:
printf(" Please enter the student number of the student you want to query :\n"); scanf("%s",&s1); for(i=0; i<n; i++) //n Represents the number of students currently recorded
if(strcmp(s1,stu[i].no)==0) //strcmp() Is a string comparison function , Judge that the input student number is consistent with the saved one { flag=1;
printf(" Student number Student name Age Gender C language achievement Advanced mathematics College English scores \n");
printf("--------------------------------------------------------------------\n");
printf("%s %16s %10s %10d %10.1f %10.1f
%10.1f\n",stu[i].no,stu[i].name,stu[i].sex,stu[i].age,stu[i].C,stu[i].M,stu[i].E);
// Output students' information } if(0==flag) // The input student number is inconsistent with the saved one printf(" The student number does not exist !\n"); break; case 2:
printf(" Please enter the name of the student you want to query :\n"); scanf("%s",&s1); // Enter student name for(i=0; i<n; i++)
if(strcmp(stu[i].name,s1)==0) // The input name is consistent with the saved name { flag=1; printf(" Student number Student name Age Gender
C language achievement Advanced mathematics College English scores \n");
printf("--------------------------------------------------------------------\n");
printf("%s %16s %10s %10d %10.1f %10.1f
%10.1f\n",stu[i].no,stu[i].name,stu[i].sex,stu[i].age,stu[i].C,stu[i].M,stu[i].E);
// Output students' information } if(0==flag) // The input name is inconsistent with the saved name printf(" The name does not exist !\n"); break; case 3:
return; default: printf(" Please check in the 1-3 Choose between \n"); } } } // Modify student information void modify() { int j=0;
FILE * fp; if((fp=fopen("student.txt","r"))==NULL) { printf("\n
warning : Error opening file , Please exit and reopen !\n"); system("pause"); exit(0); } while(fscanf(fp,"%s %s %s
%d %f %f
%f",&stu[j].no,&stu[j].name,&stu[j].sex,&stu[j].age,&stu[j].C,&stu[j].M,&stu[j].E)==7)
{ j++; } n=j; int i,item=0,num; int age; char sex1[4],s1[LEN+1],s2[LEN+1]; /*
The longest length is given by name and student number +1 Subject to */ float score1; printf(" Please enter the student number of the student to be modified :"); scanf("%s",s1);
for(i=0;i<n;i++) { if(strcmp(stu[i].no,s1)==0) /* Compare strings for equality */ { num=i; // to num Fu i value
printf("------------------\n"); printf("1. Change name \n"); printf("2. Modify age \n");
printf("3. Modify gender \n"); printf("4. modify C language achievement \n"); printf("5. Revision of higher mathematics scores \n");
printf("6. Revision of College English scores \n"); printf("7. Exit this menu \n"); printf("------------------\n");
while(1) { printf(" Please select submenu number :"); scanf("%d",&item); switch(item) { case 1:
printf(" Please enter a new name :"); scanf("%s",s2); strcpy(stu[num].name,s2);
//strcpy() Is a string copy function , Assign the newly entered value to the original value fileWrite(); break; case 2: printf(" Please enter a new age :");
scanf("%d",&age); stu[num].age=age; fileWrite(); break; case 3:
printf(" Please enter a new gender :"); scanf("%s",&sex1); strcpy(stu[num].sex,sex1); fileWrite();
break; case 4: printf(" Please enter a new C language achievement :"); scanf("%d",&score1); stu[num].C=score1;
fileWrite(); break; case 5: printf(" Please enter a new advanced mathematics score :"); scanf("%d",&score1);
stu[num].M=score1; fileWrite(); break; case 6: printf(" Please enter the new college English score :");
scanf("%d",&score1); stu[num].E=score1; fileWrite(); break; case 7: return;
default: printf(" Please check in the 1-7 Choose between \n"); } } } else { printf(" There is no student number !!!"); } } }
// Sort functions by student number void sort() { int i,j,*p,*q,s; char temp[LEN+1],ctemp[LEN]; float
*x,*y,z; float *a,*b,c; float *k,*l,h; for(i=0;i<n-1;i++) { for(j=n-1;j>i;j--)
if(strcmp(stu[j-1].no,stu[j].no)>0) { strcpy(temp,stu[j-1].no);
strcpy(stu[j-1].no,stu[j].no); strcpy(stu[j].no,temp);
strcpy(temp,stu[j-1].name); strcpy(stu[j-1].name,stu[j].name);
strcpy(stu[j].name,temp); p=&stu[j-1].age; q=&stu[j].age; s=*q; *q=*p; *p=s;
strcpy(ctemp,stu[j-1].sex); strcpy(stu[j-1].sex,stu[j].sex);
strcpy(stu[j].sex,ctemp); x=&stu[j-1].C; y=&stu[j].C; z=*x; *x=*y; *y=z;
a=&stu[j-1].M; b=&stu[j].M; c=*a; *a=*b; *b=c; k=&stu[j-1].E; l=&stu[j].E;
h=*k; *k=*l; *l=h; } } } // Insert student information function void insert() { int i;
printf(" Please input the number of students to be added :"); scanf("%d",&n); FILE * fp;
if((fp=fopen("student.txt","at"))==NULL) printf(" The file is empty !\n"); for(i=0; i<n; i++)
{ printf("\n Please enter page %d Student number of a student :",i+1); scanf("\n%s",&stu[i].no);
printf("\n Please enter page %d The names of the students :",i+1); scanf("%s",&stu[i].name); printf("\n Please enter page %d
The gender of each student :",i+1); scanf("%s",&stu[i].sex); printf("\n Please enter page %d The age of a student :",i+1);
scanf("%d",&stu[i].age); printf("\n Please enter page %d A student's C language achievement :",i+1);
scanf("%d",&stu[i].C); printf("\n Please enter page %d A student's advanced mathematics achievement :",i+1);
scanf("%d",&stu[i].M); printf("\n Please enter page %d Students' College English scores :",i+1);
scanf("%d",&stu[i].E); sort(); // Call sort by student number function // write file fprintf(fp,"%s %16s %10s %10d
%10.1f %10.1f
%10.1f\n",stu[i].no,stu[i].name,stu[i].sex,stu[i].age,stu[i].C,stu[i].M,stu[i].E);
} printf("\n Added successfully !!!\n"); fclose(fp); } // Delete information by student number void del() { int j=0; FILE *
fp; if((fp=fopen("student.txt","r"))==NULL) return ; while(fscanf(fp,"%s %s %s
%d %f %f %f
",&stu[j].no,&stu[j].name,&stu[j].sex,&stu[j].age,&stu[j].C,&stu[j].M,&stu[j].E)==7)
{ j++; } n=j; int i,flag=0; char s1[LEN+1]; printf(" Please enter the student number of the student you want to delete :\n");
scanf("%s",s1); for(i=0; i<n; i++) { if(strcmp(stu[i].no,s1)==0)
// Compare the input student number with the record student number { flag=1; for(j=i; j<n-1; j++) { stu[j]=stu[j+1];
// The student record behind moves forward } } } if(flag==0) // The input student number is inconsistent with the recorded student number printf(" The student number does not exist !\n");
if(flag==1) // judge flag The value of is 1, Then it is deleted successfully { n--; // After successful deletion , Number of students -1 fileWrite(); fclose(fp);
printf(" Successfully deleted , Please select the menu to display the results \n"); } } // Display current information void display() { int i=0; FILE * fp;
if((fp=fopen("student.txt","r"))==NULL) printf(" The file is empty !\n"); while(fscanf(fp,"%s
%s %s %d %f %f
%f",stu[i].no,stu[i].name,stu[i].sex,&stu[i].age,&stu[i].C,&stu[i].M,&stu[i].E)==7)
{ i++; } n=i; // Record the number of rows of data in the file printf(" Student number Student name Age Gender C language achievement Advanced mathematics College English scores \n");
printf("--------------------------------------------------------------------\n");
for(i=0;i<n;i++) // Output student information in turn { printf("%s %16s %10s %10d %10.1f %10.1f
%10.1f\n",stu[i].no,stu[i].name,stu[i].sex,stu[i].age,stu[i].C,stu[i].M,stu[i].E);
} fclose(fp); } // Save current student information void fileWrite() { FILE *fp; int i;
if((fp=fopen("student.txt","w"))==NULL) printf(" The file is empty !\n"); for(i=0; i<n; i++) {
fprintf(fp,"%s %16s %10s %10d %10.1f %10.1f
%10.1f\n",stu[i].no,stu[i].name,stu[i].sex,stu[i].age,stu[i].C,stu[i].M,stu[i].E);
} fclose(fp); printf(" Saved successfully !!!"); } // Interface void menu() { int num; printf(" \n\n
\n\n"); printf(" * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n");
printf(" * Student information management system *\n"); printf(" * *\n"); printf(" * * * * * * * * System function menu * *
* * * * * *\n"); printf(" * -------------------------------------------- *\n");
printf(" * *\n"); printf(" * 0. System help and description | 1. Refresh student information *\n"); printf(" * *\n");
printf(" * 2. Query student information | 3. Modify student information *\n"); printf(" * *\n"); printf(" * 4. Add student information |
5. Delete information by student number *\n"); printf(" * *\n"); printf(" * 6. Display current information | 7. Save current student information *\n");
printf(" * *\n"); printf(" * 8. Exit the system *\n"); printf(" * * * * * * * * * * * * * *
* * * * * * * * * * * * * * *\n"); printf("- - - - - - - - - - - - - - - - - -
- - - - - - - - - - - \n"); printf(" Please select the menu number :"); scanf("%d",&num); switch(num) {
case 0: help(); break; case 1: update(); break; case 2: seek(); break; case 3:
modify(); break; case 4: insert(); break; case 5: del(); break; case 6:
display(); break; case 7: fileWrite(); break; case 8: k=0; break; default:
printf(" Please check in the 0-8 Choose between \n"); } } // Principal function int main() { while(k) { menu(); }
system("pause"); return 0; }
● Experience induction
●● Problems encountered
The program cannot read the file information : The concept of document is not clear , In the process of reading files, the data cannot be separated by commas
●● Algorithm optimization
<1> Write the main function after the sub function , Delete function declaration
<2> Using structure , Make student information more structured
<3> New sorting function by student number , After adding student information , Call the function , Sort the students' information according to the student number in the file
<4> Optimize the function of deleting student information , Move the student record directly forward
<5> Add the file reading function to every sub function involving file , The purpose is not to perform the refresh function every time
<6> Optimize and modify student information function , Call the save function after each modification , Realize automatic saving
●● experience
Student information management system is what we are learning C Examples of language time , But I feel in the file reading foundation is still relatively weak , Some basic knowledge points still can't be remembered , So in the future, we should practice more exercises related to reading files .
Technology