C語言實現(xiàn)新生入學(xué)登記系統(tǒng)
本文實例為大家分享了C語言實現(xiàn)新生入學(xué)登記系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
項目所用數(shù)據(jù)結(jié)構(gòu):鏈表
算法:對鏈表數(shù)據(jù)的增刪改查操作,冒泡排序
系統(tǒng)架構(gòu)圖:
項目文件結(jié)構(gòu):
(1)system.h
#ifndef SYSTEM_H_INCLUDED #define SYSTEM_H_INCLUDED //宏定義學(xué)生信息的一種表示形式 #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]; ?//學(xué)號 ? ? ? char name[10]; ? ? ? char sex[4]; ? ? ? int ?age; ? ? ? char className[20]; ?//班級 ? ? ? char major[20]; ?//專業(yè) ? ? ? char tel[15]; ? ? ? int ?score; ? ?//入學(xué)成績 }; 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函數(shù)使用的頭文件 #include "main.h" //主函數(shù) int main() { int selection; Int ret; ? ? Begining(); ? ? showMenu(); ? ? ?//展示界面 ? ? ret = scanf("%d", &selection); //讀入用戶輸入數(shù)字 getchar(); While(ret != 1) { ? ? ?printf(“輸入錯誤,請選擇(0-6):”); ? ? ?ret = scanf("%d", &selection); //讀入用戶輸入數(shù)字 } ? ? while (selection) ? ? { ? ? ? ? switch (selection) ? ? ? ? { ? ? ? ? case 1: ? ? ? ? ? ? AddStudent(); ?//錄入學(xué)生信息 ? ? ? ? ? ? break; ? ? ? ? case 2: ? ? ? ? ? ? ShowStudent(); ?//瀏覽學(xué)生信息 ? ? ? ? ? ? break; ? ? ? ? case 3: ? ? ? ? ? ? SearchStudent(); //查找學(xué)生信息 ? ? ? ? ? ? break; ? ? ? ? case 4: ? ? ? ? ? ? DeleteStudent(); //刪除學(xué)生信息 ? ? ? ? ? ? break; ? ? ? ? case 5: ? ? ? ? ? ? ModifyStudent();//修改學(xué)生信息 ? ? ? ? ? ? break; ? ? ? ? case 6: ? ? ? ? ? ? ShowRanking(); //顯示學(xué)生排名 ? ? ? ? ? ? break; ? ? ? ? default: ? ? ? ? ? ? printf("\t\t請輸入正確的數(shù)字!\n"); ? ? ? ? } ? ? ? ? Ending(); //將鏈表數(shù)據(jù)寫入文件 ? ? ? ? printf("|按任意鍵返回系統(tǒng)菜單|"); ? ? ? ? getch(); //接收用戶輸入的任意字符 ? ? ? ? system("cls"); ? ? ? ? showMenu(); ? ? ? ? ret = scanf("%d", &selection); //提示用戶輸入數(shù)字 ? ? ? ? getchar(); ? ? While(ret != 1) { ? ? ? ? printf(“輸入錯誤,請選擇(0-6):”); ? ? ? ? ret = scanf("%d", &selection); //讀入用戶輸入數(shù)字 } ? ? } ? ? return 0; } void showMenu() { ? ? printf("\n\n\n\n\n"); ? ? printf("\t|--------------- 歡迎進入 ----------------|\n"); ? ? printf("\t| ? ? ? ? ? ? 新生入學(xué)登記系統(tǒng) ? ? ? ? ? ?|\n"); ? ? printf("\t| ? ? ? ? ? ? ? ? 主菜單 ? ? ? ? ? ? ? ? ?|\n"); ? ? printf("\t| ? ? ? ? ? ?1. 錄入學(xué)生信息 ? ? ? ? ? ? ?|\n"); ? ? printf("\t| ? ? ? ? ? ?2. 瀏覽學(xué)生信息 ? ? ? ? ? ? ?|\n"); ? ? printf("\t| ? ? ? ? ? ?3. 查找學(xué)生信息 ? ? ? ? ? ? ?|\n"); ? ? printf("\t| ? ? ? ? ? ?4. 刪除學(xué)生信息 ? ? ? ? ? ? ?|\n"); ? ? printf("\t| ? ? ? ? ? ?5. 修改學(xué)生信息 ? ? ? ? ? ? ?|\n"); ? ? printf("\t| ? ? ? ? ? ?6. 新生入學(xué)排名 ? ? ? ? ? ? ?|\n"); ? ? printf("\t| ? ? ? ? ? ?0. 退出系統(tǒng) ? ? ? ? ? ? ? ? ?|\n"); ? ? printf("\t|-----------------------------------------|\n"); ? ? printf("\n"); ? ? printf("\t\t請選擇(0-6):"); }
(4)BeginingAndEnding.h
#ifndef BEGININGANDENDING_H_INCLUDED #define BEGININGANDENDING_H_INCLUDED //關(guān)于啟動函數(shù)和結(jié)束函數(shù) void Begining(); void Ending(); #endif // BEGININGANDENDING_H_INCLUDED
(5)BeginingAndEnding.c
#include <stdio.h> #include <conio.h> ? //getc函數(shù)使用的頭文件 #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 //關(guān)于鏈表的函數(shù)聲明 //創(chuàng)建節(jié)點 struct Node* createNode(struct student data); //插入節(jié)點 void insertNodeByHead(struct student data); //指定位置刪除節(jié)點 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函數(shù)使用的頭文件 #include <windows.h> //Sleep函數(shù)使用的頭文件 #include <string.h>//strcmp函數(shù)使用的頭文 #include "system.h" #include "MyList.h" struct Node* studentList = NULL; ?//鏈表的頭指針 //創(chuàng)建節(jié)點 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; } //插入節(jié)點 void insertNodeByHead(struct student data) { ? ? ? struct Node* newNode = createNode(data); ? ? ? //表頭法插入,每次插入都將數(shù)據(jù)插入到頭節(jié)點的下一個,先判斷頭節(jié)點是否為空,為空則新節(jié)點就是頭節(jié)點,不為空,則插入在頭節(jié)點的下一個位置 ? ? ? if(studentList == NULL) ? ? ? { ? ? ? ? ? ? studentList = newNode; ? ? ? } ? ? ? else//不改變頭節(jié)點 ? ? ? { ? ? ? ? ? ? newNode->next = studentList->next; ? ? ? ? ? ? studentList->next = newNode; ? ? ? } } //指定位置刪除節(jié)點(知道這個節(jié)點的前驅(qū)和后續(xù),然后進行刪除) void deleteAppointNode( char* studentId) { ? ? //將鏈表頭部設(shè)為指定位置,先判斷頭節(jié)點是否為空,為空則鏈表沒有數(shù)據(jù),無法刪除 ? ? //如果頭節(jié)點不為空,則設(shè)頭結(jié)點為指定位置,如果頭結(jié)點是所找的節(jié)點,則刪除,如果不是,設(shè)頭結(jié)點的下一個為指定節(jié)點,頭結(jié)點為指定節(jié)點的前驅(qū)節(jié)點,一直向下查詢 ? ? //指定位置 ? ? struct Node* posNode = studentList; ? ? //指定位置的前面 ? ? struct Node* posFrontNode = NULL; ? ? //查找指定節(jié)點 ? ? if(posNode == NULL) ? ? { ? ? ? ?printf("數(shù)據(jù)為空無法刪除!\n"); ? ? ? ? return; ? ? } ? ? else ? ? { ? ? ? ? ? //姓名是字符串,不能直接比較, strcmp(), 相等返回值為0,相同返回值為負數(shù)或者正數(shù) ? ? ? ? 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)) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? //繼續(xù)向下一個節(jié)點移動 ? ? ? ? ? ? ? ? posFrontNode = posNode; ? ? ? ? ? ? ? ? posNode = posFrontNode->next; ? ? ? ? ? ? ? ? if(posNode == NULL) //找到了鏈表尾部,沒有找到數(shù)據(jù) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? printf("未找到指定位置,無法刪除!"); ? ? ? ? ? ? ? ? ? ? return; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? ? ? //查到到對應(yīng)數(shù)據(jù)后,進行刪除,將該節(jié)點架空,然后釋放該節(jié)點的存儲單元 ? ? ? ? ? ? posFrontNode->next = posNode->next; ? ? ? ? ? ? free(posNode); ? ? ? ? ? ? return; ? ? ? ? } ? ? } } //查找功能 struct Node* searchInfoByData(char* studentId) { ? ? ? struct Node* pMove; ? ? ? pMove = studentList; ? ? ? if(pMove == NULL) //頭節(jié)點為空,鏈表為空 ? ? ? { ? ? ? ? ? ? //printf("學(xué)生鏈表為空!\n"); ? ? ? ? ? ? return pMove; ? ? ? } ? ? ? else ? ? ? { ? ? ? ? ? ? // 查找到或者查找到鏈表最后,則結(jié)束循環(huán),返回查詢結(jié)果,結(jié)果為空,有兩種可能,一種是鏈表中沒有數(shù)據(jù),另一種是沒有查詢到 ? ? ? ? ? ? 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; ? ? ? //涉及到展示數(shù)據(jù) ? ? ? //表頭 ? ? ? 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","學(xué)號","姓名","性別","年齡","班級","專業(yè)","電話","入學(xué)成績"); ? ? ? ? ? ? ?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(); ?//錄入學(xué)生信息 #endif // ADDSTUDENT_H_INCLUDED
(9)AddStudent.c
#include <stdio.h> #include <conio.h> ? //getc函數(shù)使用的頭文件 #include <windows.h> //Sleep函數(shù)使用的頭文件 #include <string.h>//strcmp函數(shù)使用的頭文 #include "AddStudent.h" #include "system.h" #include ?"MyList.h" //錄入學(xué)生信息 void AddStudent() { ? ? struct student studentData; ? ? struct Node* isNull = NULL; ?//接收查詢的返回值 ? ? int iFlagExist; ?//保證不重復(fù)輸入 ? ? char cFlag; ? ? int ret; ?//用來接收scanf的返回值,判斷輸入數(shù)據(jù)是否正確 ? ? system("cls"); ? ? printf("===================================【錄入學(xué)生信息】===============================\n"); ? ? printf("\n請選擇是否輸入學(xué)生信息(y/n):"); ? ? cFlag = getchar(); ? ? getchar(); ? ? while(cFlag != 'n' && cFlag!='y') ? ? { ? ? ? ? ? printf("輸入有誤,請輸入‘y'或者‘n'!"); ? ? ? ? ? printf("\n請選擇是否輸入學(xué)生信息(y/n):"); ? ? ? ? ? cFlag = getchar(); ? ? ? ? ? getchar(); ? ? } ? ? if (cFlag == 'n') ? ? ? ? return; ? ? //循環(huán)輸入學(xué)生信息可輸入一條也可多條輸入 ? ? while (cFlag == 'y') ? ? { ? ? ? ? printf("請輸入學(xué)生學(xué)號:"); ? ? ? ? do ? ? ? ? { ? ? ? ? ? ? iFlagExist = 0; ? ? ? ? ? ? gets(studentData.studentId); ? ? ? ? ? ? //對學(xué)生編號在鏈表中進行查詢,對查詢結(jié)果進行判斷,如果存在則重新輸入,不存在則繼續(xù) ? ? ? ? ? ? isNull = searchInfoByData(studentData.studentId); ? ? ? ? ? ? if(isNull!= NULL) //可以查詢到 ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? printf("該學(xué)生已經(jīng)存在請重新輸入!\n"); ? ? ? ? ? ? ? ? ? printf("請輸入學(xué)生學(xué)號:"); ? ? ? ? ? ? ? ? ? iFlagExist = 1; ? ? ? ? ? ? } ? ? ? ? } while (iFlagExist == 1); ? ? ? ? //添加學(xué)生信息 ? ? ? ? printf("請輸入學(xué)生姓名:"); ? ? ? ? ret = scanf("%s",studentData.name); ? ? ? ? while(ret!=1) ? ? ? ? { ? ? ? ? ? ? ? printf("輸入學(xué)生姓名有誤,請重新輸入!\n"); ? ? ? ? ? ? ? printf("請輸入學(xué)生姓名:"); ? ? ? ? ? ? ? ret = scanf("%s",studentData.name); ? ? ? ? } ? ? ? ? getchar(); ? ? ? ? printf("請輸入學(xué)生性別(男-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("請輸入學(xué)生性別(男-M,女-F):"); ? ? ? ? } ? ? ? ? printf("請輸入學(xué)生年齡(15-25):"); ? ? ? ? ret = scanf("%d", &studentData.age); ? ? ? ? while((ret != 1) || studentData.age<15 || studentData.age>25) ? ? ? ? { ? ? ? ? ? ? ? printf("輸入年齡錯誤,請重新輸入學(xué)生年齡(15-25):"); ? ? ? ? ? ? ? ret = scanf("%d", &studentData.age); ? ? ? ? } ? ? ? ? getchar(); ? ? ? ? printf("請輸入學(xué)生班級(eg: B電子191):"); ? ? ? ? gets(studentData.className); ? ? ? ? printf("請輸入學(xué)生專業(yè):"); ? ? ? ? gets(studentData.major); ? ? ? ? printf("請輸入學(xué)生電話:"); ? ? ? ? gets(studentData.tel); ? ? ? ? printf("請輸入學(xué)生入學(xué)成績(200-750):"); ? ? ? ? ret = scanf("%d", &studentData.score); ? ? ? ? while((ret != 1) || studentData.score<200 || studentData.score>750) ? ? ? ? { ? ? ? ? ? ? ? printf("輸入成績信息錯誤,請重新輸入學(xué)生入學(xué)成績(200-750):"); ? ? ? ? ? ? ? ret = scanf("%d", &studentData.score); ? ? ? ? } ? ? ? ? getchar(); ? ? ? ? insertNodeByHead(studentData); ? ? ? ? fflush(stdin); ? ? ? ? printf("繼續(xù)輸入信息嗎(y/n):"); ? ? ? ? cFlag = getchar(); ? ? ? ? getchar(); ? ? ? ? while(cFlag != 'n' && cFlag!='y') ? ? ? ? { ? ? ? ? ? ? printf("輸入有誤,請輸入‘y'或者‘n'!"); ? ? ? ? ? ? printf("\n請選擇是否輸入學(xué)生信息(y/n):"); ? ? ? ? ? ? cFlag = getchar(); ? ? ? ? ? ? getchar(); ? ? ? ? } ? ? } ? ? printf("添加學(xué)生信息執(zhí)行完畢!\n"); }
(10)ShowStudent.h
#ifndef SHOWSTUDENT_H_INCLUDED #define SHOWSTUDENT_H_INCLUDED void ShowStudent(); //查找學(xué)生信息 #endif // SHOWSTUDENT_H_INCLUDED
(11)ShowStudent.c
#include <stdio.h> #include <conio.h> ? //getc函數(shù)使用的頭文件 #include <windows.h> //Sleep函數(shù)使用的頭文件 #include <string.h>//strcmp函數(shù)使用的頭文 #include "ShowStudent.h" #include "system.h" #include ?"MyList.h" //瀏覽學(xué)生信息 void ShowStudent() { ? ? ? system("cls"); ? ? ? printf("\n"); ? ? ? printf("\t====================================【瀏覽學(xué)生信息】================================\n"); ? ? ? printf("\n\n"); ? ? ? printList(); }
(12)SearchStudent.h
#ifndef SEARCHSTUDENT_H_INCLUDED #define SEARCHSTUDENT_H_INCLUDED void SearchStudent(); //查找學(xué)生信息 #endif // SEARCHSTUDENT_H_INCLUDED
(13)SearchStudent.c
#include <stdio.h> #include <conio.h> ? //getc函數(shù)使用的頭文件 #include <windows.h> //Sleep函數(shù)使用的頭文件 #include <string.h>//strcmp函數(shù)使用的頭文 #include "SearchStudent.h" #include "system.h" #include ?"MyList.h" //查找學(xué)生信息 void SearchStudent() { ? ? ? //查詢成功,則返回該學(xué)生信息,查詢失敗則輸出提示信息,可重新輸入,也可退出 ? ? ? struct student studentData; ? ? ? struct Node* pMove = NULL; //用來接收查詢返回的結(jié)果 ? ? ? char cFlag; ?//接收用戶的選擇 ? ? ? system("cls"); ? ? ? printf("\n"); ? ? ? printf("\t==================================【查找學(xué)生信息】==============================\n"); ? ? ? printf("\t是否進行學(xué)生查詢(y/n):"); ? ? ? cFlag = getchar(); ? ? ? getchar(); //接收回車鍵 ? ? ? while(cFlag != 'n' && cFlag!='y') ? ? ? { ? ? ? ? ? ? printf("輸入有誤,請輸入‘y'或者‘n'!"); ? ? ? ? ? ? printf("\n請選擇是否查詢學(xué)生信息(y/n):"); ? ? ? ? ? ? cFlag = getchar(); ? ? ? ? ? ? getchar(); ? ? ? } ? ? ? if (cFlag == 'n') ? ? ? ? return; ? ? ? while(cFlag == 'y') ? ? ? { ? ? ? ? printf("\t請輸入需要查找的學(xué)生的學(xué)號:"); ? ? ? ? //這里通過學(xué)號進行查詢,學(xué)號是唯一的,姓名有重名現(xiàn)象 ? ? ? ? gets(studentData.studentId); ? ? ? ? pMove = searchInfoByData(studentData.studentId); ? ? ? ? if(pMove) ?//pMove 為真時,表示查詢到 ? ? ? ? { ? ? ? ? ? ? printf("\t查詢成功,以下為該學(xué)生信息:\n"); ? ? ? ? ? ? printf("\t-------------------------------------------------------------------------------------\n"); ? ? ? ? ? ? printf("\t|%-10s |%-7s |%-4s |%-4s |%-12s |%-12s |%-12s |%-5s|\n","學(xué)號","姓名","性別","年齡","班級","專業(yè)","電話","入學(xué)成績"); ? ? ? ? ? ? printf("\t-------------------------------------------------------------------------------------\n"); ? ? ? ? ? ? printf("\t|%-10s |%-7s |%-4s |%-4d |%-12s |%-12s |%-12s |%-8d|\n", STUDENT_DATA); ? ? ? ? ? ? printf("\t-------------------------------------------------------------------------------------\n"); ? ? ? ? } ? ? ? ? else //pMove 為空時,未查詢到,這里為空有兩種情況,一種為學(xué)生名單為空,一種是沒有查詢到 ? ? ? ? { ? ? ? ? ? ? printf("\t查詢失敗,該學(xué)生不存在或?qū)W生列表為空!\n"); ? ? ? ? ? ? printf("\t是否重新查詢(y/n):"); ? ? ? ? ? ? cFlag = getchar(); ? ? ? ? ? ? getchar(); ? ? ? ? ? ? while(cFlag != 'n' && cFlag!='y') ? ? ? ? ? ? { ? ? ? ? ? ? ? ? printf("輸入有誤,請輸入‘y'或者‘n'!"); ? ? ? ? ? ? ? ? printf("\n是否重新查詢學(xué)生信息(y/n):"); ? ? ? ? ? ? ? ? cFlag = getchar(); ? ? ? ? ? ? ? ? getchar(); ? ? ? ? ? ? } ? ? ? ? } ? ? ? } ? ? ? printf("\t學(xué)生信息查詢結(jié)束!\n"); }
(14)DeleteStudent.h
#ifndef DELETESTUDENT_H_INCLUDED #define DELETESTUDENT_H_INCLUDED void DeleteStudent(); //刪除學(xué)生信息 #endif // DELETESTUDENT_H_INCLUDED
(15)DeleteStudent.c
#include <stdio.h> #include <conio.h> ? //getc函數(shù)使用的頭文件 #include <windows.h> //Sleep函數(shù)使用的頭文件 #include <string.h>//strcmp函數(shù)使用的頭文 #include "DeleteStudent.h" #include "system.h" #include ?"MyList.h" //刪除學(xué)生信息 void DeleteStudent() { ? ? ? //先根據(jù)學(xué)號對該學(xué)生進行查找,查找到則刪除,沒有查找到則輸出提示信息 ? ? ? struct student studentData; ? ? ? struct Node* pMove = NULL; //用來接收查詢返回的結(jié)果 ? ? ? char cFlag; ? ? ? system("cls"); ? ? ? printf("\n"); ? ? ? printf("\t==================================【刪除學(xué)生信息】==============================\n"); ? ? ? printf("\t請輸入需要刪除的學(xué)生的學(xué)號:"); ? ? ? gets(studentData.studentId); ? ? ? pMove = searchInfoByData(studentData.studentId); ? ? ? if(pMove) ?//該學(xué)生存在,進行刪除 ? ? ? { ? ? ? ? //先對學(xué)生信息進行展示 ? ? ? ? printf("\t-------------------------------------------------------------------------------------\n"); ? ? ? ? printf("\t|%-10s |%-7s |%-4s |%-4s |%-12s |%-12s |%-12s |%-5s|\n","學(xué)號","姓名","性別","年齡","班級","專業(yè)","電話","入學(xué)成績"); ? ? ? ? printf("\t-------------------------------------------------------------------------------------\n"); ? ? ? ? printf("\t|%-10s |%-7s |%-4s |%-4d |%-12s |%-12s |%-12s |%-8d|\n", STUDENT_DATA); ? ? ? ? printf("\t-------------------------------------------------------------------------------------\n"); ? ? ? ? printf("\t已查找到該學(xué)生信息,是否刪除?(y/n)"); ? ? ? ? cFlag = getchar(); ? ? ? ? getchar(); //吃掉緩沖區(qū)的空格 ? ? ? ? while(cFlag != 'n' && cFlag!='y') ? ? ? ? { ? ? ? ? ? ? printf("輸入有誤,請輸入‘y'或者‘n'!"); ? ? ? ? ? ? printf("\n請選擇是否輸入學(xué)生信息(y/n):"); ? ? ? ? ? ? cFlag = getchar(); ? ? ? ? ? ? getchar(); ? ? ? ? } ? ? ? ? if(cFlag == 'n') ? ? ? ? ? ? return; ? ? ? ? else if(cFlag == 'y') ? ? ? ? { ? ? ? ? ? ? ? deleteAppointNode(studentData.studentId); ? ? ? ? ? ? ? printf("\t已刪除該學(xué)生信息!\n"); ? ? ? ? ? ? ? printf("\t刪除操作執(zhí)行結(jié)束!\n"); ? ? ? ? } ? ? ? } ? ? ? else //找到了鏈表的末尾,或者鏈表為空 ? ? ? { ? ? ? ? printf("\t該學(xué)生不存在!無法執(zhí)行刪除操作\n"); ? ? ? } }
(16)ModifyStudent.h
#ifndef MODIFYSTUDENT_H_INCLUDED #define MODIFYSTUDENT_H_INCLUDED #include "system.h" void ModifyStudent();//修改學(xué)生信息 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函數(shù)使用的頭文件 #include <windows.h> //Sleep函數(shù)使用的頭文件 #include <string.h>//strcmp函數(shù)使用的頭文 #include "ModifyStudent.h" #include "system.h" #include "MyList.h" //修改學(xué)生信息 void ModifyStudent() { ? ? ? struct student studentData; ? ? ? struct Node* pMove = NULL; //對學(xué)生信息查詢結(jié)果進行保存 ? ? ? int selection; ?//保存選擇信息 ? ? ? char isContinue = 'n'; ?//是否繼續(xù)進行修改 ? ? ? system("cls"); ? ? ? printf("\n"); ? ? ? printf("\t==================================【修改學(xué)生信息】==============================\n"); ? ? ? printf("\t請輸入需要修改信息的學(xué)生的學(xué)號:"); ? ? ? gets(studentData.studentId); ? ? ? pMove = searchInfoByData(studentData.studentId); ? ? ? if(pMove == NULL) ? ? ? { ? ? ? ? ? ? printf("\t該學(xué)生信息不存在,無法進行信息修改\n"); ? ? ? ? ? ? return; ? ? ? } ? ? ? else ?//可修改多條學(xué)生信息,也可以只修改一條學(xué)生信息 ? ? ? { ? ? ? ? ? ? printf("\t-------------------------------------------------------------------------------------\n"); ? ? ? ? ? ? printf("\t|%-10s |%-7s |%-4s |%-4s |%-12s |%-12s |%-12s |%-5s|\n","學(xué)號","姓名","性別","年齡","班級","專業(yè)","電話","入學(xué)成績"); ? ? ? ? ? ? printf("\t-------------------------------------------------------------------------------------\n"); ? ? ? ? ? ? printf("\t|%-10s |%-7s |%-4s |%-4d |%-12s |%-12s |%-12s |%-8d|\n", STUDENT_DATA); ? ? ? ? ? ? printf("\t-------------------------------------------------------------------------------------\n"); ? ? ? ? ? ? printf("\t是否進行學(xué)生信息的修改?(y/n)"); ? ? ? ? ? ? scanf("%c", &isContinue); ? ? ? ? ? ? getchar(); ? ? ? ? ? ? while(isContinue != 'n' && isContinue !='y') ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? printf("\t輸入有誤,請輸入‘y'或者‘n'!"); ? ? ? ? ? ? ? ? ? printf("\t請選擇是否修改學(xué)生信息(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是否繼續(xù)修改學(xué)生信息(y/n)?"); ? ? ? ? ? ? ? ? ? ? ? ? isContinue = getchar(); ? ? ? ? ? ? ? ? ? ? ? ? getchar(); ? ? ? ? ? ? ? ? ? ? ? ? while(isContinue != 'n' && isContinue!='y') ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? printf("\n輸入有誤,請輸入‘y'或者‘n'!"); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? printf("\n請選擇是否繼續(xù)修改學(xué)生信息(y/n):"); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? isContinue = getchar(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? getchar(); ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? printf("\t學(xué)生信息修改完畢!\n"); ? ? ? ? ? ?} ? ? ? } } //學(xué)生信息修改菜單 void ShowModifyMenu() { ? ? printf("\n"); ? ? //printf("\t| ? ? ? ? ? ? ?1.學(xué)號 ? ? ? ? ? ? ?|\n"); ? ? printf("\t| ? ? ? ? ? ? ?1.姓名 ? ? ? ? ? ? ?|\n"); ? ? printf("\t| ? ? ? ? ? ? ?2.性別 ? ? ? ? ? ? ?|\n"); ? ? printf("\t| ? ? ? ? ? ? ?3.年齡 ? ? ? ? ? ? ?|\n"); ? ? printf("\t| ? ? ? ? ? ? ?4.班級 ? ? ? ? ? ? ?|\n"); ? ? printf("\t| ? ? ? ? ? ? ?5.專業(yè) ? ? ? ? ? ? ?|\n"); ? ? printf("\t| ? ? ? ? ? ? ?6.電話 ? ? ? ? ? ? ?|\n"); ? ? printf("\t| ? ? ? ? ? ? ?7.入學(xué)成績 ? ? ? ? ?|\n"); ? ? printf("\n"); ? ? printf("請輸入所要修改的信息(鍵入相應(yīng)的數(shù)字:1-7):"); } //處理用戶選擇的修改項 void dealSelection(struct student studentData, int selection, struct Node* pMove) { ? ? int ret; //用來接收scanf的返回值 ? ? switch (selection) ? ? { ? ? case 1: ? ? ? ? printf("\t請輸入輸入學(xué)生姓名:"); ? ? ? ? gets(studentData.name); ? ? ? ? strcpy(pMove->studentData.name, studentData.name); ? ? ? ? break; ? ? case 2: ? ? ? ? printf("\t請輸入學(xué)生性別(男-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請輸入學(xué)生性別(男-M,女-F):"); ? ? ? ? } ? ? ? ? strcpy(pMove->studentData.sex,studentData.sex); ? ? ? ? break; ? ? case 3: ? ? ? ? printf("\t請輸入學(xué)生年齡(15-25):"); ? ? ? ? ret = scanf("%d", &studentData.age); ? ? ? ? while((ret != 1) || studentData.age<15 || studentData.age>25) ? ? ? ? { ? ? ? ? ? ? ? printf("\t輸入年齡錯誤,請重新輸入學(xué)生年齡(15-25):"); ? ? ? ? ? ? ? ret = scanf("%d", &studentData.age); ? ? ? ? } ? ? ? ? pMove->studentData.age = studentData.age; ? ? ? ? break; ? ? case 4: ? ? ? ? printf("\t請輸入學(xué)生班級(eg:B電子191):"); ? ? ? ? gets(studentData.className); ? ? ? ? strcpy(pMove->studentData.className, studentData.className); ? ? ? ? break; ? ? case 5: ? ? ? ? printf("\t請輸入學(xué)生專業(yè):"); ? ? ? ? gets(studentData.major); ? ? ? ? strcpy(pMove->studentData.major, studentData.major); ? ? ? ? break; ? ? case 6: ? ? ? ? printf("\t請輸入學(xué)生電話:"); ? ? ? ? gets(studentData.tel); ? ? ? ? strcpy(pMove->studentData.tel, studentData.tel); ? ? ? ? break; ? ? case 7: ? ? ? ? printf("\t請輸入學(xué)生入學(xué)成績(100-750):"); ? ? ? ? ret = scanf("%d", &studentData.score); ? ? ? ? while((ret != 1) || studentData.score<200 || studentData.score>750) ? ? ? ? { ? ? ? ? ? ? ? printf("\t輸入成績信息錯誤,請重新輸入學(xué)生入學(xué)成績(200-750):"); ? ? ? ? ? ? ? ret = scanf("%d", &studentData.score); ? ? ? ? } ? ? ? ? pMove->studentData.score = studentData.score; ? ? ? ? break; ? ? default: ? ? ? ? printf("\t\t請輸入正確的數(shù)字!"); ? ? ? ? break; ? ? } }
(18)ShowStudentRanking.h
#ifndef SHOWSTUDENTRANKING_H_INCLUDED #define SHOWSTUDENTRANKING_H_INCLUDED #include "system.h" void ShowRanking(); //顯示學(xué)生排名 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函數(shù)使用的頭文件 #include <windows.h> //Sleep函數(shù)使用的頭文件 #include <string.h>//strcmp函數(shù)使用的頭文 #include "ShowStudentRanking.h" #include "system.h" void ShowRanking() { ? ? ? //*對鏈表中學(xué)生的成績進行排名,并顯示排名結(jié)果,排名結(jié)果括學(xué)號姓名班級專業(yè)入學(xué)成績排名 ? ? ? //*排名是struct studentRanking 的一個結(jié)構(gòu)體成員,在得出排名后,再進行展示 ? ? ? //*1.對鏈表中所有學(xué)生的成員進行排序,排序結(jié)果保存在student.ranking中 ? ? ? //重新定義一個結(jié)構(gòu)體,保存排名信息并輸出 ? ? ? //定義一個結(jié)構(gòu)體數(shù)組 ? ? ? struct Node *pMove = NULL; ? ? ? struct studentRanking *stuRanking; ? ? ? int i, j; ? ? ? int length = 0; //用來保存鏈表的長度 ? ? ? system("cls"); ? ? ? printf("\n"); ? ? ? printf("\t==================================【學(xué)生排名信息】==============================\n"); ? ? ? if(studentList == NULL) ? ? ? { ? ? ? ? ? printf("學(xué)生登記為空,無法進行成績排名\n"); ? ? ? ? ? return; ? ? ? } ? ? ? else //學(xué)生鏈表頭指針不為空,代表學(xué)生鏈表中存有學(xué)生信息 ? ? ? { ? ? ? ? ? pMove = studentList; ?//pMove指向鏈表的頭 ? ? ? ? ? //通過遍歷得到鏈表有多少個存儲單元 ? ? ? ? ? for(i=0; pMove != NULL; i++) ? ? ? ? ? { ? ? ? ? ? ? ?pMove = pMove->next; ? ? ? ? ? } ? ? ? ? ? length = i; ? ? ? ? ? printf("現(xiàn)有學(xué)生總?cè)藬?shù)為%d\n", length); ? ? ? ? ? //動態(tài)數(shù)組 ? ? ? ? ? stuRanking = (struct studentRanking *)malloc(length * sizeof(struct studentRanking)); ? ? ? ? ? if(stuRanking == NULL) ? ? ? ? ? ? return; ? ? ? ? ? //將需要輸出的學(xué)生信息復(fù)制到結(jié)構(gòu)體數(shù)組當(dāng)中 ? ? ? ? ? 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; ? ? ? ? ? } ? ? ? ? ? //復(fù)制完成后,根據(jù)成績對學(xué)生進行排序 ? ? ? ? ? sortByScore(stuRanking, length); ? ? ? ? ? //根據(jù)排序結(jié)果,為每名同學(xué)添加排名信息 ? ? ? ? ? Ranking(stuRanking, length); ? ? ? ? ? //展示排名 ? ? ? ? ? printf("排名結(jié)果如下:\n"); ? ? ? ? ? printf("\t-------------------------------------------------------\n"); ? ? ? ? ? printf("\t|%-10s |%-7s |%-12s |%-5s |%-5s|\n","學(xué)號","姓名","班級","入學(xué)成績","全級排名"); ? ? ? ? ? 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"); } //通過成績對鏈表中的數(shù)據(jù)進行排序 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)//后一項比前一項大,則交換兩個存儲單元中的數(shù)據(jù),一輪排序下來,最小項就位,在列表的最末尾 ? ? ? ? ? ? { ? ? ? ? ? ? ? ? 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函數(shù)使用的頭文件 #include <windows.h> //Sleep函數(shù)使用的頭文件 #include <string.h>//strcmp函數(shù)使用的頭文 #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.讀文件 ? ? ? //格式化讀取文件,沒有讀到文件結(jié)束標(biāo)志,則一直讀下去,讀到的數(shù)據(jù)插入到鏈表里面 ? ? ? 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) ? ? ? ? ? ? { ? ? ? ? ? ? //將文件中原來的數(shù)據(jù)插入到鏈表當(dāng)中 ? ? ? ? ? ? ? ? ? insertNodeByHead(data); ? ? ? ? ? ? } ? ? ? ? ? ?//3.關(guān)閉文件, ? ? ? ? ? ?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+具有創(chuàng)建的功能,建立一個新文件可讀可寫 ? ? ? ? ? ? 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.關(guān)閉文件 ? ? ? fclose(fp); }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
visual?studio?2022?編譯出來的文件被刪除并監(jiān)視目錄中的文件變更(示例詳解)
這篇文章主要介紹了visual?studio?2022?編譯出來的文件被刪除?并監(jiān)視目錄中的文件變更,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-08-08