How to implement the student management system ? First of all, we should be able to analyze the framework of the problem . In this way, when writing later, you will have a clear understanding of what you need .

        that , The task of the management system is : Can delete , Find and modify student information , Can sort , Can print information .

         These are the most basic functions , Write these functional frameworks in a c In the source file , Run area as main function .
#include"StudentManager.h" enum Option { EXIT, ADD, DEL, SEARCH, MODIFY, SORT,
PRINT }; int main() { int input = 0; // Create personnel list Student stulist;// Personnel list // Initialize personnel list
InitStudent(&stulist); do { menu(); printf(" Please select :\n"); scanf_s("%d", &input);
switch(input) { case ADD: AddStudent(&stulist);// Modify a certain transmission address break; case DEL:
DelStudent(&stulist); break; case SEARCH:
SearchStudent(&stulist);// Can transfer value , But the efficiency of address transmission is higher break; case MODIFY:
ModifyStudent(&stulist); break; case SORT: SortStudent(&stulist); break; case
PRINT: PrintStulist(&stulist); break; case EXIT: printf(" Exit management system \n"); default:
break; } } while (input); SaveFile(&stulist); }
========================================================================= 

         then , Is a separate file , Implement the declaration of the function used , In order to make the business logic clearer :( At the same time, try to define the required storage types here , In this way, it will be clearer when writing function functions )
#pragma once #include<stdio.h> #include<stdlib.h> #include<string.h> // Structure definition
typedef struct StuInfo { //(1) Each student's personal information includes : full name , Student number , Native place , Age , mathematics , English , Physics achievement char name[15];
char address[20]; char ID[15]; int age; int score[3]; int sum; int order;
}StuInfo; // Encapsulate the structure typedef struct Student { StuInfo data[100]; // Stored personnel information int
size; // Record the current number of people }Student; int FindByName(Student* pc, char name[]); void
InitStudent(Student* pc); void AddStudent(Student* pc); void
DelStudent(Student* pc); void SearchStudent(Student* pc); void
ModifyStudent(Student* pc); void SortStudent(Student* pc); void
SaveFile(Student* pc); void PrintStulist(const Student* pc); void menu();
 =========================================================================

         after , This is the realization of the function , Before implementing the function , We have determined how to store the student information we write . therefore , Is the realization of function function .

First, the most basic is the menu interface :
// Display menu interface void menu() { printf("---------------\n"); printf("1. increase 2. Delete \n");
printf("3. check 4. change \n"); printf("5. Row 6. Print \n"); printf(" 0. sign out \n");
printf("---------------\n"); }
========================================================================= 

    Then write a function to initialize the space ( Not much explanation ):
// Initialization data , For use void InitStudent(Student* pc) { pc->size = 0; memset(pc->data, 0,
sizeof(pc->data));// Initialize all data to 0 }
         Then there is the realization of some functions , For example, let's implement a printing function first , Because the code of printing function can also be used and reflected in other functions ( For example, find ):

        Pointer pc Want to dereference the address , You need to use ' -> ', Others are ' .
'. So the operation of printing information is clear at a glance , First, the pointer dereferences the information of a single student , Then the structure takes out the corresponding information data .
// Print information void PrintStulist(const Student* pc) { int i = 0; // Print title
printf("%-10s\t%-10s\t%-10s\t%-10s\t%-20s\n", " full name ", " Student number ", " Native place ", " Age ",
" achievement : mathematics , English , Physics ,"); for (i = 0; i < pc->size; i++) {
printf("%-10s\t%-10s\t%-10s\t%-10d\t%d\t%d\t%d\n", pc->data[i].name,
pc->data[i].ID, pc->data[i].address, pc->data[i].age, pc->data[i].score[0],
pc->data[i].score[1], pc->data[i].score[2]);// Three grades entered } }
       
here , You need to briefly explain this function , use const The read-only attribute is because we accept the information of the student list , However, in order to ensure that the student list information will not be misoperated ( Operations other than printing ), So add const. Then someone might ask , Since I'm afraid of being modified , Why not directly define a common variable to receive student information , Instead, use a pointer ? Then please think about it :
The pointer accepts an address , Ordinary variables accept all the information , Which is more efficient ? Of course, the efficiency of address transmission is high .

=========================================================================

       
Then there are the added functions , A new student came to the class , You have to re-enter the information , So what does the added function need ? Of course, it is to modify the corresponding information ( When encapsulating the student list defined here , Defined as 100, of course , You can also choose #define Macro definition , For later maintenance ):
// Add personnel information void AddStudent(Student* pc) { if (pc->size == 100) {
printf(" The list is full , Cannot add \n"); return; } // Add information printf(" Please enter your name :\n"); scanf_s("%s",
pc->data[pc->size].name, 15); printf(" Please enter student number :\n"); scanf_s("%s",
pc->data[pc->size].ID, 15); printf(" Please enter your native place :\n"); scanf_s("%s",
pc->data[pc->size].address, 20); printf(" Please enter age :\n"); scanf_s("%d",
&(pc->data[pc->size].age)); printf(" Please enter math , English , Physics achievement :\n"); scanf_s("%d %d %d",
&(pc->data[pc->size].score[0]), &(pc->data[pc->size].score[1]),
&(pc->data[pc->size].score[2])); pc->size++; printf(" Information added successfully \n"); }
        The operation of adding is similar to that of printing , No more explanation .

