C/C++语言实现 学生管理系统
生活随笔
收集整理的這篇文章主要介紹了
C/C++语言实现 学生管理系统
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
C/C++語言實(shí)現(xiàn) 學(xué)生管理系統(tǒng)
首先,讓我分部分介紹本學(xué)生管理系統(tǒng)
本系統(tǒng)分為兩個(gè)大板塊,分別是學(xué)生版和教師版,學(xué)生版又分出三個(gè)具體功能,教師版分為七個(gè)功能,讓我依次來介紹它們吧!
(一)學(xué)生版
1.學(xué)生版開始菜單
int PrintStudentMenu(void)//學(xué)生版菜單 {printf("\n\n\t━━━━━━━━━━━━━━━━━━━━【歡迎使用學(xué)生管理系統(tǒng)|學(xué)生版】━━━━━━━━━━━━━━━━━━━━━━━\n");printf("\t|選項(xiàng) 功能 *|\n");printf("\t|* *|\n");printf("\t|1 查找學(xué)生信息 *|\n");printf("\t|2 列出所有的學(xué)生信息 *|\n");printf("\t|3 回到標(biāo)題界面 *|\n");printf("\t|* *|\n");printf("\t━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n"); }2.查找學(xué)生信息
STU *SearchNode(STU *head, char keyword[])//通過name或num查找學(xué)生結(jié)構(gòu)體 {STU *pr = head;if (pr == NULL)//鏈表為空 {return NULL;}while (strcmp(pr->name, keyword) && strcmp(pr->num, keyword))//遍歷 {pr = pr->next;if(pr == NULL){return NULL; }}return pr;//返回找到的指向節(jié)點(diǎn)的指針 }3.列出所有的學(xué)生信息
void PrintNode(STU *head)//在屏幕上打印全部學(xué)生信息 {STU *p = head;if (p == NULL){printf("\n\t列表中無數(shù)據(jù)\n\n"); return;}printf("\n\t|姓名\t |學(xué)號(hào)\t\t|總分\n"); while (p != NULL){printf("\t|\t |\t\t\t|\n");printf("\t|%-12s|%-18s|%-12.2f\n", p->name, p->num, p->score);p = p->next;}printf("\n"); }4.回到標(biāo)題界面
(二)教師版
1.教師版開始菜單
int PrintTeacherMenu(void)//教師版菜單 {printf("\n\n\t━━━━━━━━━━━━━━━━━━━━【歡迎使用學(xué)生管理系統(tǒng)|教師版】━━━━━━━━━━━━━━━━━━━━━━ \n");printf("\t|選項(xiàng) 功能 *|\n");printf("\t|* *|\n");printf("\t|1 輸入學(xué)生信息 *|\n");printf("\t|2 刪除學(xué)生信息 *|\n");printf("\t|3 查找學(xué)生信息 *|\n");printf("\t|4 排序?qū)W生信息 *|\n");printf("\t|5 列出所有的學(xué)生信息 *|\n");printf("\t|6 保存并回到標(biāo)題界面 *|\n");printf("\t|7 幫助 *|\n");printf("\t|* *|\n");printf("\t━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n"); }2.輸入學(xué)生信息
void GetInput(STU *p)//輸入學(xué)生信息 {char num[18]; char name[20]; float score; char words[20]; do{printf("\t請(qǐng)輸入學(xué)生姓名:"); gets(name); }while (!JudgementName(name)); strcpy(p->name, name); do{printf("\t請(qǐng)輸入學(xué)生學(xué)號(hào):"); gets(num);}while (isnumber(num)==0);strcpy(p->num, num);do{printf("\t請(qǐng)輸入學(xué)生成績:"); gets(words);}while (!JudgementScore(words)); score = atof(words);//將字符串轉(zhuǎn)化為浮點(diǎn)數(shù) p->score = score; }3.刪除學(xué)生信息
int DeleteSpecNode(STU **head, char str[])//刪除節(jié)點(diǎn) {STU *pr = *head, *p = *head;int flag = 0; if (p == NULL)//鏈表內(nèi)無內(nèi)容 {return 0; }while (p != NULL)//遍歷鏈表 {if(!strcmp(p->num, str))//找到相應(yīng)學(xué)生信息 {flag = 1;break;//停止遍歷,進(jìn)行刪除操作 }pr = p;p = p->next;}if (flag){if(p == *head){*head = p->next;free(p);}else {pr->next = p->next;free(p);}}else//如果沒有找到,則返回1 {return 0; } return 1;}4.查找學(xué)生信息(見學(xué)生版部分)
5.排序?qū)W生信息
6.列出所有的學(xué)生信息(見學(xué)生版部分)
7.保存并回到標(biāo)題界面
8.幫助
char code2[3]; printf("\n\n\t━━━━━━━━━━━━━━━━━━━ 【請(qǐng)問您遇到了哪些問題?】━━━━━━━━━━━━━━━━━━━━━━━━━━━ \n"); printf("\t|選項(xiàng) 功能 *|\n"); printf("\t|* *|\n"); printf("\t|1 無法查詢學(xué)生信息 *|\n"); //printf("\t|2* *|\n"); //printf("\t|3* *|\n"); printf("\t|* *|\n"); printf("\t━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n"); printf("\t如遇到更多問題,歡迎聯(lián)系QQ:1872557359反饋\n\n"); do{printf("\t請(qǐng)輸入幫助選項(xiàng)代碼:"); gets(code2);}while(!JudgementOptions2(code2));if(!strcmp(code2, "1")){printf("\t請(qǐng)確認(rèn):\n\t1.已錄入學(xué)生信息\n\t2.本次未錄入信息但上次打開程序時(shí)已錄入信息\n\t3.若上次打開程序后錄入了信息,但這次仍然無法查詢,則說明上次退出時(shí)未選擇[保存并退出]\n\n");} printf("\n\t按回車鍵繼續(xù)!"); getchar();(三)主函數(shù)及初始菜單
1.初始菜單
int PrintStartMenu(void)//開始菜單 {printf("\n\n\t━━━━━━━━━━━━━━━━━━━━【歡迎使用學(xué)生管理系統(tǒng)|請(qǐng)選擇模式】━━━━━━━━━━━━━━━━━━ \n");printf("\t|選項(xiàng) 功能 *|\n");printf("\t|* *|\n");printf("\t|1 進(jìn)入學(xué)生模式 *|\n");printf("\t|2 進(jìn)入教師模式 *|\n");printf("\t|3 退出管理系統(tǒng) *|\n");printf("\t|* *|\n");printf("\t━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n"); }2.主函數(shù)
(四)最終成果
最后全覽一下本系統(tǒng)的代碼,看看最終的成果:
#include <stdio.h> #include <windows.h> #include <string.h> #include <stdlib.h> #include <ctype.h> typedef struct students//定義學(xué)生結(jié)構(gòu)體 {char name[20];//姓名 char num[18];//學(xué)號(hào) float score;//成績 struct students *next; }STU;int PrintStartMenu(void);//開始菜單 int PrintOverMenu(void);//退出菜單 int PrintStudentMenu(void);//學(xué)生版菜單 int PrintTeacherMenu(void);//教師版菜單 int JudgementOptions(char words[]);//判斷選項(xiàng)是否合法1,2 int JudgementOptions2(char words[]);//判斷選項(xiàng)是否合法1 int JudgementOptionsT(char words[]);//判斷選項(xiàng)是否合法1,2,3,4,5,6,7 int JudgementOptionsS(char words[]);//判斷選項(xiàng)是否合法1,2,3 int isnumber(char a[]);//判斷學(xué)號(hào)是否全為數(shù)字 STU *AppendNode(STU *head);//增加一個(gè)新的節(jié)點(diǎn) void GetInput(STU *p);//輸入學(xué)生信息 void PrintNode(STU *head);//在屏幕上打印全部學(xué)生信息 int WriteToFile(char path[], STU *head);//將鏈表存入文件 STU *ReadFromFile(char path[], STU *head);//從文件中讀取 STU *SearchNode(STU *head, char keyword[]);//通過name或num查找學(xué)生結(jié)構(gòu)體 STU *SortNode(STU *head, int mode);//鏈表排序 int JudgementScore(char words[]);//判斷輸入分?jǐn)?shù)是否合法 void ReleaseList(STU *head);//釋放鏈表 int JudgementName(char name[]);//判斷輸入姓名是否合法 int DeleteSpecNode(STU **head, char str[]); //刪除節(jié)點(diǎn)int main() {char code[2];system("color F2");//設(shè)置控制臺(tái)背景顏色和前景色start:PrintStartMenu(); do{printf("\t請(qǐng)輸入操作選項(xiàng):"); gets(code);}while (!JudgementOptionsS(code));if(!strcmp(code, "1")){system("cls");//清屏char code[2]; STU *head = NULL;//定義頭指針指向空PrintStudentMenu();int KEY = 0;//做標(biāo)記,方便之后讀取時(shí)確定是否錄入過while(1){int miao = 1;//printf("\n\t正在讀取學(xué)生信息......");if(miao == 1)//操作前加載一次學(xué)生信息{ char path[128] = {"students.bin"};STU *temp; if (KEY == 1)//已經(jīng)讀取過了,則直接進(jìn)入選項(xiàng)階段 {goto nextone1;} temp = ReadFromFile(path, head);//讀取鏈表并儲(chǔ)存在temp中if (temp == NULL)//如果讀取到的為空,則直接進(jìn)入選項(xiàng)階段 {goto nextone1;} head = temp;//將temp置為頭節(jié)點(diǎn),方便之后使用 KEY = 1; //讀取完成,將標(biāo)記設(shè)為1;防止重復(fù)讀取 }nextone1:do{system("cls");PrintStudentMenu();printf("\t請(qǐng)輸入操作代碼:"); gets(code);}while(!JudgementOptionsS(code));if (!strcmp(code, "1")){char keyword[28];STU *p;//聲明結(jié)構(gòu)體指針p printf("\t請(qǐng)輸入要查找的姓名或?qū)W號(hào):");gets(keyword); p = SearchNode(head, keyword);//查找學(xué)生信息,并賦值給p if (p == NULL)//返回值為空,鏈表中沒有找到相應(yīng)的學(xué)生信息結(jié)構(gòu)體{printf("\n\t未找到該學(xué)生信息!\n\n");}else//返回值不為空,則輸出該結(jié)構(gòu)體內(nèi)學(xué)生信息 {printf("\n\t%-12s學(xué)號(hào):%-18s總分:%-12.2f\n\n", p->name, p->num, p->score);} printf("\n\t按回車鍵繼續(xù)!");getchar();}if (!strcmp(code, "2")){PrintNode(head);//在屏幕上打印全部學(xué)生信息printf("\n\t按回車鍵繼續(xù)!");getchar();}if (!strcmp(code, "3")){ReleaseList(head);system("cls");//清空屏幕goto start;}}}if(!strcmp(code, "2")){system("cls");//清屏char code[6]; STU *head = NULL;//定義頭指針 int KEY = 0; //做標(biāo)記,方便之后讀取時(shí)確定是否錄入過 PrintTeacherMenu();while(1){int miao = 1;//printf("\n\t正在讀取學(xué)生信息......");if(miao == 1)//操作前加載一次學(xué)生信息{ char path[128] = {"students.bin"};//文件名(包括文件路徑)STU *temp; if (KEY == 1)//已經(jīng)讀取過了,則直接進(jìn)入選項(xiàng)階段{goto nextone2;} temp = ReadFromFile(path, head);//讀取鏈表并儲(chǔ)存在temp中if (temp == NULL)//如果讀取到的為空,則直接進(jìn)入選項(xiàng)階段{goto nextone2;} head = temp;//將temp置為頭節(jié)點(diǎn),方便之后使用KEY = 1;//讀取完成,將標(biāo)記設(shè)為1;防止重復(fù)讀取 }nextone2:do{system("cls");PrintTeacherMenu();printf("\t請(qǐng)輸入操作代碼:"); gets(code);}while(!JudgementOptionsT(code));if(!strcmp(code, "1")) {char choice[2];do{head = AppendNode(head);printf("\n\t是否繼續(xù)加入學(xué)生信息?(是則輸入大寫字母Y,否則輸入大寫字母N):");gets(choice);}while (!strcmp(choice, "Y"));printf("\n\t按回車鍵繼續(xù)!");getchar();}if(!strcmp(code, "2")){char str[20];int result;//保存執(zhí)行后的結(jié)果 printf("\n\t請(qǐng)輸入學(xué)生的學(xué)號(hào):");gets(str);result = DeleteSpecNode(&head, str);if (result == 0){printf("\n\t未找到該學(xué)號(hào)!\n\n");}else{printf("\n\t刪除成功!\n\n");}printf("\n\t按回車鍵繼續(xù)!");getchar();}if(!strcmp(code, "3")){char keyword[28];STU *p;printf("\t請(qǐng)輸入要查找的姓名或?qū)W號(hào):");gets(keyword); p = SearchNode(head, keyword);if (p == NULL){printf("\n\t未找到該學(xué)生信息!\n\n");}else{printf("\n\t%-12s學(xué)號(hào):%-18s總分:%-12.2f\n\n", p->name, p->num, p->score);} printf("\n\t按回車鍵繼續(xù)!");getchar();}if(!strcmp(code, "4")){char code2[3];printf("\t━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");printf("\t|* *|\n");printf("\t|* 1.按學(xué)號(hào)排序?qū)W生信息 *|\n");printf("\t|* 2.按姓名排序?qū)W生信息 *|\n");printf("\t|* 3.按總分排序?qū)W生信息 *|\n");printf("\t|* *|\n");printf("\t━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");do {printf("\n\t 輸入您的選擇:");gets(code2); }while(!JudgementOptionsS(code2));if(!strcmp(code2, "1")){head = SortNode(head, 2);printf("\n\t學(xué)號(hào)排序成功\n\n");}if(!strcmp(code2, "2")){ head = SortNode(head, 3);printf("\n\t字典排序成功\n\n");}if(!strcmp(code2, "3")){head = SortNode(head, 1);printf("\n\t總分排序成功\n\n");}printf("\n\t按回車鍵繼續(xù)!");getchar();}if(!strcmp(code, "5")){PrintNode(head);printf("\n\t按回車鍵繼續(xù)!");getchar();}if(!strcmp(code, "6")){int flag;char path[128] = {"students.bin"};flag = WriteToFile(path, head);if (flag){printf("\n\t保存成功\n\n");KEY = 1;//之后讀取時(shí)方便確定是否錄入過 }else{printf("\t文件載入入失敗或無內(nèi)容可載入!\n");}ReleaseList(head);system("cls");goto start;}if(!strcmp(code, "7")){char code2[3];printf("\n\n\t━━━━━━━━━━━━━━━━━━━ 【請(qǐng)問您遇到了哪些問題?】━━━━━━━━━━━━━━━━━━━━━━━━━━━ \n");printf("\t|選項(xiàng) 功能 *|\n");printf("\t|* *|\n");printf("\t|1 無法查詢學(xué)生信息 *|\n");//printf("\t|2* *|\n");//printf("\t|3* *|\n");printf("\t|* *|\n");printf("\t━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");printf("\t如遇到更多問題,歡迎聯(lián)系QQ:1872557359反饋\n\n"); do{printf("\t請(qǐng)輸入幫助選項(xiàng)代碼:"); gets(code2);}while(!JudgementOptions2(code2));if(!strcmp(code2, "1")){printf("\t請(qǐng)確認(rèn):\n\t1.已錄入學(xué)生信息\n\t2.本次未錄入信息但上次打開程序時(shí)已錄入信息\n\t3.若上次打開程序后錄入了信息,但這次仍然無法查詢,則說明上次退出時(shí)未選擇[保存并退出]\n\n");}printf("\n\t按回車鍵繼續(xù)!");getchar();}}}if(!strcmp(code, "3")){system("cls");PrintOverMenu();exit(0);} }int PrintStartMenu(void)//開始菜單 {printf("\n\n\t━━━━━━━━━━━━━━━━━━━━【歡迎使用學(xué)生管理系統(tǒng)|請(qǐng)選擇模式】━━━━━━━━━━━━━━━━━━ \n");printf("\t|選項(xiàng) 功能 *|\n");printf("\t|* *|\n");printf("\t|1 進(jìn)入學(xué)生模式 *|\n");printf("\t|2 進(jìn)入教師模式 *|\n");printf("\t|3 退出管理系統(tǒng) *|\n");printf("\t|* *|\n");printf("\t━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n"); }int PrintOverMenu(void)//退出菜單 {printf("\n\n\t━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");printf("\t|* *|\n");printf("\t|* #制作by王思淼# *|\n");printf("\t|* #感謝您的使用# *|\n");printf("\t|* *|\n");printf("\t━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");printf("\t使用回車鍵退出本程序");getchar(); }int PrintStudentMenu(void)//學(xué)生版菜單 {printf("\n\n\t━━━━━━━━━━━━━━━━━━━━【歡迎使用學(xué)生管理系統(tǒng)|學(xué)生版】━━━━━━━━━━━━━━━━━━━━━━━\n");printf("\t|選項(xiàng) 功能 *|\n");printf("\t|* *|\n");printf("\t|1 查找學(xué)生信息 *|\n");printf("\t|2 列出所有的學(xué)生信息 *|\n");printf("\t|3 回到標(biāo)題界面 *|\n");printf("\t|* *|\n");printf("\t━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n"); }int PrintTeacherMenu(void)//教師版菜單 {printf("\n\n\t━━━━━━━━━━━━━━━━━━━━【歡迎使用學(xué)生管理系統(tǒng)|教師版】━━━━━━━━━━━━━━━━━━━━━━ \n");printf("\t|選項(xiàng) 功能 *|\n");printf("\t|* *|\n");printf("\t|1 輸入學(xué)生信息 *|\n");printf("\t|2 刪除學(xué)生信息 *|\n");printf("\t|3 查找學(xué)生信息 *|\n");printf("\t|4 排序?qū)W生信息 *|\n");printf("\t|5 列出所有的學(xué)生信息 *|\n");printf("\t|6 保存并回到標(biāo)題界面 *|\n");printf("\t|7 幫助 *|\n");printf("\t|* *|\n");printf("\t━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n"); }int JudgementOptions(char words[])//判斷選項(xiàng)是否合法1,2 {int i; int temp;//用于存入字符串轉(zhuǎn)換成的整數(shù) for (i = 0; words[i] != '\0'; i++)//遍歷 {if(!isdigit(words[i]))//檢查其是否為十進(jìn)制字符 {return 0;}}temp = atoi(words);if(temp >= 1 && temp <= 2){return 1;}else{return 0;} }int JudgementOptions2(char words[])//判斷選項(xiàng)是否合法1 {int i;int temp;//用于存入字符串轉(zhuǎn)換成的整數(shù) for (i = 0; words[i] != '\0'; i++)//遍歷 {if(!isdigit(words[i]))//檢查其是否為十進(jìn)制字符 {return 0;}}temp = atoi(words);if(temp >= 1 && temp <= 1){return 1;}else{return 0;} }int JudgementOptionsT(char words[])//判斷選項(xiàng)是否合法1,2,3,4,5,6,7 {int i; int temp;//用于存入字符串轉(zhuǎn)換成的整數(shù) for (i = 0; words[i] != '\0'; i++)//遍歷 {if(!isdigit(words[i]))//檢查其是否為十進(jìn)制字符 {return 0;}}temp = atoi(words); if(temp >= 1 && temp <= 7){return 1;}else{return 0;} }int JudgementOptionsS(char words[])//判斷選項(xiàng)是否合法1,2,3 {int i; int temp;//用于存入字符串轉(zhuǎn)換成的整數(shù) for (i = 0; words[i] != '\0'; i++)//遍歷 {if(!isdigit(words[i]))//檢查其是否為十進(jìn)制字符 {return 0;}}temp = atoi(words); if(temp >= 1 && temp <= 3){return 1;}else{return 0;} }int isnumber(char a[])//判斷學(xué)號(hào)是否全為數(shù)字 {int i;int len = strlen(a);int j =0;for(int i =0;i<len;i++) //遍歷整個(gè)字符串{if(a[i]<=57&&a[i]>=48) //0~9的ASCII碼是48~57{j++;} //找到數(shù)字了就數(shù)量++}//數(shù)字總數(shù)和字符串長度一樣,則全是數(shù)字,總數(shù)為0,則都不是數(shù)字,在0~len之間則有部分是數(shù)字if (j==len){ i = 1; }else {i = 0;}return i; }void GetInput(STU *p)//輸入學(xué)生信息 {char num[18]; char name[20]; float score; char words[20]; do{printf("\t請(qǐng)輸入學(xué)生姓名:"); gets(name); }while (!JudgementName(name)); strcpy(p->name, name); do{printf("\t請(qǐng)輸入學(xué)生學(xué)號(hào):"); gets(num);}while (isnumber(num)==0);strcpy(p->num, num);do{printf("\t請(qǐng)輸入學(xué)生成績:"); gets(words);}while (!JudgementScore(words)); score = atof(words);//將字符串轉(zhuǎn)化為浮點(diǎn)數(shù) p->score = score; }STU *AppendNode(STU *head)//增加一個(gè)新的節(jié)點(diǎn) {STU *p = NULL, *pr = head;//定義末節(jié)點(diǎn)和新節(jié)點(diǎn) pr = head; p = (STU *)malloc(sizeof(STU));//聲明一個(gè)空間來存放 if (p == NULL){exit(0); }GetInput(p);//為新結(jié)構(gòu)體指針賦值 p->next = NULL;//新指針尾指向空 if (head == NULL){head = p; }else{while (pr->next != NULL)//讓pr指向最后一個(gè)成員 {pr = pr->next; } pr->next = p;//讓末節(jié)點(diǎn)指向新節(jié)點(diǎn) } return head; }void PrintNode(STU *head)//在屏幕上打印全部學(xué)生信息 {STU *p = head;if (p == NULL){printf("\n\t列表中無數(shù)據(jù)\n\n"); return;}printf("\n\t|姓名\t |學(xué)號(hào)\t\t|總分\n"); while (p != NULL){printf("\t|\t |\t\t\t|\n");printf("\t|%-12s|%-18s|%-12.2f\n", p->name, p->num, p->score);p = p->next;}printf("\n"); }int WriteToFile(char path[], STU *head)//將鏈表存入文件 {FILE *fp;STU *p = head;fp = fopen(path, "wb");if (fp == NULL){printf("\n文件打開失敗!\n\n");return 0;}if (head == NULL){ printf("當(dāng)前沒有學(xué)生信息可寫入!\n");return 0;}while (p != NULL){fwrite(p, sizeof(STU), 1, fp); p = p->next;}fclose(fp);return 1; }STU *ReadFromFile(char path[], STU *head)//從文件中讀取 {FILE *fp;STU *pr = head, *p = NULL;int flag; //判斷是否讀到文件末尾或讀取失敗 fp = fopen(path, "rb");if (fp == NULL){return NULL; //此處不直接退出是因?yàn)?#xff0c;讀取文件失敗還不足以讓整個(gè)進(jìn)程無法進(jìn)行 }if (pr == NULL) //若鏈表為空則加入第一個(gè)成員后,進(jìn)入非空的處理流程 {p = (STU *)malloc(sizeof(STU));flag = fread(p, sizeof(STU), 1, fp);if (!flag) //判斷文件中是否有內(nèi)容 {free(p);return NULL;}head = p;p->next = NULL;}pr = head;while (pr->next != NULL) //鏈表非空,遍歷到鏈表末尾,再加入新的成員 {pr = pr->next;}do{p = (STU *)malloc(sizeof(STU)); flag = fread(p, sizeof(STU), 1, fp);if (flag) //如果讀取成功,則插入鏈表末尾 {pr->next = p;p->next = NULL; pr = p;}}while (flag);free(p); //讀到末尾時(shí),多申請(qǐng)的內(nèi)存實(shí)際沒有存放數(shù)據(jù),及時(shí)釋放 fclose(fp); //關(guān)閉文件 return head; }STU *SortNode(STU *head, int mode)//給結(jié)構(gòu)體鏈表排序 {STU *pr,*pt, temp;pr = head;pt = NULL; bool condition;//判斷條件 if(pr == NULL || pr->next == NULL){return head;}while(pr != pt){while(pr->next != pt)//每輪會(huì)把最大或最小的數(shù)排到最后 {switch (mode){case 1:// "1"表示按降序降序condition = pr->score < pr->next->score;break; case 2:// "2"表示按學(xué)號(hào)排序 condition = (strcmp(pr->num, pr->next->num) > 0);break;case 3:// "3"表示按姓名排序 condition = (strcmp(pr->name, pr->next->name) > 0);break;default://如果mode不正確,則直接返回原鏈表,不進(jìn)行排序 return head;} if(condition) {temp = *(pr->next);strcpy(pr->next->name, pr->name);strcpy(pr->next->num, pr->num);pr->next->score = pr->score;strcpy(pr->name, temp.name);strcpy(pr->num, temp.num);pr->score = temp.score;}pr = pr->next;}pt = pr;pr = head;}return head; }STU *SearchNode(STU *head, char keyword[])//通過name或num查找學(xué)生結(jié)構(gòu)體 {STU *pr = head;if (pr == NULL)//鏈表為空 {return NULL;}while (strcmp(pr->name, keyword) && strcmp(pr->num, keyword))//遍歷 {pr = pr->next;if(pr == NULL){return NULL; }}return pr;//返回找到的指向節(jié)點(diǎn)的指針 }int JudgementScore(char words[])//判斷輸入分?jǐn)?shù)是否合法 {int i = 0;int flag = 1; double temp; if (words[i] == '\0')//若輸入為空,則錯(cuò)誤 {return 0;}for (i = 0; words[i] != '\0'; i++)//遍歷 {if (i == 0)//判斷第一個(gè)數(shù) {if(!isdigit(words[i]))// 第一個(gè)符號(hào)必須是數(shù)字 {return 0;}}else//判斷之后的數(shù) {if (!isdigit(words[i])){if (words[i] == '.')//判斷有沒有小數(shù)點(diǎn){if (flag)//至多一個(gè)小數(shù)點(diǎn) { flag = 0; continue;}else {return 0;} } return 0;//如果是其他字符也錯(cuò)誤 }}}temp = atof(words);//把字符串轉(zhuǎn)換成浮點(diǎn)數(shù) if(temp >= 0 && temp <= 100){return 1;}else{return 0;} }void ReleaseList(STU *head)//釋放鏈表 {STU *pr = head, *p = NULL;while (pr != NULL){p = pr;free(pr);pr = p->next;} }int DeleteSpecNode(STU **head, char str[])//刪除節(jié)點(diǎn) {STU *pr = *head, *p = *head;int flag = 0; if (p == NULL)//鏈表內(nèi)無內(nèi)容 {return 0; }while (p != NULL)//遍歷鏈表 {if(!strcmp(p->num, str))//找到相應(yīng)學(xué)生信息 {flag = 1;break;//停止遍歷,進(jìn)行刪除操作 }pr = p;p = p->next;}if (flag){if(p == *head){*head = p->next;free(p);}else {pr->next = p->next;free(p);}}else//如果沒有找到,則返回1 {return 0; } return 1;}int JudgementName(char name[])//判斷輸入姓名是否合法 {if (name[0] == '\0')//用戶沒有輸入 {return 0;}else{return 1;} }總結(jié)
以上是生活随笔為你收集整理的C/C++语言实现 学生管理系统的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mondrian mysql驱动,导入M
- 下一篇: MVC区域areas添加出现错误,生成空