I. Important C language knowledge involved in this design:
1. operations on a single-chain table
2. file stream operations
3. Sorting of Single-Chain tables
II. This design mainly consists of the following modules:
1. Add Student Score Module
2. Student Score deletion Module
3. Student Score modification module
4. Student Achievement search Module
5. Student Score display module (including score sorting)
6. Student Achievement retention Module
:
This design involves about 5 or 6 hundred lines of code. Through this design, I feel a lot of proficiency in the C language,
I hope it will be helpful for cainiao students like me. Let's talk about the code!
3. The program code 1. Main. c file mainly completes the jump of the main menu and receives user information to complete the jump to each module.
# Include <stdio. h> # include "students. h "int select_menu (void); void hand_menu (int cmd, int * flag); void quit (); int main (int argc, char * argv []) {int cmd; int flag = 0; while (1) {cmd = select_menu (); hand_menu (cmd, & flag) ;}} int select_menu (void) {int select = 0; printf ("************************************* * *********************** \ n "); printf ("Student Management System \ n"); printf ("1: Add Student Information 2: delete student information \ n"); printf ("3: Modify student information 4: query Student Information \ N "); printf (" 5: View student information 6: Save \ n "); printf (" 0: Exit \ n "); printf ("************************************* * ************************ \ n "); printf ("select:"); select = getch (); while (select <'0' | select> '6') {printf ("\ n input error, please reselect: "); select = getch ();} return select;} void hand_menu (int cmd, int * flag) {static STU * stu_head = NULL; if (* flag = 0) {stu_head = init_student (stu_head); * flag = 1;} switch (cmd) {case '0': save_student (stu _ Head); quit (); break; case '1': system ("cls"); stu_head = add_student (stu_head); system ("cls"); break; case '2': system ("cls"); stu_head = del_student (stu_head); system ("cls"); break; case '3': system ("cls "); modify_student (stu_head); system ("cls"); break; case '4': system ("cls"); search_student (stu_head); system ("cls"); break; case '5': system ("cls"); stu_head = display_student (stu_head); system ("cls"); break; case '6': syst Em ("cls"); save_student (stu_head); system ("cls"); break; default: break ;}} void quit () {printf ("\ n thanks for using the student management system! \ N "); exit (0 );}2. The students. h file mainly contains the definitions required for function operations in the students. c file.
#ifndef _STUDENTS_H_#define _STUDENTS_H_typedef struct st{unsigned long ID;char name[10]; float chinese;float math; float english; double all_sub; float average;struct st* next;}STU;#define ASK_STU(p) do{p=(STU *)malloc(sizeof(STU));\if(p==NULL){printf("malloc memory failed!\n");exit(1);}\}while(0)STU *search_by_nb(STU *stu_head, unsigned long nb);STU *search_by_name(STU *stu_head, char name[]);STU *select_sort(STU *g, char flag);STU *add_student(STU *stu_head);STU *del_all(STU *stu_head);STU *del_one(STU *stu_head, int flag);STU *del_student(STU *stu_head);void modify_student(STU *stu_head);STU *display_student(STU *stu_head);STU *search_by_name(STU *stu_head, char name[]);STU *search_by_nb(STU *stu_head, unsigned long nb);#endif
3. Store. h
#ifndef _STORE_H_#define _STORE_H_void save_student(STU *stu_head);STU *init_student(STU *stu_head);#endif
4. The students. c file is the core part of this design, including the implementation functions of each module, including adding, deleting, modifying, searching, and displaying student information.
# Include <stdio. h> # include "students. h "STU * select_sort (STU * g, char flag) {STU * p, * q, * t, * s, * h; h = (STU *) malloc (sizeof (STU); h-> next = g; p = h; while (p-> next! = NULL) {for (s = p, q = p-> next; q-> next! = NULL; q = q-> next) {if (flag = '1') if (q-> next-> ID <s-> next-> ID) s = q; if (flag = '2') if (q-> next-> chinese <s-> next-> chinese) s = q; if (flag = '3') if (q-> next-> math <s-> next-> math) s = q; if (flag = '4 ') if (q-> next-> english <s-> next-> english) s = q; if (flag = '5 ') if (q-> next-> average <s-> next-> average) s = q;} if (s! = Q) {t = s-> next; s-> next = t-> next; t-> next = p-> next; p-> next = t ;} p = p-> next;} g = h-> next; free (h); return g;} STU * add_student (STU * stu_head) {STU * stu; STU * head = stu_head; STU * node = stu_head; STU * node_fd; int sel; ASK_STU (stu); stu-> next = NULL; printf ("Enter the student ID: "); scanf (" % d ", & stu-> ID); while (search_by_nb (node, stu-> ID) {printf (" this student ID already exists! Enter a student ID again: "); scanf (" % d ", & stu-> ID);} printf (" Enter the Student name :"); scanf ("% s", stu-> name); printf ("Enter the student's english score:"); scanf ("% f", & stu-> english ); while (stu-> english <0.0 | stu-> english> 100.0) {printf ("incorrect input. Please enter the english score again:"); scanf ("% f ", & stu-> english);} printf ("Enter the student's mathematical score:"); scanf ("% f", & stu-> math ); while (stu-> math <0.0 | stu-> math> 100.0) {printf ("incorrect input. Please enter a new score:"); scanf ("% f ", & stu-> math);} printf ("Enter the student's Chinese score:"); scanf ("% F", & stu-> chinese); while (stu-> chinese <0.0 | stu-> chinese> 100.0) {printf ("input error, enter the math score again: "); scanf (" % f ", & stu-> chinese );} stu-> all_sub = stu-> chinese + stu-> math + stu-> english; stu-> average = stu-> all_sub/3; if (! Head) {head = stu; return head;} while (node-> next) node = node-> next; node-> next = stu; head = select_sort (head, '1'); return head;} STU * del_all (STU * stu_head) {if (! Stu_head) return NULL; del_all (stu_head-> next); free (stu_head); stu_head = NULL; return NULL;} STU * del_one (STU * stu_head, int flag) {STU * node = stu_head; STU * head = stu_head; STU * pre; char name [10]; unsigned long ID; if (head = NULL) {printf ("no student information! \ N "); return head;} if (flag = '2') {printf (" enter the name of the student to be deleted: "); scanf (" % s ", name); if (! Strcmp (node-> name, name) {free (node); head = node-> next; printf ("deleted successfully! Press any key to return. \ N "); getch (); return head;} while (node) {if (! Strcmp (node-> name, name) {pre-> next = node-> next; free (node); printf ("deleted successfully! Press any key to return. \ N "); getch (); return head;} pre = node; node = node-> next;} printf (" the student information is not found! \ N "); return head;} if (flag = '3') {printf (" Enter the student ID to delete: "); scanf (" % d ", & ID); if (node-> ID = ID) {free (node); head = node-> next; printf ("deleted successfully! Press any key to return. \ N "); getch (); return head;} while (node) {if (! Strcmp (node-> name, name) {pre-> next = node-> next; free (node); printf ("deleted successfully! Press any key to return. \ N "); getch (); return head;} pre = node; node = node-> next;} printf (" the student information is not found! Press any key to return \ n "); getch (); return head ;}} STU * del_student (STU * stu_head) {int sel; STU * head = stu_head; printf (" 1: delete all 2: delete by name 3: delete by student ID 0: Return \ n "); printf (" select \ n "); sel = getch (); while (sel <'0' | sel> '3') {printf ("incorrect input. Please reselect: \ n"); sel = getch ();} if (sel = '0') return head; if (! Head) {printf ("no student information! Press any key to return. \ n "); getch (); return head ;}if (sel = '1') {head = del_all (head); printf (" deleted successfully! Press any key to return. "); getch (); return head;} if (sel = '2' | sel = '3') {head = del_one (head, sel ); return head ;}} void modify_student (STU * stu_head) {STU * head = stu_head; STU * node_fd; int sel; char name [10]; unsigned long nb; recyc3: printf ("1: Modify the student information of the specified student ID 2: Modify the student information of the specified name 0: Return \ n select \ n"); sel = getch (); while (sel <'0' | sel> '2') {printf ("input error! Please reselect: \ n "); sel = getch ();} if (sel = '1' | sel = '2 ') {if (sel = '1') {printf ("Enter the student ID to be modified:"); scanf ("% d", & nb ); node_fd = search_by_nb (head, nb);} if (sel = '2') {printf ("enter the name of the student to be modified :"); scanf ("% s", name); node_fd = search_by_name (head, name);} if (node_fd) {printf ("Enter the student's English score :"); scanf ("% f", & node_fd-> english); while (node_fd-> english <0.0 | node_fd-> english> 100.0) {printf ("input error, enter the English score again: "); scanf (" % f ", & node _ Fd-> english);} printf ("Enter the student's mathematical score:"); scanf ("% f", & node_fd-> math ); while (node_fd-> math <0.0 | node_fd-> math> 100.0) {printf ("incorrect input. Please enter a new score:"); scanf ("% f ", & node_fd-> math);} printf ("Enter the student's chinese score:"); scanf ("% f", & node_fd-> chinese ); while (node_fd-> chinese <0.0 | node_fd-> chinese> 100.0) {printf ("incorrect input. Please enter a new score:"); scanf ("% f ", & node_fd-> chinese);} node_fd-> all_sub = node_fd-> chinese + node_fd-> math + node_fd-> e Nglish; node_fd-> average = node_fd-> all_sub/3; printf ("modification successful! \ N "); goto recyc3;} else {printf (" this student information is not available! \ N "); goto recyc3 ;}} STU * display_student (STU * stu_head) {int sel; STU * head = stu_head; STU * node = stu_head; if (node = NULL) {printf ("no student information, press any key to return"); getch (); return head;} recyc: printf ("student ID name Chinese Mathematics English average score \ n"); while (node) {printf ("% d % s % 0.1f % 0.1f % 0.1f % 0.1f % 0.1f \ n", node-> ID, node-> name, \ node-> chinese, node-> math, node-> english, node-> average, node-> all_sub); node = node-> next;} printf ("sorting method: 1: sort by student ID 2: sort by Chinese score Order 3: sort by mathematical Score \ n 4: sort by English score 5: sort by total score 0: Return \ n select: "); sel = getch (); while (sel <'0' | sel> '5') {printf ("input error! Please reselect: "); sel = getch ();} if (sel = '0') return head; head = node = select_sort (head, sel ); system ("cls"); goto recyc; return head;} STU * search_by_name (STU * stu_head, char name []) {STU * node = stu_head; if (! Node) return NULL; while (node) {if (! Strcmp (node-> name, name) return node; node = node-> next;} return NULL;} STU * search_by_nb (STU * stu_head, unsigned long nb) {STU * node = stu_head; if (! Node) return NULL; while (node) {if (node-> ID = nb) return node; node = node-> next;} return NULL ;} void search_student (STU * stu_head) {STU * head = stu_head; STU * node; int sel = 0; char name [10]; unsigned long ID; recyc2: printf ("1: search by name 2: search by student ID 0: Return \ n select: "); sel = getch (); while (sel <'0' | sel> '2 ') {printf ("\ n input error, please reselect:"); sel = getch ();} if (sel = '0') return; if (sel = '1' | sel = '2') {if (sel = '1 ') {printf ("\ n enter the Student name you want to search Name: "); scanf (" % s ", name); node = search_by_name (head, name );} else {printf ("\ n enter the student ID you want to search for:"); scanf ("% d", & ID); node = search_by_nb (head, ID );} if (node) {printf ("student ID name Chinese Mathematics English average score \ n "); printf ("% d % s % 0.1f % 0.1f % 0.1f % 0.1f % 0.1f \ n", node-> ID, node-> name, \ node-> chinese, node-> math, node-> english, node-> average, node-> all_sub); goto recyc2;} else {printf ("this student information is not found! \ N "); goto recyc2 ;}}}
5. Store. C. This file contains the Student Score information in the file when the program starts, and the student Score information saved to the file when it needs to be saved or exited.
# Include <stdlib. h> # include <stdio. h> # include "students. H "# define file_name" Students "Void save_student (Stu * stu_head) {Stu * node = stu_head; file * FP; FP = fopen (file_name," W + "); while (node) {fprintf (FP, "% LD % S % 0.1f % 0.1f % 0.1f % 0.1lf % 0.1f \ n", node-> ID, node-> name, \ node-> Chinese, node-> math, node-> English, node-> all_sub, node-> average); node = node-> next;} fclose (FP ); printf ("saved successfully! Press any key to return. \ N "); getch ();} Stu * init_student (Stu * stu_head) {Stu * node = stu_head; Stu * head = stu_head; Stu * new_node; int I; file * FP; char name [10]; int num; ask_stu (new_node); new_node-> next = NULL; FP = fopen (file_name, "R + "); rewind (FP); While (I = fscanf (FP, "% LD % S % F % lf % F \ n", & new_node-> ID, new_node-> name, \ & new_node-> Chinese, & new_node-> math, & new_node-> English, & new_node-> all_sub, & new_node-> average )) = 7) {If (! Head) Head = node = new_node; else {While (node-> next) node = node-> next; node-> next = new_node;} ask_stu (new_node ); new_node-> next = NULL;} fclose (FP); return head ;}
Iv. Summary The Basics of cainiao's C Language Training has come to an end. Through this period, I have gained a deeper understanding of the C language, skills in coding with pointers, arrays, structs, and linked lists. If you have time in the future, you may cultivate the C algorithm. In the next time, I will cultivate C ++. I hope to help cainiao like me in C language. Everyone will work together and work together!