In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces how to use C language to achieve student performance management system, which has a certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article. Let the editor take you to know it.
First, the picture above:
The whole program uses linked lists to manage student scores, and file processing functions are used to save student grades. Fopen (FilePath, "wb") is used to write the data of student information in memory directly to the file, and the corresponding program is opened after reading using fopen (FilePath, "rb") to restore the data directly to memory.
Option 6 is to manually save linked list data and configuration data.
Option 7 configures whether the data in the linked list needs to be saved automatically each time it is modified.
After the entry of option 1, directly follow the prompts to enter all the information of the students, one by one
Option 3 modify the student information, after entering, choose how to find the student to be modified, and then enter the student's student number or name, find it (because there may be students with duplicate names, so here is a serial number index), enter the result index, and then re-enter the student's information according to the prompts to achieve the effect of modification.
Option 4 query, the function is shown in the figure above
Option 5 sort, according to the prompted conditions, sorting will cause the linked list data to change, so when you return to the main menu, the data will be automatically saved to the file.
Option 8 voting system, enter the names of the people who exist in the linked list, corresponding to the voting results below will be changed in real time, and sorted, the person with the highest number of votes is highlighted.
Option 9 after input, the prompt "Export successful" appears on the right, then the student_export.txt file will appear in the corresponding directory, which contains all the student information in the linked list. Here, the fprintf function is used to output to the file.
When sorting the linked list, the data exchange uses the in-memory data copy MoveMemory function in kernel32.dll, and only needs to copy and exchange the data except the pNext in the linked list.
The following is the code:
/ / Student Management system. CPP: Defines the entry point for the console application.//#include "stdafx.h" # include "string.h" # include "conio.h" # include "windows.h" # include "stdlib.h" # define LIST_TITLE "Student ID name gender Chinese Mathematical English\ n" # define LIST_TITLE_1 "gender Chinese Mathematical English average score\ n" # define FILE_DATABASE "C:\\ student_database.dat" # define FILE_EXPORT "C:\\ student_export.txt" / / Color enum {BLACK BLUE, GREEN, CYAN, RED, MAGENTA, BROWN, LIGHTGRAY, DARKGRAY, LIGHTBLUE, LIGHTGREEN, LIGHTCYAN, LIGHTRED, LIGHTMAGENTA, YELLOW, WHITE} / / functional index enum {Func_Add = 1 Magazine / add student information Func_Delete,// delete Func_Modify,// modify Func_Search,// search Func_Sort,// sort Func_Save,// save Func_AutoSave,// automatic save Func_Vote,// voting system Func_Export,// export student information Func_ExitSystem// exit system}; struct Student {int num;// student number char name [20]; / / name char sex [8] / / gender float score [3]; / / average float averge;// score of the three courses, int count;// voting count Student* pNext;}; Student* GroupStuadBack / chain header bool G_autoStore = false / / automatically save / * set font color * / / * * * / void setFontColor (int ForgC) {WORD wColor HANDLE hOutput = GetStdHandle (STD_OUTPUT_HANDLE); CONSOLE_SCREEN_BUFFER_INFO csbi; if (GetConsoleScreenBufferInfo (hOutput, & csbi)) {/ / set font color wColor = (csbi.wAttributes & 0xF0) + (ForgC & 0x0F); SetConsoleTextAttribute (hOutput, wColor) }} / / * cursor jumps to the specified location * / / * * * / void gotoxy (int x Int y) {/ / Update cursor location COORD pos HANDLE hOutput = GetStdHandle (STD_OUTPUT_HANDLE); pos.X = x; pos.Y = y; SetConsoleCursorPosition (hOutput, pos); / / Hidden cursor / / CONSOLE_CURSOR_INFO cursor;// cursor.bVisible = FALSE;// cursor.dwSize = 1; / / cells with values between 1 and 100 are underlined at the bottom ~ fully filled cells / / SetConsoleCursorInfo (hOutput, & cursor) } / * whether printing at the specified location in the main menu is automatically saved * / / * * * * / void printAutoStoreInfo () {setFontColor (LIGHTGREEN) Gotoxy (13,10); printf ("% s\ n", G_autoStore? "Yes": "No"); setFontColor (WHITE) } / * display the top system title * / / * * * / void showSystemTitle () {setFontColor (LIGHTGREEN) Printf ("- -\ n"); printf ("\ t\ t Welcome to the student management system\ n") Printf ("- -\ n"); setFontColor (WHITE) } / * initialize the screen * / / * * * / void initScreen () {system ("CLS") ShowSystemTitle (); printf ("Please enter a numeric serial number and select the action you want to perform:\ n"); printf ("1, add student information\ n"); printf ("2, delete student information\ n"); printf ("3, modify student information\ n"); printf ("4, query student information\ n"); printf ("5, sort\ n") Printf ("6, save (no manual execution is required if auto-save is turned on); printf (" 7, auto-save: "); printAutoStoreInfo (); printf (" 8, voting system\ n "); printf (" 9, export student information\ n "); setFontColor (LIGHTRED); printf (" 10, exit student management system\ n "); setFontColor (WHITE) } / * clear the specified length element * / / * from the specified location * * / void gotodelete (int x Int y, int length) {int I For (I = 0; I)
< length; i++) { gotoxy(x + i, y); printf(" "); }}/************************************************************************//*清除指定位置元素*//************************************************************************/void gotodelete(int x, int y){ gotodelete(x, y, 1);}/************************************************************************//*投票系统 *//************************************************************************/void voteSystem(){ bool hasFound; char name[20]; int count, i, j; Student* pStu, *pTmp; Student** pStuArr; //初始化票数清零 pStu = G_pStuHead->PNext; while (pStu! = NULL) {pStu- > count = 0; pStu = pStu- > pNext;} count = 0; pStuArr = (Student**) malloc (4 * 100); / / used to store the pointer gotoxy (0,6) for students who have obtained votes; printf ("voting results are as follows:\ n"); gotoxy (0,3); printf ("Please enter the name of the person you want to vote for below (enter-1 to return to the main menu):\ n") While (1) {gotodelete (0,4,20); / clear input line gotoxy (0,4); scanf ("% s", name); if (strcmp (name, "- 1") = = 0) {break;} hasFound = false; pStu = GroupStudio-> pNext; / / find the corresponding person name while (pStu! = NULL) {if (pStu- > name, name) = = 0) {hasFound = true; break in the system } pStu = pStu- > pNext;} if (! HasFound) {printf ("check no such person!") ; Sleep (1000); gotodelete (0,5,20); continue;} / / after finding, the number of votes corresponding to this person + 1 pStu- > count++; for (I = 0; I
< count; i++) { if(pStuArr[i] == pStu) { break; } } if(i == count)//说明未找到,则添加进候选人数组 { pStuArr[count++] = pStu; if(count % 100 == 0) { pStuArr = (Student**)realloc(pStuArr, count + 100); } } //冒泡排序,票数 for (i = 0; i < count - 1; i++) { for (j = i + 1; j < count; j++) { if(pStuArr[i]->Count
< pStuArr[j]->Count) {pTmp = pStuArr [I]; pStuArr [I] = pStuArr [j]; pStuArr [j] = pTmp;}} gotoxy (0,7); / / Jump to the line where the votes are printed / / the number of votes printed for (I = 0; I
< count; i++) { if(i == 0) { setFontColor(LIGHTGREEN); } else { setFontColor(WHITE); } printf("%d %s\t%d\n", pStuArr[i]->Num, pStuArr [I]-> name, pStuArr [I]-> count);}} free (pStuArr) } / * Export student information (clear text) * / / * * * * / bool exportStudentInfo () {Student* pStu FILE* fp; pStu = GroompStuhead-> pNext; if ((fp = fopen (FILE_EXPORT, "w")) = NULL) {return false;} while (pStu! = NULL) {fprintf (fp, "d% s% s% .2f% .2f% .2f\ n", pStu- > num, pStu- > name, pStu- > sex, pStu- > score [0], pStu- > score [1], pStu- > score [2], pStu- > averge); pStu = pStu- > pNext } fclose (fp); return true } / * Save student information (as a database) * / / * * * * / bool saveStudentInfo () {FILE * fp Student * pStu; pStu = wb head; if ((fp = fopen (FILE_DATABASE, "wb")) = NULL) {return false;} fwrite (& G_autoStore, sizeof (G_autoStore), 1, fp); while (pStu! = NULL) {fwrite (pStu, sizeof (Student), 1, fp); pStu = pStu- > pNext;} fclose (fp); return true } / * read student information (read documents in the form of database) * / / * * * / bool loadStudentInfo () {FILE * fp Int count; Student stu, * pStu, * pStuNew; count = 0; pStu = GroupStud; if ((fp = fopen (FILE_DATABASE, "rb")) = = NULL) {return false;} fread (& G_autoStore, sizeof (G_autoStore), 1, fp); / / read whether while (1) {fread (& stu, sizeof (Student), 1, fp) is automatically saved / / read the data of each student in the document if (feof (fp)) / / jump out of {break;} if (count++ > 0) / / here > 0 because the chain header will be saved when saving, and the chain header does not have valid data, so rule out {pStuNew = (Student*) malloc (sizeof (Student)); MoveMemory (pStuNew, & stu, sizeof (Student)-4) / / copy all data except pointers of the structure into memory pStuNew- > pNext = NULL; pStu- > pNext = pStuNew; pStu = pStuNew;} if (stu.pNext = = NULL) {break;}} fclose (fp); return true } / * sort student information * / / * * * / bool sortStudentInfo () {int order1 Order2 Bool swapData; char yesOrNo; Student* pStu1, * pStu2, tmpStu; pStu1 = GroompStuhead-> pNext; if (pStu1 = = NULL) {printf ("No student information in the system! \ n "); system (" pause "); return false;} printf (" enter the following serial numbers to perform the corresponding functions (enter other serial numbers to return to the main menu):\ n "); printf (" 1, sort by student number\ n "); printf (" 2, sort by name\ n "); printf (" 3, sort by Chinese score\ n "); printf (" 4, sort by math score\ n ") Printf ("5, sort by English score\ n"); printf ("6, sort by GPA\ n"); scanf ("% d", & order1); if (order1 > = 1 & & order1 = 1 & & order2 pNext! = NULL; pStu1 = pStu1- > pNext) {for (pStu2 = pStu1- > pNext; pStu2! = NULL; pStu2 = pStu2- > pNext) {swapData = false / / do you want to exchange data switch (order1) {case 1 if / sort by student number {if (order2 = = 1? (pStu1- > num > pStu2- > num): (pStu1- > num
< pStu2->Num)) / / Ternary operator to determine whether the positive order or the reverse order {swapData = true;} break;} case 2break; / sort by name {if (order2 = = 1? (strcmp (pStu1- > name, pStu2- > name) > 0): (strcmp (pStu1- > name, pStu2- > name)
< 0)) { swapData = true; } break; } case 3://根据语文排序 case 4://根据数学排序 case 5://根据英语排序 { if(order2 == 1 ? (pStu1->Score [order1-3] > pStu2- > score [order1-3]): (pStu1- > score [order1-3]
< pStu2->Score [order1-3])) {swapData = true;} break;} case 6Rank / sort {if according to average score (order2 = = 1? (pStu1- > averge > pStu2- > averge): (pStu1- > averge
< pStu2->Averge) {swapData = true;} break;}} if (swapData) {/ / Exchange memory data, only need to copy and exchange structural data other than pNext pointer to MoveMemory (& tmpStu, pStu1, sizeof (Student)-4); MoveMemory (pStu1, pStu2, sizeof (Student)-4) MoveMemory (pStu2, & tmpStu, sizeof (Student)-4);} printf ("sort complete, show? Y getchar N\ n "); getchar (); / / filter out the newline character scanf ("% c ", & yesOrNo); if (yesOrNo = ='Y' | | yesOrNo = ='y') {pStu1 = GroupStuad-> pNext; setFontColor (LIGHTGREEN); printf (LIST_TITLE_1); / / display the column header setFontColor (WHITE) / / print the sorted student information while (pStu1! = NULL) {printf ("% d% s% .2f% .2f% .2f% .2f\ n", pStu1- > num, pStu1- > name, pStu1- > sex, pStu1- > score [0], pStu1- > score [1], pStu1- > score [2], pStu1- > averge); pStu1 = pStu1- > pNext;} system ("pause");} return true;} return false } / * query student information * / / * * * / void searchStudentInfo () {bool hasFound Int order, stuID, count, I, min, max; float score; char name [20]; Student* pStu; Student** pStuArr; pStuArr = NULL; while (1) {system ("CLS"); showSystemTitle (); if (pStuArr! = NULL) / / if queried again, it needs to be judged to empty {free (pStuArr);} count = 0; stuID = 0; hasFound = false; pStu = GroupStuad-> pNext PStuArr = (Student**) malloc (4 * 100); / / initialize the array of student information pointers stored after the query ("enter the following serial number to perform the corresponding function (enter other serial numbers to return to the main menu):\ n"); printf ("1, enter student number query information\ n"); printf ("2, enter name query information\ n"); printf ("3, enter language score range query information\ n") Printf ("4, enter math score range query information\ n"); printf ("5, enter English grade range query information\ n"); printf ("6, enter average score range query information\ n"); printf ("7, list all student information\ n"); scanf ("% d", & order) Switch (order) {case 1 num / query {printf ("Please enter the student number to be queried:"); scanf ("% d", & stuID); while (pStu! = NULL) {if (pStu- > num = = stuID) {hasFound = true; break;} pStu = pStu- > pNext;} if (hasFound) / / {setFontColor (LIGHTGREEN) Printf (LIST_TITLE_1); setFontColor (WHITE); printf ("d% s% s% .2f% .2f% .2f\ n", pStu- > num, pStu- > name, pStu- > sex, pStu- > score [0], pStu- > score [1], pStu- > score [2], pStu- > averge);} break } case 2name / query {printf by name ("Please enter the name of the student to be queried:"); scanf ("% s", name); while (pStu! = NULL) {if (strcmp (pStu- > name, name) = = 0) {hasFound = true; pStuArr [count++] = pStu; if (count% 100 = 0) {pStuArr = (Student**) realloc (pStuArr, count+ 100) } pStu = pStu- > pNext;} if (hasFound) {setFontColor (LIGHTGREEN); printf (LIST_TITLE_1); setFontColor (WHITE); for (I = 0; I
< count; i++) { printf("%d %s %s %.2f %.2f %.2f %.2f\n", pStuArr[i]->Num, pStuArr [I]-> name, pStuArr [I]-> sex, pStuArr [I]-> score [0], pStuArr [I]-> score [1], pStuArr [I]-> score [2], pStuArr [I]-> averge);}} break } case 3Granger / query case 4Granger according to the range of Chinese scores / query case 5Granger by range of maths scores / query case 6 by range of English scores / query {char * subjectStrArr [4] = {"Chinese", "maths", "English", "average"}; printf ("Please enter the% s score range to be queried:", subjectStrArr [order-3]) Scanf ("d d", & min, & max); while (pStu! = NULL) {if (order)
< 6)// 3 ~ 5 { score = pStu->Score [order-3];} else / / order = 6 {score = pStu- > averge;} if (score > = min & & score pNext;} if (hasFound) {setFontColor (LIGHTGREEN); printf (LIST_TITLE_1); setFontColor (WHITE); / / print student information for (I = 0; I) in the pointer array
< count; i++) { printf("%d %s %s %.2f %.2f %.2f %.2f\n", pStuArr[i]->Num, pStuArr [I]-> name, pStuArr [I]-> sex, pStuArr [I]-> score [0], pStuArr [I]-> score [1], pStuArr [I]-> score [2], pStuArr [I]-> averge);}} break;} case 7 color / list all student information {hasFound = true; setFontColor (LIGHTGREEN); printf (LIST_TITLE_1); setFontColor (WHITE) While (pStu! = NULL) {printf ("% d% s% .2f% .2f% .2f\ n", pStu- > num, pStu- > name, pStu- > sex, pStu- > score [0], pStu- > score [1], pStu- > score [2], pStu- > averge); pStu = pStu- > pNext;} break;} default: {goto lab_search;} if (! HasFound) {printf ("the corresponding student information could not be found! \ n ");} system (" pause ");} lab_search: free (pStuArr) } / * delete student information * / / * * * / bool deleteStudentInfo () {char yesOrNo Int stuID; bool hasFound; Student* pStu, * pStu1; hasFound = false; pStu = GroompStuhead-> pNext; pStu1 = GroupStud; printf ("Please enter the student ID you want to delete:"); scanf ("% d", & stuID); while (pStu! = NULL) {if (pStu- > num = = stuID) {hasFound = true; break;} pStu1 = pStu; pStu = pStu- > pNext;} if (hasFound) {printf ("find this student's information is as follows:\ n") SetFontColor (LIGHTGREEN); printf (LIST_TITLE_1); setFontColor (WHITE); printf ("d% s% s% .2f% .2f% .2f\ n", pStu- > num, pStu- > name, pStu- > sex, pStu- > score [0], pStu- > score [1], pStu- > score [2], pStu- > averge); printf ("do you want to delete? ); getchar (); / / filter out the newline character scanf ("% c", & yesOrNo); if (yesOrNo = ='y' | | yesOrNo = ='Y') {pStu1- > pNext = pStu- > pNext; free (pStu); printf ("deleted\ n");} else {hasFound = false;}} else {printf ("corresponding student information not found\ n");} system ("pause"); } / * modify student information * / / * * * / bool modifyStudentInfo () {int order Count, i Int stuID; char name [20]; char yesOrNo; bool hasModify; Student* pStu; Student** pStuArr; hasModify = false; count = 0; pStu = GroupStud-> pNext; pStuArr = (Student**) malloc (4 * 100); / / used to store the found student information pointer. The pointer array is defined here to prevent the query name from having a duplicate printf ("Please enter the following serial number and select the corresponding function (1 or 2, otherwise return to the superior menu)\ n"). Printf ("1, enter the student number to find students\ n"); printf ("2, enter the name to find students\ n"); scanf ("% d", & order); if (order = = 1) {printf ("Please enter the student number to be modified:\ n"); scanf ("% d", & stuID); while (pStu! = NULL) {if (pStu- > num = = stuID) {pStuArr [count++] = pStu; break } pStu = pStu- > pNext;}} else if (order = = 2) {printf ("Please enter the student name to modify:\ n"); scanf ("% s", name); while (pStu! = NULL) {if (pStu- > name, name) = 0) {pStuArr [count++] = pStu If (count% 100 = = 0) / / if the array is full, request memory again {pStuArr = (Student**) realloc (pStuArr, count + 100);}} pStu = pStu- > pNext;}} else {return false;} if (count = = 0) {printf ("No information was found, do you want to continue to modify it? ); getchar (); / / filter out the newline character scanf ("% c", & yesOrNo); if (yesOrNo = ='y' | | yesOrNo = ='Y') {system ("CLS"); showSystemTitle (); return modifyStudentInfo ();}} else {printf ("find% d student information for you:\ n", count); setFontColor (LIGHTGREEN); printf (LIST_TITLE); setFontColor (WHITE); for (I = 0; I
< count; i++) { printf("%d、%d %s %s %.2f %.2f %.2f\n", i + 1, pStuArr[i]->Num, pStuArr [I]-> name, pStuArr [I]-> sex, pStuArr [I]-> score [0], pStuArr [I]-> score [1], pStuArr [I]-> score [2]);} printf ("Please enter the information sequence number you want to modify (1% d), other numbers are returned to the main menu\ n", count); scanf ("% d", & order) If (order > = 1 & order num, pStu- > name, pStu- > sex, & pStu- > score [0], & pStu- > score [1], & pStu- > score [2]); pStu- > averge = (pStu- > score [0] + pStu- > score [1] + pStu- > score [2]) / 3; hasModify = true;} free (pStuArr); return hasModify } / * detect the existence of the student ID * / / * * * / bool checkStuIDExist (int stuID) {Student* pStu PStu = GroompStuhead-> pNext; while (pStu! = NULL) {if (pStu- > num = = stuID) {return true;} pStu = pStu- > pNext;} return false } / * add student information * / / * * * / bool addStudentInfo () {printf ("enter-1 enter Return to the superior menu\ n ") SetFontColor (LIGHTGREEN); printf (LIST_TITLE); setFontColor (WHITE); char c; bool hasAdd = false; Student* pStu = GroupStud; Student* pStuNew; while (pStu- > pNext! = NULL) {pStu = pStu- > pNext;} while (1) {pStuNew = (Student*) malloc (sizeof (Student)); scanf ("% d", & pStuNew- > num) If (pStuNew- > num = =-1) / / enter-1 returns the main menu {while ((c = getchar ())! = EOF & & c! ='\ n'); / / keep using getchar () to get the characters in the buffer until the obtained c is "\ n" or the file Terminator EOF (pStuNew); return hasAdd } else if (checkStuIDExist (pStuNew- > num)) {while ((c = getchar ())! = EOF & & c! ='\ n'); / / keep using getchar () to get the characters in the buffer until the obtained c is "\ n" or the file Terminator EOF ("the student number already exists, please re-enter it!" \ n "); free (pStuNew); continue;} hasAdd = true; scanf ("% s% s% f% f ", pStuNew- > name, pStuNew- > sex, & pStuNew- > score [0], & pStuNew- > score [1], & pStuNew- > score [2]); pStuNew- > averge = (pStuNew- > score [0] + pStuNew- > score [1] + pStuNew- > score [2]) / 3; pStuNew- > pNext = NULL; pStu- > pNext = pStuNew; pStu = pStuNew;} return hasAdd } / * execute the corresponding function according to the instruction sequence number * / / * * / bool orderToExecute (int order) {bool succ Succ = false; if (orderlies = Func_Save & & orderlies = Func_AutoSave & & orderlies = Func_Export) {system ("CLS"); showSystemTitle ();} switch (order) {case Func_Add:// add {succ = addStudentInfo (); break;} case Func_Delete:// delete {succ = deleteStudentInfo (); break;} case Func_Modify:// modify {succ = modifyStudentInfo (); break } case Func_Search:// search {searchStudentInfo (); break;} case Func_Sort:// sort {succ = sortStudentInfo (); break;} case Func_Save:// save {succ = saveStudentInfo (); if (succ) {gotoxy (42, Func_Save + 3); setFontColor (LIGHTGREEN); printf ("saved successfully!") ; setFontColor (WHITE); gotodelete (0, Func_ExitSystem + 4,2); gotoxy (0, Func_ExitSystem + 4);} return false;} case Func_AutoSave:// setting auto save {G_autoStore =! GambiautoStore; printAutoStoreInfo (); orderToExecute (Func_Save); / Save configuration break;} case Func_Vote:// voting system {voteSystem (); break;} case Func_Export:// export all student information (plaintext) {succ = exportStudentInfo (); gotoxy (17, Func_Export + 3); setFontColor (LIGHTGREEN); if (succ) {printf ("Export successful!") ;} else {printf ("Export failed!") ;} setFontColor (WHITE); gotodelete (0, Func_ExitSystem + 4,2); gotoxy (0, Func_ExitSystem + 4); return false;} default: {break;}} return succ;} int main (int argc, char* argv []) {int order; bool succ; system ("title student management system by smart egg"); order = 0; succ = false; G_pStuHead = (Student*) malloc (sizeof (Student); GroupStuad-> pNext = NULL; loadStudentInfo () While (1) {if (order! = Func_Save & & order! = Func_AutoSave & & order! = Func_Export) / / there is no need to initialize the screen {initScreen ();} if (succ & & order! = Func_Save & & G_autoStore) / / perform automatic save {orderToExecute (Func_Save);} succ = false; do {scanf ("% d", & order); if (order > = Func_Add & & order)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.