C語言編寫實現(xiàn)學生管理系統(tǒng)
本文實例為大家分享了C語言實現(xiàn)學生管理系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
項目介紹
學生信息管理系統(tǒng)是一個基于C語言開發(fā)的系統(tǒng),其中有用到冒泡排序、指針、結(jié)構(gòu)體、二位數(shù)組等知識。通過模塊化的方法編寫各個函數(shù),其中在主界面函數(shù)調(diào)用各個模塊的函數(shù)的實現(xiàn)以下具體功能:
1、學生信息的增刪改查
2、學生成績的排序
3、統(tǒng)計學生人數(shù)
4、顯示所有學生的信息。
5、對學生信息存檔
總體設(shè)計
本實驗通過在main函數(shù)打開保存數(shù)據(jù)結(jié)果的文檔和調(diào)用主界面函數(shù);在主界面函數(shù)welcom()中實現(xiàn)邊框的繪制,以及顯示各個功能及各個功能對應(yīng)的函數(shù)實現(xiàn)方法。
錄入信息函數(shù)addInfo():此函數(shù)通過結(jié)構(gòu)體student來保存錄入的信息,其中為了確保數(shù)據(jù)的后續(xù)操作的準確性,通過學號的唯一性來標識每個學生的信息,通過編寫及調(diào)用一個isIdSame()函數(shù),該函數(shù)通過遍歷所有學號確認學號來保障學號的唯一性,學號重復會提示用戶需要重新輸入函數(shù)。
查找學生信息函數(shù):通過學號查找學生的信息,編寫并調(diào)用一個findIndex()函數(shù),該函數(shù)會遍歷結(jié)構(gòu)體的學號信息,查詢到會返回該學號的坐標,沒有找到該學號則返回-1;通過變量target來保存返回的結(jié)果。如果target不等于-1,則程序找到了該學號,通過編寫并調(diào)用一個showInfo()函數(shù)來輸出所有該學生的信息;否則輸出查詢此人,因為下標不可能為負數(shù)。
更新學生信息函數(shù)update():通過學號來找到該學生,調(diào)用findIndex()函數(shù)來確定該學生的位置,如果返回結(jié)果是小于0則函數(shù)結(jié)束,查無此人;若大于0則找到該學生,通過do…while函數(shù)switch選擇語句的嵌套來進行用戶需要求改某一項的內(nèi)容。
刪除函數(shù)del():查找學生的步驟跟更新學生信息函數(shù)的流程一樣,如果findIndex()函數(shù)小于0則函數(shù)結(jié)束,否則通過一個for循環(huán)把結(jié)構(gòu)體的數(shù)組從光標開始往前覆蓋從而達到刪除效果。
插入學生信息函數(shù)inserInfo():通過要求用戶輸入位置來定位插入到位置,輸入用戶輸入的大于結(jié)構(gòu)體數(shù)組的總數(shù)則插入到最后一個數(shù)組。否則通過一個for循環(huán),把數(shù)組從最后開始往后移一位,把用戶輸入的位置的結(jié)果移到后一數(shù)組就編寫并調(diào)用插入函數(shù)inserCurrentInfo()對當前位置數(shù)組進行覆蓋插入。inserCurrentInfo()函數(shù)只負責對接收到的該位置的元素所有信息的寫入。
排序函數(shù)sortTotal():創(chuàng)建一個臨時變量提供元素與元素之間交換信息。通過雙循環(huán)嵌套結(jié)構(gòu)進行結(jié)構(gòu)體的分數(shù)進行大小對比、交換位置來進行冒泡排序。最后排序完成之后輸出分數(shù)由高到低排序的所有學生的信息。
顯示學生信息函數(shù)showAllInfo():該函數(shù)通過全局變量count(該變量記錄了所有添加、插入或刪除過的學生信息,能準確記錄學生的總?cè)藬?shù))通過for循環(huán)去遍歷student結(jié)構(gòu)體,從而輸出所有的所生信息。
學生數(shù)據(jù)存檔函數(shù)writeData():該函數(shù)定義一個指針,以寫入方式打開”stu.txt文本”,并把該文本的地址賦給指針fp。通過一個for循環(huán)遍歷結(jié)構(gòu)體里的元素,把結(jié)構(gòu)體里的元素的屬性輸入到”stu.txt文本”。
詳細代碼
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <conio.h> #include <windows.h> void addInfo();// 添加 void welcom(); //主界面? void showAllInfo();//展示所有信息? void showInfo(struct Student student);//展示學生信息? int findIndex(struct Student student[],int id);// 根據(jù)學號 返回對應(yīng)下標? void del(); //刪除? void search();// 查找學生信息? void updata();//更新? void sortTotal();//按總分排序? void writeData();//數(shù)據(jù)寫入文件中? void initData();//初始化數(shù)據(jù) 從文件中讀取數(shù)據(jù),初始化數(shù)組 void showCount(); // 展示存儲學生個數(shù)? void inserInfo();//插入學生信息? void inserCurrentInfo(int site); //當前位置插入? void con();//按任意鍵繼續(xù)? int find1(struct Student student[],int id); //判斷學號是否有重復 重復返回1 不重復返回0? void isIdSame(int x); //校驗所輸入學號是否重復? void gotoxy(int x,int y);//光標定位 int color(int c); //設(shè)置顏色輸出? struct Student{ ?? ?int id; ?? ?char name[20]; ?? ?int _math; ?? ?int _chinese; ?? ?int _english; ?? ?int total;// 總分? } student[500];? int count=0;// 記錄當前數(shù)組中存儲學生個數(shù)? //主函數(shù)? int main(){ ?? ?initData();? ?? ?welcom();? ?? ?return 0; } // 光標定位? void gotoxy(int x, int y) { ?? ?COORD pos; ??? ?pos.X = x; ??? ?//橫坐標 ??? ?pos.Y = y; ??? ?//縱坐標 ??? ?SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos); } //設(shè)置顏色輸出? int color(int c) { ?? ?SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c); ? ? ? ?//更改文字顏色 ?? ?return 0; };? // 主界面? void welcom() { ?? ?while(1){ ?? ??? ?system("cls"); ?? ??? ??? ?int n; ?? ??? ??? ?int i,j = 1; ?? ??? ??? ?color(10); ? ? ? ? ??? ??? ??? ??? ??? ??? ??? ??? ?//淡綠色邊框 ?? ??? ??? ?for (i = 5; i <= 35; i++) ? ?? ??? ??? ??? ??? ??? ??? ?//循環(huán)y軸坐標,打印輸出上下邊框=== ?? ??? ??? ?{ ?? ??? ??? ??? ?for (j = 10; j <= 57; j++) ??? ??? ??? ??? ??? ?//循環(huán)x軸坐標,打印輸出左右邊框|| ?? ??? ??? ??? ?{ ?? ??? ??? ??? ??? ?gotoxy(j, i); ?? ??? ??? ??? ??? ?if (i == 5 || i == 35) printf("=");?? ??? ??? ?//輸出上下邊框=== ?? ??? ??? ??? ??? ?else if (j == 10 || j == 56) printf("||");?? ?//輸出左右邊框|| ?? ??? ??? ??? ?} ?? ??? ??? ?} ?? ??? ? ?? ?color(15);//白色? ?? ??? ? ?? ?gotoxy(25,8); ?? ??? ? ?? ?printf("學生信息管理系統(tǒng)"); ?? ??? ??? ?color(14);?? ??? ??? ??? ?//設(shè)置字體顏色為黃色 ?? ??? ??? ?gotoxy(15, 12); ?? ??? ??? ?printf("1:錄入學生信息"); ?? ??? ??? ?gotoxy(35, 12); ?? ??? ??? ?printf("2.查找學生信息"); ?? ??? ??? ?gotoxy(15, 16); ?? ??? ??? ?printf("3.刪除學生信息"); ?? ??? ??? ?gotoxy(35,16); ?? ??? ??? ?printf("4.修改學生信息"); ?? ??? ??? ?gotoxy(15, 20); ?? ??? ??? ?printf("5.插入學生信息"); ?? ??? ??? ?gotoxy(35,20); ?? ??? ??? ?printf("6.按照學生成績排序");?? ?? ?? ??? ??? ?gotoxy(15, 24); ?? ??? ??? ?printf("7.統(tǒng)計學生總數(shù)"); ?? ??? ??? ?gotoxy(35,24); ?? ??? ??? ?printf("8.顯示所有學生信息"); ?? ??? ??? ?gotoxy(15, 28); ?? ??? ??? ?printf("9.學生數(shù)據(jù)存檔并退出"); ?? ??? ??? ?gotoxy(25,32);? ?? ??? ??? ?int choose; ?? ??? ??? ?printf("請選擇:[ ]\b\b"); //\b 光標回退一格 ? ?? ??? ??? ?color(15); // ?顏色變回白色? ?? ??? ? ?? ?scanf("%d", &choose); ?? ??? ? ?? ??? ?switch (choose){ ?? ??? ? ?? ? ?? ??? ?case 1:addInfo(); break; ?? ??? ? ?? ? ?? ??? ?case 2:search(); break; ?? ??? ? ?? ? ?? ??? ?case 3:del(); break; ?? ??? ? ?? ? ?? ??? ?case 4:updata(); break; ?? ??? ? ?? ? ?? ??? ?case 5:inserInfo();break;? ?? ??? ? ?? ? ?? ??? ?case 6:sortTotal(); break; ?? ??? ? ?? ? ?? ??? ?case 7:showCount(); break; ?? ??? ? ?? ? ?? ??? ?case 8:showAllInfo(); break; ?? ??? ? ?? ? ?? ??? ?case 9:writeData();exit(0); ?? ??? ? ?? ? ?? ??? ?}? ?? ?}?? ??? ??? ??? ? } // 添加? void addInfo(){ ?? ?system("cls"); ?? ?printf("\t添加學生信息\n"); ?? ?printf("請輸入學號\n"); ?? ?isIdSame(count); ?? ?printf("請輸入姓名\n"); ?? ?scanf("%s",&student[count].name); ?? ?printf("請輸入語文成績\n"); ?? ?scanf("%d",&student[count]._chinese); ?? ?printf("請輸入數(shù)學成績\n"); ?? ?scanf("%d",&student[count]._math); ?? ?printf("請輸入英語成績\n"); ?? ?scanf("%d",&student[count]._english); ?? ?student[count].total=student[count]._chinese+student[count]._english+student[count]._math; ?? ?printf("%s的信息錄入成功\n\n",student[count].name); ?? ?int choose; ?? ?printf("1繼續(xù) 2返回主界面\n");? ?? ?count++; ? ?? ?scanf("%d",&choose); ?? ?if(choose==1){ ?? ??? ?addInfo(); ?? ?}? ?? ?system("cls");? ? }? ? // 查找 展示結(jié)果? void search(){ ?? ?system("cls"); ?? ?int id; ?? ?printf("請輸入你想查找學生的學號\n"); ?? ?scanf("%d",&id); ?? ?int target = findIndex(student,id); ?//目標下表 ?? ?int flag=1;//是否存在要查詢的學號? ?? ? ?? ?//for循環(huán)對比? ?? ?if(target != -1)? ?? ?{ ?? ??? ?printf("\n\t查詢結(jié)果\n\n"); ?? ??? ?showInfo(student[target]); ?? ??? ?con(); ? ?? ??? ? ?? ?} ? ?? ?else{ // 輸出查詢結(jié)果? ?? ??? ??? ?printf("\n查無此人\n");? ?? ??? ??? ??? ?con(); ?? ?}? } ? ? // 更新? void updata(){ ?? ?system("cls"); ?? ?int id; ?? ?printf("請輸入你要修改學生的學號\n"); ?? ?scanf("%d",&id); ?? ?int?? ?target = findIndex(student,id); ? ?? ?if(target<0){ ?? ??? ?printf("查無此人"); ?? ??? ?con();? ?? ?}else{ ?? ??? ?int flag=1;?? ? ?? ?do{ ?? ??? ?int choose=0; ?? ??? ?printf("請輸入需要修改的選項\t(1.學號\t2.姓名\t3.語文\t4.數(shù)學\t5.英語):\n"); ?? ??? ?scanf("%d",&choose);? ?? ??? ??? ?switch (choose) { ?? ??? ??? ??? ?case 1: ?? ??? ??? ??? ??? ?printf("請輸入學號\n"); //?? ??? ??? ??? ??? ?scanf("%d",&student[target].id); ?? ??? ??? ??? ??? ?isIdSame(target);?? ??? ??? ? ?? ??? ??? ??? ??? ?break; ?? ??? ??? ??? ?case 2: ?? ??? ??? ??? ??? ?printf("請輸入姓名\n"); ?? ??? ??? ??? ??? ?scanf("%s",&student[target].name); ?? ??? ??? ??? ??? ?break; ?? ??? ??? ??? ?case 3: ?? ??? ??? ??? ??? ?printf("請輸入語文成績\n"); ?? ??? ??? ??? ??? ?scanf("%d",&student[target]._chinese); ?? ??? ??? ??? ??? ?break;? ?? ??? ??? ??? ?case 4: ?? ??? ??? ??? ??? ?printf("請輸入數(shù)學成績\n"); ?? ??? ??? ??? ??? ?scanf("%d",&student[target]._math); ?? ??? ??? ??? ??? ?break;?? ? ?? ??? ??? ??? ?case 5: ?? ??? ??? ??? ??? ?printf("請輸入英語成績\n"); ?? ??? ??? ??? ??? ?scanf("%d",&student[target]._english); ?? ??? ??? ??? ??? ?break;?? ??? ? ? ?? ??? ??? ?}? ?? ??? ??? ?student[target].total=student[target]._chinese+student[target]._english+student[target]._math; ?? ??? ??? ?printf("%s的信息修改成功\n",student[target].name); ?? ??? ??? ?printf("\n按1繼續(xù) 按2退出修改\n"); ?? ??? ??? ?int choose2;? ?? ??? ??? ?scanf("%d",&choose2); ?? ??? ??? ?if(choose2==1){ ?? ??? ??? ??? ?flag=1; ?? ??? ??? ?}else{ ?? ??? ??? ??? ?flag=0; ?? ??? ??? ?}? ?? ??? ??? ? ?? ?}while(flag); ? ?? ?}?? ? }? //刪除 void del(){ ?? ?system("cls");?? ? ?? ?int id; ?? ?int target;//目標元素的下標? ?? ?printf("\n請輸入你想刪除學生的學號\n"); ?? ?scanf("%d",&id); ?? ?target=findIndex(student, id); ?? ?if(target<0){ ?? ??? ?printf("\n查無此人\n"); ?? ??? ?con(); ?? ??? ? ?? ?} else{ ?? ??? ? for(int i=target;i<count;i++){ ?? ??? ? ?? ?student[i]=student[i+1]; //刪除操作 后一位元素覆蓋前一位元素? ?? ??? ? } ?? ??? ?printf("刪除成功\n"); ?? ??? ?con(); ?? ?count--;? ?? ?}? } ? //插入學生信息 void inserInfo(){ ?? ?system("cls"); ?? ?int site; //位置? ?? ?printf("請輸入你要插入學生信息的位置(從0開始):\n"); ?? ?scanf("%d",&site); ?? ?//插入位置大于總數(shù),則插入在數(shù)組最后一位 ?? ?if ( site > count){ ?? ??? ?inserCurrentInfo(count);? ?? ??? ?printf("%s的信息插入成功\n", student[count].name); ?? ??? ? ?? ?}else{ ?? ??? ?//不是最后一位 從當前位置 數(shù)組全部后移一位? ?? ??? ?for (int i = count; i >= site; i--){ ?? ??? ??? ??? ?student[i + 1] = student[i]; ?? ??? ??? ?} ?? ??? ?//在當前位置添加學員 ?? ??? ?inserCurrentInfo(site); ?? ??? ?printf("%s同學的信息插入成功\n", student[site].name); ?? ??? ?con();? ?? ??? ?} ? }? //當前位置插入 void inserCurrentInfo(int site){ ??? ?printf("請輸入學號\n"); ??? ?isIdSame(site); ??? ?printf("請輸入姓名\n"); ??? ?scanf("%s", student[site].name); ??? ?printf("請輸入語文成績\n"); ??? ?scanf("%d", &student[site]._chinese); ??? ?printf("請輸入數(shù)學成績\n"); ??? ?scanf("%d", &student[site]._math); ??? ?printf("請輸入英語成績\n"); ??? ?scanf("%d", &student[site]._english); ?? ?student[site].total= student[site]._chinese+student[site]._english+student[site]._math; ??? ?count++; ??? ?con(); ?} // 判斷學號是否重復 重復返回1 否則返回0? int find1(struct Student student[],int id) { ?int temp = 0; ?for(int i=0;i<count;i++) ? { ? ?if(student[i].id==id) ? ? { ? ? ?temp=1; ? ? ?break; ? ? } ? }? ?return temp; }? //校驗所添加學號是否重復 void isIdSame(int x){ ? ?? ?int inputId;?? ? ?? ?scanf("%d",&inputId); ?? ? do{ ?? ? ? if(find1(student,inputId)){ ?? ? ? ? printf("學號有重復,請重新輸入\n"); ?? ? ? ? scanf("%d",&inputId); ?? ? ? ?} ?? ? ? else ?? ? ? ?{ ?? ? ? ? student[x].id=inputId; ?? ? ? ? break; ?? ? ? ?} ?? ? ?}while(1); } ? // 根據(jù)學號 返回下標? int findIndex (struct Student student[],int id){ ?? ?int temp; ?? ?for(int i=0;i<count;i++){ ?? ??? ?if(student[i].id==id){ ?? ??? ??? ?temp=i; ?? ??? ??? ?break;? ?? ??? ?} else { ?? ??? ??? ?temp = -1;? ?? ??? ?}? ?? ?} ?? ? ?? ?return temp; } //按總分排序 ?void sortTotal(){ ??? ?//冒泡排序? ?? ?struct Student temp;// 元素與元素交換的臨時容器? ?? ?for (int i = 0; i < count - 1; i++){ ?? ??? ?for (int j = 0; j < count - 1 - i; j++){ ?? ??? ??? ?if (student[j].total<student[j+1].total){ ?? ??? ??? ??? ?temp = student[j + 1]; ?? ??? ??? ??? ?student[j + 1] = student[j]; ?? ??? ??? ??? ?student[j]= temp; ?? ??? ??? ?} ?? ??? ?} ?? ?} ?? ?printf("排序完成"); ?? ?showAllInfo();?? ?? ?} ?//按任意鍵繼續(xù)? ?void con(){ ??? ?printf("\n按任意鍵繼續(xù)\n");? ??? ?getch(); ?};? ?//展示學生總個數(shù)? ?void showCount(){ ??? ?system("cls");? ??? ?printf("\n\t學生總個數(shù)為:%d個\n",count);? ?? ?con();? ?}? ?//初始化數(shù)據(jù)? ?void initData(){ ??? ?FILE * fp = NULL; ??? ?fp = fopen("stu.txt", "r"); ??? ?if (!fp){ ??? ??? ?printf("文件打開失敗\n"); ??? ??? ?exit(0);// 退出程序? ??? ?} ??? ?while (1){ ? ?//讀取數(shù)據(jù) 賦值給數(shù)組? ??? ??? ?fscanf(fp, "%d%s%d%d%d%d", &student[count].id, student[count].name, &student[count]._chinese, &student[count]._math, &student[count]._english, &student[count].total); ??? ??? ?if (feof(fp)){ //文件末尾 跳出循環(huán)? ??? ??? ??? ?break; ??? ??? ?} ??? ??? ?count++; ??? ?} ?} ?//數(shù)據(jù)寫入文件中? ?void writeData(){ ?? ?FILE * fp = NULL; ?? ?fp = fopen("stu.txt", "w"); ?? ?for (int i = 0; i < count; i++){ ?? ??? ?fprintf(fp, "%d\t%s\t%d\t%d\t%d\t%d\n", student[i].id, student[i].name, student[i]._chinese, student[i]._math, student[i]._english,student[i].total); ?? ?} ?? ?printf("數(shù)據(jù)保存成功\n"); ?? ? ?} ? ? // 展示所有信息 void showAllInfo(){ ?? ?system("cls");//清屏? ?? ?for(int i=0;i<count;i++){ ?? ??? ?showInfo(student[i]);?? ?? ?? ?}? ?? ?con();? }? // 展示學生信息 void showInfo(struct Student stu){//傳入數(shù)組里的元素? ?? ?printf("學號:%d\t姓名:%s\t語文:%d\t數(shù)學:%d\t英語:%d\t總分:%d",stu.id,stu.name,stu._chinese,stu._math,stu._english,stu.total); ?? ?printf("\n-----------------分割線-----------------------\n");? }?
主界面
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C++深入探究哈希表如何封裝出unordered_set和unordered_map
哈希表是一種根據(jù)關(guān)鍵碼去尋找值的數(shù)據(jù)映射結(jié)構(gòu),該結(jié)構(gòu)通過把關(guān)鍵碼映射的位置去尋找存放值的地方,說起來可能感覺有點復雜,我想我舉個例子你就會明白了,最典型的的例子就是字典2022-06-06C/C++自主分配出現(xiàn)double free or corruption問題解決
這篇文章主要為大家介紹了C/C++出現(xiàn)double free or corruption問題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-04-04