C語言實現新生入學登記系統
本文實例為大傢分享瞭C語言實現新生入學登記系統的具體代碼,供大傢參考,具體內容如下
項目所用數據結構:鏈表
算法:對鏈表數據的增刪改查操作,冒泡排序
系統架構圖:
項目文件結構:
(1)system.h
#ifndef SYSTEM_H_INCLUDED #define SYSTEM_H_INCLUDED //宏定義學生信息的一種表示形式 #define STUDENT_DATA pMove->studentData.studentId,pMove->studentData.name,pMove->studentData.sex,pMove->studentData.age,pMove->studentData.className,pMove->studentData.major,pMove->studentData.tel,pMove->studentData.score #define STUDENT_RANKING stuRanking[j].studentId, stuRanking[j].name, stuRanking[j].className, stuRanking[j].score,stuRanking[j].ranking struct student { char studentId[15]; //學號 char name[10]; char sex[4]; int age; char className[20]; //班級 char major[20]; //專業 char tel[15]; int score; //入學成績 }; struct Node { struct student studentData; struct Node* next; }; struct studentRanking { char studentId[15]; char name[10]; char className[20]; int score; int ranking; }; extern struct Node* studentList; //鏈表的頭指針 #endif // SYSTEM_H_INCLUDED
(2)main.h
#ifndef MAIN_H_INCLUDED #define MAIN_H_INCLUDED #include "AddStudent.h" #include "BeginingAndEnding.h" #include "DeleteStudent.h" #include "ModifyStudent.h" #include "SearchStudent.h" #include "ShowStudent.h" #include "ShowStudentRanking.h" #include "MyList.h" #include "system.h" void showMenu(); #endif // MAIN_H_INCLUDED
(3)main.c
#include <stdio.h> #include <stdlib.h> #include <conio.h> //getc函數使用的頭文件 #include "main.h" //主函數 int main() { int selection; Int ret; Begining(); showMenu(); //展示界面 ret = scanf("%d", &selection); //讀入用戶輸入數字 getchar(); While(ret != 1) { printf(“輸入錯誤,請選擇(0-6):”); ret = scanf("%d", &selection); //讀入用戶輸入數字 } while (selection) { switch (selection) { case 1: AddStudent(); //錄入學生信息 break; case 2: ShowStudent(); //瀏覽學生信息 break; case 3: SearchStudent(); //查找學生信息 break; case 4: DeleteStudent(); //刪除學生信息 break; case 5: ModifyStudent();//修改學生信息 break; case 6: ShowRanking(); //顯示學生排名 break; default: printf("\t\t請輸入正確的數字!\n"); } Ending(); //將鏈表數據寫入文件 printf("|按任意鍵返回系統菜單|"); getch(); //接收用戶輸入的任意字符 system("cls"); showMenu(); ret = scanf("%d", &selection); //提示用戶輸入數字 getchar(); While(ret != 1) { printf(“輸入錯誤,請選擇(0-6):”); ret = scanf("%d", &selection); //讀入用戶輸入數字 } } return 0; } void showMenu() { printf("\n\n\n\n\n"); printf("\t|--------------- 歡迎進入 ----------------|\n"); printf("\t| 新生入學登記系統 |\n"); printf("\t| 主菜單 |\n"); printf("\t| 1. 錄入學生信息 |\n"); printf("\t| 2. 瀏覽學生信息 |\n"); printf("\t| 3. 查找學生信息 |\n"); printf("\t| 4. 刪除學生信息 |\n"); printf("\t| 5. 修改學生信息 |\n"); printf("\t| 6. 新生入學排名 |\n"); printf("\t| 0. 退出系統 |\n"); printf("\t|-----------------------------------------|\n"); printf("\n"); printf("\t\t請選擇(0-6):"); }
(4)BeginingAndEnding.h
#ifndef BEGININGANDENDING_H_INCLUDED #define BEGININGANDENDING_H_INCLUDED //關於啟動函數和結束函數 void Begining(); void Ending(); #endif // BEGININGANDENDING_H_INCLUDED
(5)BeginingAndEnding.c
#include <stdio.h> #include <conio.h> //getc函數使用的頭文件 #include "system.h" #include "AboutFiles.h" #include "BeginingAndEnding.h" #include "MyList.h" void Begining() { readInfoFromFile("studentList.txt"); } void Ending() { writeInfoToFile("studentList.txt"); }
(6)MyList.h
#ifndef MYLIST_H_INCLUDED #define MYLIST_H_INCLUDED //關於鏈表的函數聲明 //創建節點 struct Node* createNode(struct student data); //插入節點 void insertNodeByHead(struct student data); //指定位置刪除節點 void deleteAppointNode(char* studentId); //查找功能 struct Node* searchInfoByData(char* studentId); //打印鏈表 void printList(); #endif // MYLIST_H_INCLUDED
(7)MyList.c
#include <stdio.h> #include <conio.h> //getc函數使用的頭文件 #include <windows.h> //Sleep函數使用的頭文件 #include <string.h>//strcmp函數使用的頭文 #include "system.h" #include "MyList.h" struct Node* studentList = NULL; //鏈表的頭指針 //創建節點 struct Node* createNode(struct student studentData) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); if(newNode != NULL) { newNode->studentData = studentData; newNode->next = NULL; } return newNode; } //插入節點 void insertNodeByHead(struct student data) { struct Node* newNode = createNode(data); //表頭法插入,每次插入都將數據插入到頭節點的下一個,先判斷頭節點是否為空,為空則新節點就是頭節點,不為空,則插入在頭節點的下一個位置 if(studentList == NULL) { studentList = newNode; } else//不改變頭節點 { newNode->next = studentList->next; studentList->next = newNode; } } //指定位置刪除節點(知道這個節點的前驅和後續,然後進行刪除) void deleteAppointNode( char* studentId) { //將鏈表頭部設為指定位置,先判斷頭節點是否為空,為空則鏈表沒有數據,無法刪除 //如果頭節點不為空,則設頭結點為指定位置,如果頭結點是所找的節點,則刪除,如果不是,設頭結點的下一個為指定節點,頭結點為指定節點的前驅節點,一直向下查詢 //指定位置 struct Node* posNode = studentList; //指定位置的前面 struct Node* posFrontNode = NULL; //查找指定節點 if(posNode == NULL) { printf("數據為空無法刪除!\n"); return; } else { //姓名是字符串,不能直接比較, strcmp(), 相等返回值為0,相同返回值為負數或者正數 if(strcmp(posNode->studentData.studentId, studentId) ==0) { studentList = studentList->next; free(posNode); return; } else { posFrontNode = posNode; posNode = posNode->next; //posFrontNode = posNode; //!! while(strcmp(posNode->studentData.studentId, studentId)) { //繼續向下一個節點移動 posFrontNode = posNode; posNode = posFrontNode->next; if(posNode == NULL) //找到瞭鏈表尾部,沒有找到數據 { printf("未找到指定位置,無法刪除!"); return; } } //查到到對應數據後,進行刪除,將該節點架空,然後釋放該節點的存儲單元 posFrontNode->next = posNode->next; free(posNode); return; } } } //查找功能 struct Node* searchInfoByData(char* studentId) { struct Node* pMove; pMove = studentList; if(pMove == NULL) //頭節點為空,鏈表為空 { //printf("學生鏈表為空!\n"); return pMove; } else { // 查找到或者查找到鏈表最後,則結束循環,返回查詢結果,結果為空,有兩種可能,一種是鏈表中沒有數據,另一種是沒有查詢到 while(pMove != NULL) { if(strcmp(pMove->studentData.studentId, studentId)==0)//找見 { //printf("已查找到!\n"); return pMove; } else { pMove = pMove->next; } } //printf("未找到!\n"); return pMove; } } //打印鏈表 void printList() { struct Node* pMove = studentList; //涉及到展示數據 //表頭 if(pMove == NULL) { printf("No student record! Please add.\n"); return; } else { printf("\t-------------------------------------------------------------------------------------\n"); printf("\t|%-10s |%-7s |%-4s |%-4s |%-12s |%-12s |%-12s |%-5s|\n","學號","姓名","性別","年齡","班級","專業","電話","入學成績"); printf("\t-------------------------------------------------------------------------------------\n"); while(pMove) { printf("\t|%-10s |%-7s |%-4s |%-4d |%-12s |%-12s |%-12s |%-8d|\n", STUDENT_DATA); printf("\t-------------------------------------------------------------------------------------\n"); pMove = pMove->next; } } printf("\n"); }
(8)AddStudent.h
#ifndef ADDSTUDENT_H_INCLUDED #define ADDSTUDENT_H_INCLUDED void AddStudent(); //錄入學生信息 #endif // ADDSTUDENT_H_INCLUDED
(9)AddStudent.c
#include <stdio.h> #include <conio.h> //getc函數使用的頭文件 #include <windows.h> //Sleep函數使用的頭文件 #include <string.h>//strcmp函數使用的頭文 #include "AddStudent.h" #include "system.h" #include "MyList.h" //錄入學生信息 void AddStudent() { struct student studentData; struct Node* isNull = NULL; //接收查詢的返回值 int iFlagExist; //保證不重復輸入 char cFlag; int ret; //用來接收scanf的返回值,判斷輸入數據是否正確 system("cls"); printf("===================================【錄入學生信息】===============================\n"); printf("\n請選擇是否輸入學生信息(y/n):"); cFlag = getchar(); getchar(); while(cFlag != 'n' && cFlag!='y') { printf("輸入有誤,請輸入‘y'或者‘n'!"); printf("\n請選擇是否輸入學生信息(y/n):"); cFlag = getchar(); getchar(); } if (cFlag == 'n') return; //循環輸入學生信息可輸入一條也可多條輸入 while (cFlag == 'y') { printf("請輸入學生學號:"); do { iFlagExist = 0; gets(studentData.studentId); //對學生編號在鏈表中進行查詢,對查詢結果進行判斷,如果存在則重新輸入,不存在則繼續 isNull = searchInfoByData(studentData.studentId); if(isNull!= NULL) //可以查詢到 { printf("該學生已經存在請重新輸入!\n"); printf("請輸入學生學號:"); iFlagExist = 1; } } while (iFlagExist == 1); //添加學生信息 printf("請輸入學生姓名:"); ret = scanf("%s",studentData.name); while(ret!=1) { printf("輸入學生姓名有誤,請重新輸入!\n"); printf("請輸入學生姓名:"); ret = scanf("%s",studentData.name); } getchar(); printf("請輸入學生性別(男-M,女-F):"); //這裡采用防禦式編程,如果不是M,F或者沒有輸入該項則重新輸入 while (gets(studentData.sex) != NULL) { if (strcmp(studentData.sex, "F")==0 || strcmp(studentData.sex, "M")==0) break; printf("錯誤,隻能輸入'F'或者'M',請重新輸入\n"); printf("請輸入學生性別(男-M,女-F):"); } printf("請輸入學生年齡(15-25):"); ret = scanf("%d", &studentData.age); while((ret != 1) || studentData.age<15 || studentData.age>25) { printf("輸入年齡錯誤,請重新輸入學生年齡(15-25):"); ret = scanf("%d", &studentData.age); } getchar(); printf("請輸入學生班級(eg: B電子191):"); gets(studentData.className); printf("請輸入學生專業:"); gets(studentData.major); printf("請輸入學生電話:"); gets(studentData.tel); printf("請輸入學生入學成績(200-750):"); ret = scanf("%d", &studentData.score); while((ret != 1) || studentData.score<200 || studentData.score>750) { printf("輸入成績信息錯誤,請重新輸入學生入學成績(200-750):"); ret = scanf("%d", &studentData.score); } getchar(); insertNodeByHead(studentData); fflush(stdin); printf("繼續輸入信息嗎(y/n):"); cFlag = getchar(); getchar(); while(cFlag != 'n' && cFlag!='y') { printf("輸入有誤,請輸入‘y'或者‘n'!"); printf("\n請選擇是否輸入學生信息(y/n):"); cFlag = getchar(); getchar(); } } printf("添加學生信息執行完畢!\n"); }
(10)ShowStudent.h
#ifndef SHOWSTUDENT_H_INCLUDED #define SHOWSTUDENT_H_INCLUDED void ShowStudent(); //查找學生信息 #endif // SHOWSTUDENT_H_INCLUDED
(11)ShowStudent.c
#include <stdio.h> #include <conio.h> //getc函數使用的頭文件 #include <windows.h> //Sleep函數使用的頭文件 #include <string.h>//strcmp函數使用的頭文 #include "ShowStudent.h" #include "system.h" #include "MyList.h" //瀏覽學生信息 void ShowStudent() { system("cls"); printf("\n"); printf("\t====================================【瀏覽學生信息】================================\n"); printf("\n\n"); printList(); }
(12)SearchStudent.h
#ifndef SEARCHSTUDENT_H_INCLUDED #define SEARCHSTUDENT_H_INCLUDED void SearchStudent(); //查找學生信息 #endif // SEARCHSTUDENT_H_INCLUDED
(13)SearchStudent.c
#include <stdio.h> #include <conio.h> //getc函數使用的頭文件 #include <windows.h> //Sleep函數使用的頭文件 #include <string.h>//strcmp函數使用的頭文 #include "SearchStudent.h" #include "system.h" #include "MyList.h" //查找學生信息 void SearchStudent() { //查詢成功,則返回該學生信息,查詢失敗則輸出提示信息,可重新輸入,也可退出 struct student studentData; struct Node* pMove = NULL; //用來接收查詢返回的結果 char cFlag; //接收用戶的選擇 system("cls"); printf("\n"); printf("\t==================================【查找學生信息】==============================\n"); printf("\t是否進行學生查詢(y/n):"); cFlag = getchar(); getchar(); //接收回車鍵 while(cFlag != 'n' && cFlag!='y') { printf("輸入有誤,請輸入‘y'或者‘n'!"); printf("\n請選擇是否查詢學生信息(y/n):"); cFlag = getchar(); getchar(); } if (cFlag == 'n') return; while(cFlag == 'y') { printf("\t請輸入需要查找的學生的學號:"); //這裡通過學號進行查詢,學號是唯一的,姓名有重名現象 gets(studentData.studentId); pMove = searchInfoByData(studentData.studentId); if(pMove) //pMove 為真時,表示查詢到 { printf("\t查詢成功,以下為該學生信息:\n"); printf("\t-------------------------------------------------------------------------------------\n"); printf("\t|%-10s |%-7s |%-4s |%-4s |%-12s |%-12s |%-12s |%-5s|\n","學號","姓名","性別","年齡","班級","專業","電話","入學成績"); printf("\t-------------------------------------------------------------------------------------\n"); printf("\t|%-10s |%-7s |%-4s |%-4d |%-12s |%-12s |%-12s |%-8d|\n", STUDENT_DATA); printf("\t-------------------------------------------------------------------------------------\n"); } else //pMove 為空時,未查詢到,這裡為空有兩種情況,一種為學生名單為空,一種是沒有查詢到 { printf("\t查詢失敗,該學生不存在或學生列表為空!\n"); printf("\t是否重新查詢(y/n):"); cFlag = getchar(); getchar(); while(cFlag != 'n' && cFlag!='y') { printf("輸入有誤,請輸入‘y'或者‘n'!"); printf("\n是否重新查詢學生信息(y/n):"); cFlag = getchar(); getchar(); } } } printf("\t學生信息查詢結束!\n"); }
(14)DeleteStudent.h
#ifndef DELETESTUDENT_H_INCLUDED #define DELETESTUDENT_H_INCLUDED void DeleteStudent(); //刪除學生信息 #endif // DELETESTUDENT_H_INCLUDED
(15)DeleteStudent.c
#include <stdio.h> #include <conio.h> //getc函數使用的頭文件 #include <windows.h> //Sleep函數使用的頭文件 #include <string.h>//strcmp函數使用的頭文 #include "DeleteStudent.h" #include "system.h" #include "MyList.h" //刪除學生信息 void DeleteStudent() { //先根據學號對該學生進行查找,查找到則刪除,沒有查找到則輸出提示信息 struct student studentData; struct Node* pMove = NULL; //用來接收查詢返回的結果 char cFlag; system("cls"); printf("\n"); printf("\t==================================【刪除學生信息】==============================\n"); printf("\t請輸入需要刪除的學生的學號:"); gets(studentData.studentId); pMove = searchInfoByData(studentData.studentId); if(pMove) //該學生存在,進行刪除 { //先對學生信息進行展示 printf("\t-------------------------------------------------------------------------------------\n"); printf("\t|%-10s |%-7s |%-4s |%-4s |%-12s |%-12s |%-12s |%-5s|\n","學號","姓名","性別","年齡","班級","專業","電話","入學成績"); printf("\t-------------------------------------------------------------------------------------\n"); printf("\t|%-10s |%-7s |%-4s |%-4d |%-12s |%-12s |%-12s |%-8d|\n", STUDENT_DATA); printf("\t-------------------------------------------------------------------------------------\n"); printf("\t已查找到該學生信息,是否刪除?(y/n)"); cFlag = getchar(); getchar(); //吃掉緩沖區的空格 while(cFlag != 'n' && cFlag!='y') { printf("輸入有誤,請輸入‘y'或者‘n'!"); printf("\n請選擇是否輸入學生信息(y/n):"); cFlag = getchar(); getchar(); } if(cFlag == 'n') return; else if(cFlag == 'y') { deleteAppointNode(studentData.studentId); printf("\t已刪除該學生信息!\n"); printf("\t刪除操作執行結束!\n"); } } else //找到瞭鏈表的末尾,或者鏈表為空 { printf("\t該學生不存在!無法執行刪除操作\n"); } }
(16)ModifyStudent.h
#ifndef MODIFYSTUDENT_H_INCLUDED #define MODIFYSTUDENT_H_INCLUDED #include "system.h" void ModifyStudent();//修改學生信息 void ShowModifyMenu();//展示修改選項菜單 void dealSelection(struct student studentData, int selection, struct Node *pMove); //處理用戶選擇的修改項 #endif // MODIFYSTUDENT_H_INCLUDED
(17)ModifyStudent.c
#include <stdio.h> #include <conio.h> //getc函數使用的頭文件 #include <windows.h> //Sleep函數使用的頭文件 #include <string.h>//strcmp函數使用的頭文 #include "ModifyStudent.h" #include "system.h" #include "MyList.h" //修改學生信息 void ModifyStudent() { struct student studentData; struct Node* pMove = NULL; //對學生信息查詢結果進行保存 int selection; //保存選擇信息 char isContinue = 'n'; //是否繼續進行修改 system("cls"); printf("\n"); printf("\t==================================【修改學生信息】==============================\n"); printf("\t請輸入需要修改信息的學生的學號:"); gets(studentData.studentId); pMove = searchInfoByData(studentData.studentId); if(pMove == NULL) { printf("\t該學生信息不存在,無法進行信息修改\n"); return; } else //可修改多條學生信息,也可以隻修改一條學生信息 { printf("\t-------------------------------------------------------------------------------------\n"); printf("\t|%-10s |%-7s |%-4s |%-4s |%-12s |%-12s |%-12s |%-5s|\n","學號","姓名","性別","年齡","班級","專業","電話","入學成績"); printf("\t-------------------------------------------------------------------------------------\n"); printf("\t|%-10s |%-7s |%-4s |%-4d |%-12s |%-12s |%-12s |%-8d|\n", STUDENT_DATA); printf("\t-------------------------------------------------------------------------------------\n"); printf("\t是否進行學生信息的修改?(y/n)"); scanf("%c", &isContinue); getchar(); while(isContinue != 'n' && isContinue !='y') { printf("\t輸入有誤,請輸入‘y'或者‘n'!"); printf("\t請選擇是否修改學生信息(y/n):"); isContinue = getchar(); getchar(); } if(isContinue == 'n') return; else { while(isContinue == 'y') { //system('cls'); ShowModifyMenu(); //printf("\t請選擇修改項: "); scanf("%d", &selection); getchar(); //對用戶的操作選擇進行處理 dealSelection(studentData,selection,pMove); fflush(stdin); printf("\t是否繼續修改學生信息(y/n)?"); isContinue = getchar(); getchar(); while(isContinue != 'n' && isContinue!='y') { printf("\n輸入有誤,請輸入‘y'或者‘n'!"); printf("\n請選擇是否繼續修改學生信息(y/n):"); isContinue = getchar(); getchar(); } } printf("\t學生信息修改完畢!\n"); } } } //學生信息修改菜單 void ShowModifyMenu() { printf("\n"); //printf("\t| 1.學號 |\n"); printf("\t| 1.姓名 |\n"); printf("\t| 2.性別 |\n"); printf("\t| 3.年齡 |\n"); printf("\t| 4.班級 |\n"); printf("\t| 5.專業 |\n"); printf("\t| 6.電話 |\n"); printf("\t| 7.入學成績 |\n"); printf("\n"); printf("請輸入所要修改的信息(鍵入相應的數字:1-7):"); } //處理用戶選擇的修改項 void dealSelection(struct student studentData, int selection, struct Node* pMove) { int ret; //用來接收scanf的返回值 switch (selection) { case 1: printf("\t請輸入輸入學生姓名:"); gets(studentData.name); strcpy(pMove->studentData.name, studentData.name); break; case 2: printf("\t請輸入學生性別(男-M,女-F):"); while (gets(studentData.sex) != NULL) { if (strcmp(studentData.sex, "F") == 0 || strcmp(studentData.sex, "M") == 0) break; printf("\t錯誤,隻能輸入'F'或者'M',請重新輸入\n"); printf("\t請輸入學生性別(男-M,女-F):"); } strcpy(pMove->studentData.sex,studentData.sex); break; case 3: printf("\t請輸入學生年齡(15-25):"); ret = scanf("%d", &studentData.age); while((ret != 1) || studentData.age<15 || studentData.age>25) { printf("\t輸入年齡錯誤,請重新輸入學生年齡(15-25):"); ret = scanf("%d", &studentData.age); } pMove->studentData.age = studentData.age; break; case 4: printf("\t請輸入學生班級(eg:B電子191):"); gets(studentData.className); strcpy(pMove->studentData.className, studentData.className); break; case 5: printf("\t請輸入學生專業:"); gets(studentData.major); strcpy(pMove->studentData.major, studentData.major); break; case 6: printf("\t請輸入學生電話:"); gets(studentData.tel); strcpy(pMove->studentData.tel, studentData.tel); break; case 7: printf("\t請輸入學生入學成績(100-750):"); ret = scanf("%d", &studentData.score); while((ret != 1) || studentData.score<200 || studentData.score>750) { printf("\t輸入成績信息錯誤,請重新輸入學生入學成績(200-750):"); ret = scanf("%d", &studentData.score); } pMove->studentData.score = studentData.score; break; default: printf("\t\t請輸入正確的數字!"); break; } }
(18)ShowStudentRanking.h
#ifndef SHOWSTUDENTRANKING_H_INCLUDED #define SHOWSTUDENTRANKING_H_INCLUDED #include "system.h" void ShowRanking(); //顯示學生排名 void sortByScore(struct studentRanking * stuRanking, int length); void Ranking(struct studentRanking * stuRanking, int length); #endif // SHOWSTUDENTRANKING_H_INCLUDED
(19)ShowStudentRanking.c
#include <stdio.h> #include <conio.h> //getc函數使用的頭文件 #include <windows.h> //Sleep函數使用的頭文件 #include <string.h>//strcmp函數使用的頭文 #include "ShowStudentRanking.h" #include "system.h" void ShowRanking() { //*對鏈表中學生的成績進行排名,並顯示排名結果,排名結果括學號姓名班級專業入學成績排名 //*排名是struct studentRanking 的一個結構體成員,在得出排名後,再進行展示 //*1.對鏈表中所有學生的成員進行排序,排序結果保存在student.ranking中 //重新定義一個結構體,保存排名信息並輸出 //定義一個結構體數組 struct Node *pMove = NULL; struct studentRanking *stuRanking; int i, j; int length = 0; //用來保存鏈表的長度 system("cls"); printf("\n"); printf("\t==================================【學生排名信息】==============================\n"); if(studentList == NULL) { printf("學生登記為空,無法進行成績排名\n"); return; } else //學生鏈表頭指針不為空,代表學生鏈表中存有學生信息 { pMove = studentList; //pMove指向鏈表的頭 //通過遍歷得到鏈表有多少個存儲單元 for(i=0; pMove != NULL; i++) { pMove = pMove->next; } length = i; printf("現有學生總人數為%d\n", length); //動態數組 stuRanking = (struct studentRanking *)malloc(length * sizeof(struct studentRanking)); if(stuRanking == NULL) return; //將需要輸出的學生信息復制到結構體數組當中 pMove = studentList; for(j=0; j<length; j++) { strcpy(stuRanking[j].studentId, pMove->studentData.studentId); strcpy(stuRanking[j].name , pMove->studentData.name); strcpy(stuRanking[j].className , pMove->studentData.className); stuRanking[j].score = pMove->studentData.score; pMove = pMove->next; } //復制完成後,根據成績對學生進行排序 sortByScore(stuRanking, length); //根據排序結果,為每名同學添加排名信息 Ranking(stuRanking, length); //展示排名 printf("排名結果如下:\n"); printf("\t-------------------------------------------------------\n"); printf("\t|%-10s |%-7s |%-12s |%-5s |%-5s|\n","學號","姓名","班級","入學成績","全級排名"); printf("\t-------------------------------------------------------\n"); for(j=0; j<length; j++) { printf("\t|%-10s |%-7s |%-12s |%-8d |%-8d|\n", STUDENT_RANKING); printf("\t-------------------------------------------------------\n"); } } printf("輸出排名信息完畢!\n"); system("pause"); } //通過成績對鏈表中的數據進行排序 void sortByScore(struct studentRanking *stuRanking, int length) { //進行冒泡排序,從大到小排序 int i, j; struct studentRanking temp; for(i=0; i<length-1; i++) { for(j=0; j<(length-i-1); j++) { if(stuRanking[j].score < stuRanking[j+1].score)//後一項比前一項大,則交換兩個存儲單元中的數據,一輪排序下來,最小項就位,在列表的最末尾 { temp = *(stuRanking+j); *(stuRanking+j) = *(stuRanking+j+1); *(stuRanking+j+1) =temp; } } } } void Ranking(struct studentRanking * stuRanking, int length) { int i; for(i=1; i<=length; i++) { stuRanking[i-1].ranking = i; } }
(20)AboutFiles.h
#ifndef ABOUTFILES_H_INCLUDED #define ABOUTFILES_H_INCLUDED //鏈表的讀取--文件讀操作 void readInfoFromFile(char* fileName); //鏈表的存儲--文件寫操作 void writeInfoToFile(char* fileName); #endif // ABOUTFILES_H_INCLUDED
(21)ShowStudentRanking.c
#include <stdio.h> #include <conio.h> //getc函數使用的頭文件 #include <windows.h> //Sleep函數使用的頭文件 #include <string.h>//strcmp函數使用的頭文 #include "system.h" #include "AboutFiles.h" #include "MyList.h" //鏈表的讀取--文件讀操作 void readInfoFromFile(char* fileName) { //步驟:先將信息讀到data裡面,再將信息讀到文件裡面 //1.打開文件 FILE *fp; struct student data; fp = fopen(fileName, "r"); if(fp == NULL) { fclose(fp); return NULL; } //2.讀文件 //格式化讀取文件,沒有讀到文件結束標志,則一直讀下去,讀到的數據插入到鏈表裡面 else { while(fscanf(fp, "%s\t%s\t%s\t%d\t%s\t%s\t%s\t%d\n", data.studentId,data.name,data.sex,&data.age,data.className,data.major,data.tel,&data.score) != EOF) { //將文件中原來的數據插入到鏈表當中 insertNodeByHead(data); } //3.關閉文件, fclose(fp); } } //鏈表的存儲--文件寫操作 void writeInfoToFile(char* fileName) { //1.打開文件D:\CodeBlocks\codeblocks C project\StudentSystemDemo02\studentList FILE *fp; struct Node* pMove = studentList; fp = fopen(fileName, "w"); if(fp == NULL) { //w+具有創建的功能,建立一個新文件可讀可寫 fp = fopen(fileName, "w+"); //可以給文件寫入一個表頭信息 } //2.寫文件, 按格式寫入操作 while(pMove != NULL) { fprintf(fp,"%s\t%s\t%s\t%d\t%s\t%s\t%s\t%d\n", STUDENT_DATA); pMove = pMove->next; } //3.關閉文件 fclose(fp); }
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。