=========================================================================

         that , Look at the remaining features , There are modifications , Delete and find , Let's analyze these three functions carefully , All need to find a student , therefore , Let's write a search function first (
Find can only be found sequentially , Go one by one to see which meets the conditions , So use for perhaps while):
// Define a search function , For deletion , Find use int FindByName(Student* pc, char name[]) { int i = 0;
for (i = 0; i < pc->size; i++) { if ((strcmp(pc->data[i].name, name)) == 0) {
return i; } } return -1; }
        Name search is used here , You can also change the search function to achieve more advanced operations , For example, find someone according to your grades , according to ID look for sb. ( The essence is the same ).

=========================================================================

        According to the function of this search , We can delete and modify , When a student commits a heinous crime , Then you have to drop out , So let's delete it :

        Pay attention here , Because the student's information has been changed , So you have to send an address .
// Delete personnel information void DelStudent(Student* pc) { char name[15] = { 0 }; if (pc->size ==
0) { printf(" The list is empty , No information can be deleted \n"); return; } //1. Find people to delete printf(" Please enter the person to delete :\n");
scanf_s("%s", name, 15); int position = FindByName(pc, name); /* have / No, --> 2. delete
*/ if (position == -1) { printf(" The person to delete does not exist "); return; } // delete int i = 0; for (i =
position; i < pc->size - 1; i++)//size-1 Because the last left element doesn't have to be overwritten , Later code deletion { pc->data[i] =
pc->data[i + 1];// Overwrite in turn to move forward and delete } pc->size--; printf(" Deleted successfully \n"); }
       
The implementation of deletion is quite simple , Because in fact, it is equivalent to finding the student , Then move the whole table forward , Just cover this student directly ( no way out , The defect of sequence table is the high time complexity of deletion operation ), meanwhile , When writing, we should also judge whether the students exist or not .

       
What about the lookup operation ? We wrote above about the search function , It's just that the program found this person's information , Really need to find , I have to check this person's information . The students in the class want to know their own information , Find the teacher , It must be necessary to output information , So here we also take off part of the printed code :
// Find the specified person --> Borrowed the code for deleting personnel and the code for printing information void SearchStudent(Student* pc) { char
name[15] = { 0 }; printf(" Please enter the person you want to find :\n"); scanf_s("%s", name, 15); int position
= FindByName(pc, name); if (position == -1) { printf(" The person you are looking for does not exist "); return;
}// The same code as the deleted judgment else// Go print and find the code , Pay attention to changing some contents {
printf("%-10s\t%-10s\t%-10s\t%-10s\t%-20s\n", " full name ", " Student number ", " Native place ", " Age ",
" achievement : mathematics , English , Physics ,"); printf("%-10s\t%-10s\t%-10s\t%-10d\t%d\t%d\t%d\n",
pc->data[position].name, pc->data[position].ID, pc->data[position].address,
pc->data[position].age, pc->data[position].score[0],
pc->data[position].score[1], pc->data[position].score[2]);// Code picked from the print } }
        therefore , Basically, as long as you understand the first few codes , The rest will be solved . Then we still have the modification function related to search :
