新聞中心
小編給大家分享一下C語言實(shí)現(xiàn)學(xué)生成績管理系統(tǒng)的示例分析,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
在唐山等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè) 網(wǎng)站設(shè)計(jì)制作按需規(guī)劃網(wǎng)站,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),成都品牌網(wǎng)站建設(shè),網(wǎng)絡(luò)營銷推廣,成都外貿(mào)網(wǎng)站建設(shè)公司,唐山網(wǎng)站建設(shè)費(fèi)用合理。
學(xué)生成績管理系統(tǒng)實(shí)現(xiàn)的功能有:成績錄入、學(xué)生成績查詢、刪除、修改、通過文件保存等。
開發(fā)這樣一個系統(tǒng)需要具備的知識:線性表(鏈表)、文件操作、排序(如果需要成績排序)。
開發(fā)環(huán)境為VS2015;在Linux下沒有conio.h的頭文件,需要修改與getch()函數(shù)相關(guān)的代碼。
#include#include #include #include /*學(xué)生信息結(jié)構(gòu)體*/ typedef struct Node { char Name[10]; //學(xué)生姓名 char ID[15]; //學(xué)生學(xué)號 int Score[3]; //三科成績(數(shù)學(xué)、英語、數(shù)據(jù)結(jié)構(gòu)) float Ave_Sco; struct Node *next; }Lnode; void Display(); /*界面顯示函數(shù)*/ void GetScore(Lnode *&h); /*成績錄入函數(shù)*/ void PrintScore(Lnode *h); /*成績打印函數(shù)*/ void ModifyScore(Lnode *h); /*成績修改函數(shù)*/ void FindInf(Lnode *h); /*查找信息*/ void Delete(Lnode *h); /*刪除函數(shù)*/ void Quit(Lnode *h); /*退出函數(shù)*/ void SaveInf(Lnode *h); void LoadInf(Lnode *h); /*初始化鏈表*/ void InitList(Lnode *&head) { head = (Lnode *)malloc(sizeof(Lnode)); if (head == NULL) { printf("error!"); exit(1); } head->next = NULL; //使頭節(jié)點(diǎn)指針域?yàn)榭? } int main() { Lnode *ScoreList; //建立成績鏈表,所有學(xué)生信息存放在此鏈表 int Function; char flag; int t = 0; InitList(ScoreList); LoadInf(ScoreList); while (1) { Display(); printf("請選擇操作: "); scanf("%d", &Function); switch (Function) { case 1: while (1) { GetScore(ScoreList); printf("是否繼續(xù)輸入 (Y/N)"); scanf("%s", &flag); if (flag == 'N' || flag == 'n')break; } system("cls"); break; case 2: PrintScore(ScoreList); _getch(); system("cls"); break; case 3: ModifyScore(ScoreList); system("cls"); break; case 4: FindInf(ScoreList); _getch(); system("cls"); break; case 5: Delete(ScoreList); _getch(); system("cls"); break; case 6: Quit(ScoreList); break; default: printf("Error?。?! 請重新輸入:"); break; } //switch結(jié)束 } return 0; } /*系統(tǒng)界面顯示*/ void Display() { printf("\t\t**********************************************\n"); printf("\t\t*************歡迎使用成績管理系統(tǒng)*************\n"); printf("\t\t**********************************************\n"); printf("\t\t\t\t1、錄入成績\n"); printf("\t\t\t\t2、打印成績\n"); printf("\t\t\t\t3、修改成績\n"); printf("\t\t\t\t4、查找學(xué)生信息\n"); printf("\t\t\t\t5、刪除學(xué)生信息\n"); printf("\t\t\t\t6、退出系統(tǒng)\n"); printf("\n\n\n\n\n\n"); } /*成績錄入*/ void GetScore(Lnode *&h) { Lnode *p, *q = h; char name[10], id[15]; int Math, English, Datastruct; p = (Lnode *)malloc(sizeof(Lnode)); //為學(xué)生信息申請節(jié)點(diǎn) printf("請依次輸入學(xué)生信息:\n"); printf("姓名 學(xué)號 數(shù)學(xué) 英語 數(shù)據(jù)結(jié)構(gòu)\n"); scanf("%s %s %d %d %d", &name, &id, &Math, &English, &Datastruct); for (; q->next != NULL; q = q->next){;} //移動到尾節(jié)點(diǎn) strcpy(p->Name, name); strcpy(p->ID, id); p->Score[0] = Math; p->Score[1] = English; p->Score[2] = Datastruct; p->Ave_Sco = ((float)((p->Score[0] + p->Score[1] + p->Score[2]) - 150)) / 30; p->next = NULL; q->next = p; q = p; } /*成績打印*/ void PrintScore(Lnode *h) { Lnode *p = h->next; printf("%-14s%-8s%-8s%-8s%-8s%-8s\n","排名", "學(xué)號", "姓名", "數(shù)學(xué)", "英語", "數(shù)據(jù)結(jié)構(gòu)", "平均績點(diǎn)"); while (p != NULL) { printf("%-14s%-8s%-8d%-8d%-8d%.2f\n", p->ID, p->Name, p->Score[0], p->Score[1], p->Score[2], p->Ave_Sco); p = p->next; } } /*成績修改*/ void ModifyScore(Lnode *h) { Lnode *p = h->next; char name[10], id[15]; int Math, English, Datastruct; printf("請輸入學(xué)生姓名:"); scanf("%s", name); printf("請輸入學(xué)生學(xué)號:"); scanf("%s", id); while (p) { if (strcmp(p->Name, name)==0 && strcmp(p->ID, id)==0) { printf("當(dāng)前學(xué)生信息:\n"); printf("%-14s%-8s%-8s%-8s%-8s\n", "學(xué)號", "姓名", "數(shù)學(xué)", "英語", "數(shù)據(jù)結(jié)構(gòu)"); printf("%-14s%-8s%-8d%-8d%-8d\n", p->ID, p->Name, p->Score[0], p->Score[1], p->Score[2]); printf("請輸入更正后的數(shù)學(xué)成績:"); scanf("%d", &Math); printf("請輸入更正后的英語成績:"); scanf("%d", &English); printf("請輸入更正后的數(shù)據(jù)結(jié)構(gòu)成績:"); scanf("%d", &Datastruct); p->Score[0] = Math; p->Score[1] = English; p->Score[2] = Datastruct; break; } else { p = p->next; } }//while循環(huán)結(jié)束 } /*信息查找*/ void FindInf(Lnode *h) { Lnode *p = h->next; char name[10], id[15]; printf("請輸入學(xué)生姓名:"); scanf("%s", name); printf("請輸入學(xué)生學(xué)號:"); scanf("%s", id); while (p) { if (strcmp(p->Name, name) == 0 && strcmp(p->ID, id) == 0) { printf("當(dāng)前學(xué)生信息:\n"); printf("%-14s%-8s%-8s%-8s%-8s\n", "學(xué)號", "姓名", "數(shù)學(xué)", "英語", "數(shù)據(jù)結(jié)構(gòu)"); printf("%-14s%-8s%-8d%-8d%-8d\n", p->ID, p->Name, p->Score[0], p->Score[1], p->Score[2]); break; } else { p = p->next; } }//while循環(huán)結(jié)束 } /*刪除*/ void Delete(Lnode *h) { Lnode *p = h, *q; q = p->next; char name[10], id[15]; printf("請輸入學(xué)生姓名:"); scanf("%s", name); printf("請輸入學(xué)生學(xué)號:"); scanf("%s", id); while (q) { if (strcmp(q->Name, name) == 0 && strcmp(q->ID, id) == 0) { p->next = q->next; free(q); //刪除p節(jié)點(diǎn) printf("刪除成功\n"); break; } else { p = p->next; q = q->next; } }//while循環(huán)結(jié)束 } /*退出系統(tǒng)*/ void Quit(Lnode *h) { SaveInf(h); //退出時(shí)保存信息 exit(0); } /*打開文件*/ void LoadInf(Lnode *h) { Lnode *p = h; Lnode *q; //臨時(shí)變量 用于保存從文件中讀取的信息 FILE* file = fopen("./Information.dat", "rb"); if (!file) { printf("文件打開失?。?); return ; } /* 使用feof判斷文件是否為結(jié)束要注意的問題: 當(dāng)讀取文件結(jié)束時(shí),feof函數(shù)不會立即設(shè)置標(biāo)志符為-1,而是 需要再讀取一次后,才會設(shè)置。所以要先讀一次。 */ q = (Lnode *)malloc(sizeof(Lnode)); fread(q, sizeof(Lnode), 1, file); while (!feof(file)) //一直讀到文件末尾 { p->next = q; p = q; q = (Lnode *)malloc(sizeof(Lnode)); fread(q, sizeof(Lnode), 1, file); } //while循環(huán)結(jié)束 p->next = NULL; fclose(file); } /*保存信息到文件中*/ void SaveInf(Lnode *h) { Lnode *p = h->next; int flag; FILE* file = fopen("./Information.dat", "wb"); if (!file) { printf("文件打開失??!"); return; } while (p != NULL) { flag = fwrite(p, sizeof(Lnode), 1, file); //將p的內(nèi)容寫到文件中 if (flag != 1) { break; } p = p->next; } fclose(file); }
雖然是很簡單的小項(xiàng)目,還是有很多問題。
一:鏈表相關(guān)
在寫成績錄入和成績打印功能時(shí),發(fā)現(xiàn)始終只能保存(沒加入文件保存)最后一個數(shù)據(jù),確定鏈表的相關(guān)操作沒有問題,仔細(xì)判斷邏輯關(guān)系后,發(fā)現(xiàn)是每次在頭節(jié)點(diǎn)傳到GetScore()函數(shù),為新節(jié)點(diǎn)申請內(nèi)存后,直接將數(shù)據(jù)保存在了新申請的節(jié)點(diǎn)里面,沒有將鏈表移動到尾節(jié)點(diǎn),導(dǎo)致每次錄入成績,都會覆蓋前一次輸入的數(shù)據(jù)。解決辦法是鏈表傳到函數(shù)后,先移動到最后一個節(jié)點(diǎn),將新申請的節(jié)點(diǎn)掛接在最后一個節(jié)點(diǎn)之后。
/*成績錄入*/ void GetScore(Lnode *&h) { Lnode *p, *q = h; char name[10], id[15]; int Math, English, Datastruct; p = (Lnode *)malloc(sizeof(Lnode)); //為學(xué)生信息申請節(jié)點(diǎn) printf("請依次輸入學(xué)生信息:\n"); printf("姓名 學(xué)號 數(shù)學(xué) 英語 數(shù)據(jù)結(jié)構(gòu)\n"); scanf("%s %s %d %d %d", &name, &id, &Math, &English, &Datastruct); for (; q->next != NULL; q = q->next){;} //移動到尾節(jié)點(diǎn) //保存數(shù)據(jù) strcpy(p->Name, name); strcpy(p->ID, id); p->Score[0] = Math; p->Score[1] = English; p->Score[2] = Datastruct; p->Ave_Sco = ((float)((p->Score[0] + p->Score[1] + p->Score[2]) - 150)) / 30; //始終指向最后一個節(jié)點(diǎn) p->next = NULL; q->next = p; q = p; }
二、文件操作
用文件保存遇到的問題主要是每次打印數(shù)據(jù)時(shí)除正常數(shù)據(jù)外,始終多一行亂碼。判斷方法是while(!feof(file))。排除錯誤時(shí)確定了兩種可能性:多保存了一行;多讀取了一行。經(jīng)過某度feof()與EOF的關(guān)系后,確定是多讀取了一行數(shù)據(jù)。
用feof()函數(shù)進(jìn)行文件尾判斷時(shí),當(dāng)文件已經(jīng)到達(dá)尾部后,還需要在讀取一次后,feof()函數(shù)才會返回-1,所以會出現(xiàn)多讀一次的情況;解決辦法時(shí),在循環(huán)讀取之前先將第一個數(shù)據(jù)讀取出來,然后在正常讀取。即注意多讀一次的問題。
/*打開文件*/ void LoadInf(Lnode *h) { Lnode *p = h; Lnode *q; //臨時(shí)變量 用于保存從文件中讀取的信息 FILE* file = fopen("./Information.dat", "rb"); if (!file) { printf("文件打開失??!"); return ; } /* 使用feof判斷文件是否為結(jié)束要注意的問題: 當(dāng)讀取文件結(jié)束時(shí),feof函數(shù)不會立即設(shè)置標(biāo)志符為-1,而是 需要再讀取一次后,才會設(shè)置。所以要先讀一次。 */ q = (Lnode *)malloc(sizeof(Lnode)); fread(q, sizeof(Lnode), 1, file); while (!feof(file)) //一直讀到文件末尾 { p->next = q; p = q; q = (Lnode *)malloc(sizeof(Lnode)); fread(q, sizeof(Lnode), 1, file); } //while循環(huán)結(jié)束 p->next = NULL; fclose(file); }
看完了這篇文章,相信你對“C語言實(shí)現(xiàn)學(xué)生成績管理系統(tǒng)的示例分析”有了一定的了解,如果想了解更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
文章題目:C語言實(shí)現(xiàn)學(xué)生成績管理系統(tǒng)的示例分析
路徑分享:http://www.dlmjj.cn/article/jichis.html