1, This goal
Complete all preparations before the core header file, including the creation of a new project from VS2022 to the preparation of the first custom header file.
GitHub: https://github.com/ITchujian/StudentManagementSystem_2022_C
Note: in order to share the experience of this development, I will put the analysis process and code writing process in the development record in the form of words and pictures, but I will not explain or explain some very basic actions.
Current location: [1.1 C case] please write a thousand lines student management system with me in C language
Jump:
- [1C case] please write a thousand line student management system with me in C language
- [1.2 case C] please write a thousand lines student management system with me in C language
- [1.3 case C] please write a thousand line student management system with me in C language
2, Development record
Step 1
Click create new project, and then select an empty project in the new window to continue to the next step.
Step 2
Here, we change the project name to StuMS, uncheck it, put the solution and project in the same directory, and click create.
After creation, the following interface will be displayed.
Step 3
Right click the source file in solution explorer to create a new SMS_2022.cpp procedure, attention cpp file is the source file of C + +. In fact, VS only supports the creation of C + + projects, but not c projects. After all, 99% of the time, C + + is fully compatible with the C language we write. Here, I won't dig for myself to sort out the difference of 1%. I'll mention it later.
Step 4
We don't rush to write code here. Don't forget our purpose. according to The design idea at the beginning , we will refer to many standard library header files in C, such as stdio h,io.h et al. Secondly, we need to define many variables and a small number of structures. Then, we might as well customize them into a header file as the pre configuration file of the compiler. Then, according to the above method, add a new item to the header file in the solution explorer and select the header file with the name of preconf H (pre means first and first, conf means configuration).
Step 5
Next, we begin to write the project. Our first line of code:
#pragma once
In order to prevent various problems of macro name conflict in different header files, C/C + + language provides us with two schemes:
Example:#pragma once / / scheme 1 Example:#ifndef __ Uppercase header file name_ H__ // Option 2
Since scheme 1 is more popular with programmers than before, it has been developed to 2022. In fact, both have been widely used. In contrast, both have their own advantages. Let's just learn about it here. Scheme 1 is not supported by some very old compilers, while scheme 2 is old-fashioned and has strong portability, If you need to know more about them, here are Wikipedia,Baidu Encyclopedia,academic.
Step 6
Make an explanatory note for the header file and directly quote the file header note in the previous development specification. In the following pages and other articles, the note is no longer emphasized separately. For the first time, it needs to be emphasized because the writing of the program must be standardized everywhere, which is the artistic beauty of the appearance of the code. Of course, you can also say that my thousand lines of code are probably annotated 😂~
#pragma once / / Solve various strange problems caused by macro names /********************************************************************* * Reprint please indicate the source * @FileName preconf.h * @Description Preconfigured header files * @History * version author data introduction and operations * 1.0 Initial view: create on January 23, 2022 * *** *** ****-**-** ******* */
Step 7
Next, reference the standard library header file.
#include <ctype.h> #include <string.h> #include <stdio.h> #include <malloc.h> #include <stdlib.h> #include <math.h> #include <process.h> #include <io.h> #include <direct.h> /********************************************************************* * @chujian(cn) Import standard library header file * @chujian(en) Import standard library headers */
Step 8
We want to use the linear list in the data structure. Let's have a brief understanding here. I define it as follows:
In practice, the physical address and the logical address are one-to-one corresponding to the finite sequence stored in sequence
In life, the English alphabet (A, B, C,..., Y, Z) is a linear table.
Each table has several data elements of the same type. Each data element can have several data items of different types. The data element can be called record, while a table can be called file. Is it So easy?
In this project, our total student information is a table, and there are many students in this table. Each student can have several data items. Here, we define them one by one to facilitate subsequent development:
- Linear table structure: SqList
- Data element structure: ElemType
- System setting: SysConfig
In order to improve its reusability, the above names should not be named in the relevant direction of development projects in a hurry.
The SqList structure shall include: - The structure of the data element
- Linear table length, corresponding to several data elements
- Allocated storage capacity in sizeof(ElemType)
The ElemType structure should contain: - full name
- Student number (unique in real life)
- Gender
- Age
- Chinese score
- Math score
- English score
- Average score (automatic calculation)
- Total score (calculated automatically)
SysConfig structure, which shall include: - Storage file path
- Backup file path
- File path of student information table
- Console color settings
The path and color settings in the SysConfig structure will be read at the beginning, so we should load it first from the main entry, so we need to give it a small initialization in the code.
If it is in the form of code, it is as follows:
typedef struct { char name[20]; int num; char sex[4]; int age; float score_literature; float score_math; float score_english; float average_score; float sum_score; } ElemType; /********************************************************************* * @chujian(cn) Define the student information structure, including name, student number, etc * @chujian(en) Define the student information structure, including name, student ID, etc. */ typedef struct { ElemType* elem; int length; int list_size; } SqList; /********************************************************************* * @chujian(cn) Sequential storage structure * @chujian(en) Dynamic allocation sequential storage structure of linear table */ typedef struct { char file_path[128]; char backup_path[128]; char list_path[128]; char sys_color[32]; } SysConfig; SysConfig config_bin = {}; /********************************************************************* * @chujian(cn) Settings outside compilation * @chujian(en) Dynamic allocation sequential storage structure of linear table */
Of course, we need to define the initial value of storage space, which is temporarily set to 128. When the storage space is insufficient, we will add a space increment for it. Each time it is insufficient, it will be increased according to this amount and memory demand, which is set to 16.
#define LIST_INIT_SIZE 128 #define LIST_INCREMENT 16 /********************************************************************* * @chujian(cn) The initial allocation amount of configuration storage space is 128, with an increment of 16. The unit is sizeof(ElemType) * @chujian(en) The initial allocation of configuration storage space is 128, in increments of 16, and the unit is sizeof(ElemType) */
Furthermore, according to the design requirements, we should also define two types, Boolean and Status, as well as the Status values under them.
typedef int Boolean; #define TRUE 1 #define FALSE 0 typedef int Status; #define OK 1 #define ERROR 0 #define INFEASIBLE -1 #define OVERFLOWED -2 /********************************************************************* * @chujian(cn) Define Boolean, function return status value * @chujian(en) Define boolean type, function return status value */
preconf.h の complete code:
#pragma once / / Solve various strange problems caused by macro names /********************************************************************* * Reprint please indicate the source * @FileName preconf.h * @Description Preconfigured header files * @History * version author data introduction and operations * 1.0 Initial view: create on January 23, 2022 * *** *** ****-**-** ******* */ #include <ctype.h> #include <string.h> #include <stdio.h> #include <malloc.h> #include <stdlib.h> #include <math.h> #include <process.h> #include <io.h> #include <direct.h> /********************************************************************* * @chujian(cn) Import standard library header file * @chujian(en) Import standard library headers */ #define LIST_INIT_SIZE 128 #define LIST_INCREMENT 16 /********************************************************************* * @chujian(cn) The initial allocation amount of configuration storage space is 100, with an increment of 10. The unit is sizeof(ElemType) * @chujian(en) The initial allocation of configuration storage space is 100, in increments of 10, and the unit is sizeof(ElemType) */ typedef int Boolean; #define TRUE 1 #define FALSE 0 typedef int Status; #define OK 1 #define ERROR 0 #define INFEASIBLE -1 #define OVERFLOWED -2 /********************************************************************* * @chujian(cn) Define Boolean and return status value of function through enumeration * @chujian(en) Through enumeration, define boolean type, function return status value */ typedef struct { char name[20]; int num; char sex[4]; int age; float score_literature; float score_math; float score_english; float average_score; float sum_score; } ElemType; /********************************************************************* * @chujian(cn) Define the student information structure, including name, student number, etc * @chujian(en) Define the student information structure, including name, student ID, etc. */ typedef struct { ElemType* elem; int length; int list_size; } SqList; /********************************************************************* * @chujian(cn) Sequential storage structure * @chujian(en) Dynamic allocation sequential storage structure of linear table */ typedef struct { char file_path[128]; char backup_path[128]; char list_path[128]; char sys_color[32]; } SysConfig; SysConfig config_bin = {}; /********************************************************************* * @chujian(cn) Settings outside compilation * @chujian(en) Dynamic allocation sequential storage structure of linear table */