// Modify the specified personnel information --> Borrowed the code found and added void ModifyStudent(Student* pc)// Transform with the code found {
char name[15] = { 0 }; printf(" Please enter the person to modify :\n"); scanf_s("%s", name, 15); int
position = FindByName(pc, name); if (position == -1) { printf(" The person to modify does not exist ");
return; }// The same code as the deleted judgment else// be careful , Here is the same code as adding { printf(" Please enter your name :\n"); scanf_s("%s",
pc->data[position].name, 15); printf(" Please enter student number :\n"); scanf_s("%s",
pc->data[position].ID, 15); printf(" Please enter your native place :\n"); scanf_s("%s",
pc->data[position].address, 20); printf(" Please enter age :\n"); scanf_s("%d",
&(pc->data[position].age)); printf(" Please enter math , English , Physics achievement :\n"); scanf_s("%d %d %d",
&(pc->data[position].score[0]), &(pc->data[position].score[1]),
&(pc->data[position].score[2])); printf(" Information modified successfully \n"); } }
       
Did you find it ? It's a little like adding information . in short , The functions related to search are basically fully realized , Then the sorting function is not implemented , Because the sorting function has little to do with searching , So it's listed as the next big block .

=========================================================================

       
sort , There are many sorting algorithms that we can use , For example, select sorting , Bubble sorting , Insert sort , Quick sort, etc , Here, a more understandable insertion sorting algorithm is used to sort the students ( Sort by total score ):
// Sort people void SortStudent(Student* pc) { for (int i = 0; i < pc->size; i++) {
pc->data[i].sum = pc->data[i].score[0] + pc->data[i].score[1] +
pc->data[i].score[2]; } printf(" Total score sorting result :\n");// Use insert sort to do this StuInfo p; int i, j;
for (i = 1; i < pc->size; i++) if (pc->data[i].sum > pc->data[i - 1].sum) { p =
pc->data[i]; for (j = i - 1; j >= 0 && pc->data[j].sum < p.sum; j--) pc->data[j
+ 1] = pc->data[j]; pc->data[j + 1] = p; } StuInfo* t; i = 0; for (t =
pc->data; t < pc->data + pc->size; t++) { pc->data[i].order = i + 1; i++; }
// Print out the sorted list printf("%-10s\t%-10s\t%-10s\t%-10s\t%-20s\n", " full name ", " Student number ", " Native place ",
" Age ", " achievement : mathematics , English , Physics ,"); for (int i = 0; i < pc->size; i++) {
printf("%-10s\t%-10s\t%-10s\t%-10d\t%d\t%d\t%d\n", pc->data[i].name,
pc->data[i].ID, pc->data[i].address, pc->data[i].age, pc->data[i].score[0],
pc->data[i].score[1], pc->data[i].score[2]);// Three grades entered } }
       
that , This code only slightly explains the sorting algorithm used . Insert sort , Is to find the maximum value ( You have to cycle through it to find it ), Then put it in the front , Cycle down , You get an order . Specific algorithm , I'll write another one later CSDN Explain it , After all, there are still a lot of sorting contents .

         meanwhile , This function also uses the printed code , after all , Do you have to let others see the order , So print out the information of the whole student list .

       
Write here , All functions have been basically realized . But given that students' information may eventually have to be saved in files , For future use , therefore , Here is a simple file saving function , of course , If readers want to read files to write the management system , That's OK :
// Storage file void SaveFile(Student* pc) { FILE* fp = NULL; fopen_s(&fp,
"studentInfo.txt", "wt+"); //b Indicates that the file is opened in binary mode if (fp == NULL) // fail to open file , Return error message {
printf("open file for write error\n"); } int i = 0; for (i = 0; i < pc->size;
i++) { fprintf(fp, "%-10s\t%-10s\t%-10s\t%-10d\t Three subjects :%d\t%d\t%d\n",
pc->data[i].name, pc->data[i].ID, pc->data[i].address, pc->data[i].age,
pc->data[i].score[0], pc->data[i].score[1], pc->data[i].score[2]); }
fclose(fp); fp = NULL; }
        finally , Our student management system is completed in time , that , Little cuppers, take the code and try it !

Technology
©2019-2020 Toolsou All rights reserved,
【C++ Must see for entry 】C++ from 0 reach 1 Introductory programming axios Interceptor packaging and use Spring Boot Interview must ask : Automatic configuration principle VMware 16 install centos 7 Detailed tutorial C Language data structure - Sequence table delete duplicates V2.0.0 The 12th Blue Bridge Cup c++b Group personal problem solving On sending data from serial port single chip microcomputer to upper computer centos7 install RabbitMqjava Polymorphic array of opencv-python Fourier transform and inverse transform