1. Demand analysis
Student achievement management information system
(1) It is implemented using structure array. N=100;
(2) Based on the structure array, the basic operations such as initialization, insertion, deletion and search are realized;
(3) Using files to save and read student information.
#define STUDSIZE 100 #define SCSIZE 3 struct Student { unsigned int id; //Student number char name[10]; //full name char sex[4]; //Gender unsigned int age; //Age float score[SCSIZE]; //Subject achievement float sum; //Total score float ave; //average }; struct StudentMan { struct Student data[STUDSIZE]; //Grade subjects (Chinese, mathematics, Physics) int cursize; //Number of students int maxsize; //capacity };
2. System design
2.1. Design purpose:
(1)The system interface is simple and easy to operate; (2)Able to load data from disk folder; (3)Information can be saved to disk; (4)The system has the functions of adding, querying, displaying and deleting;
2.2 system functions:
1. Add student information
2. Save student information
3. Load student information
4. Display student information
5. Find student information
6. Delete student information
7. Modify student information
2.3 module division:
1. Add student information module: the module inputs the student number, name, gender, age and test scores of Chinese, mathematics and physics respectively according to the prompt information, and the input information is saved in the structure array.
struct Student InputStudent(struct StudentMan* sp) { assert(sp != NULL); struct Student st = { 0 }; printf("Please enter student ID:\n"); scanf_s("%d", &st.id); while (Find_Id(sp, st.id) != -1) { printf("Duplicate student number entered\n"); printf("Please enter student ID:\n"); scanf_s("%d", &st.id); } rewind(stdin); //Empty buffer printf("Please enter student name:\n"); gets_s(st.name, 10); printf("Please enter student gender:\n"); gets_s(st.sex, 4); while (!(strcmp(st.sex, "male") == 0 || strcmp(st.sex, "female") == 0)) { printf("Please enter the correct student gender: \n"); gets_s(st.sex, 4); } printf("Please enter student age:\n"); scanf_s("%d", &st.age); while (st.age < 14 || st.age>49) { printf("Please enter the correct student age:\n"); scanf_s("%d", &st.age); } printf("Please enter student grade:\n"); for (int i = 0; i < SCSIZE; ++i) { printf("Input No %d Door score: \n", i + 1); scanf_s("%f", &st.score[i]); if (st.score[i] < 0 || st.score[i]>100) { printf("Input No %d Door score: \n", i + 1); scanf_s("%f", &st.score[i]); } st.sum = st.sum + st.score[i]; } st.ave = st.sum / SCSIZE; return st; } bool Add_Student(struct StudentMan* sp) { assert(sp != NULL); if (Is_Full(sp)) return false; struct Student st = InputStudent(sp); sp->data[sp->cursize] = st; sp->cursize += 1; return true; }
Because the structure array contains random values, which is inconvenient to use, so we need a function to initialize, obtain the number of students, judge empty and judge full!
void Init_StudentMan(struct StudentMan* sp) { assert(sp != NULL); sp->cursize = 0; sp->maxsize = STUDSIZE; memset(sp->data, 0, sizeof(struct Student) * sp->maxsize); } int Get_StudentSize(const struct StudentMan* sp) { assert(sp != NULL); return sp->cursize; } bool Is_Full(const struct StudentMan* sp) { assert(sp != NULL); return Get_StudentSize(sp) == sp->maxsize; } bool Is_Empty(const struct StudentMan* sp) { assert(sp != NULL); return Get_StudentSize(sp) == 0; }
2. Search student information module: This module can query by student number and name;
int Find_Id(const struct StudentMan* sp, int id) { int pos = -1; if (NULL == sp) return pos; for (int i = 0; i < sp->cursize; ++i) { if (sp->data[i].id == id) { pos = i; break; } } return pos; } int Find_Name(const struct StudentMan* sp, char* name) { int pos = -1; if (NULL == sp || NULL == name) return pos; for (int i = 0; i < sp->cursize; ++i) { if (strcmp(sp->data[i].name,name) == 0) { pos = i; break; } } return pos; } void Find_Student(const struct StudentMan* sp) { assert(sp != NULL); int id; char name[10]; int selete = 0; int pos = -1; do { printf("****************\n"); printf("* 1.Query by student number *\n"); printf("* 2.Query by name *\n"); printf("* 0.Exit query *\n"); printf("****************\n"); printf("Please enter your choice\n"); scanf_s("%d", &selete); switch (selete) { case 0: printf("Exit query\n"); break; case 1: printf("Please enter the student number to query:\n"); scanf_s("%d", &id); if ((pos = Find_Id(sp, id)) != -1) { PrintStudent(sp,pos); } else { printf("No such student information\n"); } break; case 2: printf("Please enter the name of the student to query:\n"); scanf_s("%s", &name,10); if ((pos = Find_Name(sp, name)) != -1) { PrintStudent(sp,pos); } else { printf("No such student information\n"); } break; default: printf("Input error, please select again"); break; } } while (selete != 0); }
Now that you have found it, of course, you need to print out this student information~
void PrintStudent(const struct StudentMan* sp,int Index) { if (NULL == sp || Index<0 || Index>sp->cursize) return; printf("*************Student information**************\n"); printf("Student number: %d\n", sp->data[Index].id); printf("full name: %s\n", sp->data[Index].name); printf("Gender: %s\n", sp->data[Index].sex); printf("Age: %d\n", sp->data[Index].age); printf("grade scores of Chinese: %.2f\n", sp->data[Index].score[0]); printf("Mathematics achievement: %.2f\n", sp->data[Index].score[1]); printf("Physics achievement: %.2f\n", sp->data[Index].score[2]); printf("Total score: %.2f\n", sp->data[Index].sum); printf("average: %.2f\n", sp->data[Index].ave); printf("***********************************\n"); }
3. Modify the student information module: This module can delete by student number and name;
void ModifyStudent(struct StudentMan* sp) { assert(sp != NULL); char name[10]; char sex[4]; int age; int selete = 0; int id; int pos; char ch; do { printf("Please enter and modify the student number\n"); scanf_s("%d", &id); pos = Find_Id(sp, id); if (pos != -1) { do { if (pos != -1) { printf("* %s Student information modification procedure *\n",sp->data[pos].name); printf("****************************\n"); printf("* 1.Modify name *\n"); printf("* 2.Modify gender *\n"); printf("* 3.Modify age *\n"); printf("* 0.Exit current student information modification *\n"); printf("****************************\n"); printf("Please enter your options\n"); scanf_s("%d", &selete); switch (selete) { case 0: printf("Exit current student information modification\n"); break; case 1: printf("Please enter the name you need to modify\n"); scanf_s("%s", &name,10); strcpy(sp->data[pos].name, name); printf("Modified successfully\n"); break; case 2: printf("Please enter the gender you need to modify\n"); scanf_s("%s", &sex,4); while (!(strcmp(sex, "male") == 0 || strcmp(sex, "female") == 0)) { printf("Please enter the correct student gender: \n"); scanf_s("%s", &sex, 4); } strcpy(sp->data[pos].sex, sex); printf("Modified successfully\n"); break; case 3: printf("Please enter the age you need to modify\n"); scanf_s("%d", &age); sp->data[pos].age = age; printf("Modified successfully\n"); break; default: printf("There is no such attribute to modify\n"); break; } } } while (selete != 0); printf("* Revised student information *\n"); PrintStudent(sp, pos); } else { printf("No such student found"); } printf("Continue to modify the information of other students( Y/N)\n"); rewind(stdin); ch = getchar(); } while (ch == 'Y' || ch == 'y'); } void ModifyScore(struct StudentMan* sp) { assert(sp != NULL); float cn; int selete = 0; int id; int pos; char ch; do { printf("Please enter and modify the student number\n"); scanf_s("%d", &id); pos = Find_Id(sp, id); if (pos != -1) { do { if (pos != -1) { printf("* %s Student achievement information revision procedure *\n", sp->data[pos].name); printf("********************************\n"); printf("* 1.Revise Chinese scores *\n"); printf("* 2.Revise math scores *\n"); printf("* 3.Modify physics scores *\n"); printf("* 0.Exit current student grade modification *\n"); printf("********************************\n"); printf("Please enter your options\n"); scanf_s("%d", &selete); switch (selete) { case 0: printf("Exit current student grade modification\n"); break; case 1: printf("Input Chinese score: \n"); scanf_s("%f", &cn); sp->data[pos].score[0] = cn; printf("Modified successfully"); break; case 2: printf("Enter math scores: \n"); scanf_s("%f", &cn); sp->data[pos].score[1] = cn; printf("Modified successfully\n"); break; case 3: printf("Enter physics scores: \n"); scanf_s("%f", &cn); sp->data[pos].score[2] = cn; printf("Modified successfully\n"); break; default: printf("There is no score for this subject, which can be modified\n"); break; } } } while (selete != 0); sp->data[pos].sum = 0; for (int i = 0; i < SCSIZE; ++i) { sp->data[pos].sum += sp->data[pos].score[i]; } sp->data[pos].ave = sp->data[pos].sum / SCSIZE; printf("Output the modified student information\n"); PrintStudent(sp, pos); } else { printf("No such student found"); } printf("Do you want to continue to modify the grades of other students( Y/N)\n"); rewind(stdin); ch = getchar(); } while (ch == 'Y' || ch == 'y'); }
4. Delete student information module: This module can delete by student number and name;
void Remove_Student(struct StudentMan* sp,int pos) { if (NULL == sp) return; if(pos<0 || pos>sp->cursize - 1) return; for (int i = pos; i < sp->cursize - 1; ++i) { sp->data[i] = sp->data[i + 1]; } sp->cursize -= 1; } void Del_Student(struct StudentMan* sp) { assert(sp != NULL); int id; char name[10]; int selete = 0; int pos = -1; do { printf("****************\n"); printf("* 1.Delete by student number *\n"); printf("* 2.Delete by name *\n"); printf("* 0.Exit delete *\n"); printf("****************\n"); printf("Please enter your options\n"); scanf_s("%d", &selete); switch (selete) { case 0: printf("Exit delete program\n"); break; case 1: printf("Please enter the student number to delete:\n"); scanf_s("%d", &id); if ((pos = Find_Id(sp, id)) != -1) { PrintStudent(sp, pos); printf("Are you sure to delete(Y/N):\n"); rewind(stdin); char ch = getchar(); //rewind(stdin); if (ch == 'y' || ch == 'Y') { Remove_Student(sp, pos); printf("Deleted successfully\n"); } else { break; } } else { printf("No such student information"); } break; case 2: printf("Please enter the name of the student to delete:\n"); scanf_s("%s", &name, 10); if ((pos = Find_Name(sp, name)) != -1) { PrintStudent(sp, pos); printf("Are you sure to delete(Y/N):\n"); rewind(stdin); char ch = getchar(); //rewind(stdin); if (ch == 'y' || ch == 'Y') { Remove_Student(sp, pos); printf("Deleted successfully\n"); } else { break; } } else { printf("No such student information"); } break; default: printf("Input error, please select again"); break; } } while (selete != 0); }
5. Student information display module: This module displays all student information in the array;
void Show_Student(const struct StudentMan* sp) { assert(sp != NULL); if (Is_Empty(sp)) return; printf("---------------------------------------------------------------------------------\n"); printf(" Student number | full name | Gender | Age | language | mathematics | Physics | Total score | average |\n"); // 9 9 9 9 ... for (int i = 0; i < sp->cursize; ++i) { printf("%5d", sp->data[i].id); printf("%8s", sp->data[i].name); printf("%8s", sp->data[i].sex); printf("%9d", sp->data[i].age); for (int j = 0; j < SCSIZE; ++j) { printf("%10.2f", sp->data[i].score[j]); } printf("%10.2f", sp->data[i].sum); printf("%8.2f", sp->data[i].ave); printf("\n"); printf("---------------------------------------------------------------------------------\n"); } }
6. Save student information module: the module saves the student information stored in the array to the disk file;
void Save_Student(const struct StudentMan* sp) { assert(sp != NULL); //Open file FILE* fp = fopen("student.data", "wb"); if (NULL == fp) { printf("fail to open file\n"); return; } // fwrite(&sp->cursize, sizeof(int), 1, fp); // fwrite(sp->data, sizeof(struct Student), sp->cursize, fp); // fclose(fp); fp = NULL; }
7. Load the student information module: the module reads the student information saved in the disk file into the array of memory;
void Load_Student(struct StudentMan* sp) { assert(sp != NULL); FILE* fp = fopen("student.data", "rb"); if (NULL == fp) { printf("fail to open file\n"); return; } fread(&sp->cursize, sizeof(int), 1, fp); int n = sp->cursize; fread(sp->data, sizeof(struct Student), n, fp); fclose(fp); fp = NULL; }
The simple student management system has been completed. If you need a complete code, you can drop me~