当前位置:网站首页>C language student management system - can check the legitimacy of user input, two-way leading circular linked list

C language student management system - can check the legitimacy of user input, two-way leading circular linked list

2022-06-24 06:41:00 chfens

It's been a long time .. There are a lot of things to sum up and write a blog , My blog ToDo List and a lot of items , Make up little by little ..
One of the courses in this semester is to design a student management system , Store students' basic information and grades . I use C Written language , Studying recently C++, Looking back at this system, I feel very uncomfortable ,C++ The feature of can reduce a lot of redundant code .

function

Add, delete, check, modify and sort , Data can be saved to disk , Read the file information every time you initialize .

Storage

The data in memory is Double headed circular linked list Storage , Therefore, the insertion and deletion time complexity is O(1), Data can be saved in Local disk Of txt In file , You can modify the storage path in your code .

The main points of

Put forward some key points that should be paid attention to when designing this system :

  • The use of data structures

Here I use the two-way leading circular linked list , The reader can modify it to Single chain list , I just used this data structure to make my code different from my classmates , In terms of efficiency, it is impossible to distinguish . Designing this system requires you to be familiar with the linked list structure , Be able to write, add, delete, check and modify by yourself .

  • Input validity

At first, there was no formal judgment , Just directly read the data entered by the user , So I was scolded by the teacher , But the teacher didn't tell us before = =.
Input judgments include students Information input The legality of and Option selection The legitimacy of time .

When users enter information, they may naughty , For example, input the student ID in Chinese , Name, student ID, etc , Therefore, it is necessary to check whether the format is correct , If it is not correct, continue to lose , Until you lose right .
When choosing Options , For example, I 5 Options from 1 To 5, But you entered a number other than , Or you enter a character , Because I use scanf Read options , If a character is entered, it will not be read , If set in while It will cause jamming , Because characters will always Stay in the buffer .

  • Modular programming

Keep modularity in mind when designing , Functions should be highly cohesive and low coupling , You can reuse it when you want to use it .

( The following content is directly copied from my experiment report )

Let's start with student ID judgment :

Student ID is used The character array stores Of , It is necessary to judge whether the student number entered is Pure number , Letters or symbols are not allowed , And the student number shall be five digits , The range of student ID allowed to be entered is 0 To 10^5 - 1, Considering the above two points , When inputting student information, first use an integer data to receive the data input by the user . If the format is correct , Then the data will be modulo each time 10 Followed by the characters 0 Of ASCII value , That is, the last bit of the integer is converted into character data and stored at the end of the array , The student ID can be stored . If you use int Type receive user input , Problems will occur when the data value entered by the user is too large , So choose long long int Type storage . When the number is successfully entered and stored in stuNumber After... In the array , call isExistStu() Function to check whether a student with this student number already exists . If all the above conditions can pass , Then execute the following procedure .

Name judgment :

Name use The character array stores , Only users are allowed to enter Chinese characters , Letters... Are not allowed 、 Numbers or symbols . Name word count support 2 To 4 position , First, judge whether the user enters characters or numbers , If not , It is necessary to further determine whether to input Chinese characters with non Chinese symbols .
Set the current environment Coding format by Simplified Chinese (GB2312), It is known that a Chinese character or a Chinese symbol in the character set under this encoding format occupies Two bytes , And of each byte highest All for 1, Therefore, it is possible to distinguish whether the contents of the space are Chinese characters . use for Loop to judge whether the user input is pure Chinese , If it is not in Chinese, let the user re-enter . If it's in Chinese , Then check whether there are Chinese symbols .
It is known that GB2312 Code the characters included “ Partition ” Handle , common 94 Districts , Each zone contains 94 bits , common 8836 A code bit .01-09 District Include all the characters except Chinese characters 682 Characters .GB2312 It is specified that each character included shall be represented by two bytes , The first byte is “ High byte ”, Corresponding 94 Districts ; The second byte is “ Low byte ”, Corresponding 94 bits . So its location code range is :0101-9494. Add... To the area code and tag number respectively 0xA0 Namely GB2312 code . Therefore, when characters other than Chinese characters are stored , The high-level content is A0-A9, You can check the high order of each Chinese input when stored in the array to determine whether it is a symbol . If all the above conditions can pass , Then execute the following procedure .

Various achievements :

Grades are stored in single precision floating-point variables , It is also not allowed to enter characters or symbols , The score range is 0-100, At most one decimal place , And the decimal part can only be 0.0 or 0.5. use modf() Function takes the decimal part of the input number to determine whether it is 0.5 or 0.0, If not, re-enter .
 Insert picture description here
 Insert picture description here

Sort student grades

Select... From the main menu 8 When sorting student information , call GetNumStu Function to get the number of information in the linked list , Then open up two arrays of equal size , Array theScore Save results , Array theNode Where the student information is stored The address of the node . Then prompt the user to enter the options to sort . Then call SortStu function , With Student grades are indexed , Turn the ranking of students' grades into the ranking of corresponding nodes . First, the students' scores to be sorted are stored in theScore in , At the same time, the corresponding student nodes are stored in theNode in . After through Insertion sort The results are sorted and modified at the same time theNode The order of nodes in . After sorting, return to . Cycle call PrintStu Print structure pointer array theNode The information of middle school students can get the ranking results of grades , Release the two opened arrays after printing , Then return to the main menu interface .

file save

use fread and fwrite function , instead of fscanf and fprintf, Because it's inconvenient to use .

I'm not going to post the code here , Upload the code to my code cloud , Take it from yourself if you need it .
Student management system source code
Any questions about the code can be raised in the comments section !
 Insert picture description here

Conclusion

for C A small test of language , I hope I can have the motivation to continue my study , Continue to blog more !!!! To achieve a little , From “ Youheng ” Start with two words .

原网站

版权声明
本文为[chfens]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206240010142636.html

随机推荐