您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關C語言如何實現簡單學生管理系統的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
花了一個月的時間,學習了鏈表,文件,多文件編程,然后就開始寫學生管理系統,比較簡單的那種,實現了增,添,改,查,多種排序,輸入的時候的限定,成績分析的功能。遇到的問題很多,也嘗試解決了,下面寫點寫學生管理系統的要注意的地方,希望對別人有幫助。
1.一定要先寫好主函數的框架,然后再往上面加東西,這樣說有點籠統,我把我的主函數在下面,可以借鑒下哈。我的主函數是一個while(1)的無限循環,里面一個switch選擇,利用一個菜單函數,讓其返回值當成switch選擇的值,然后進入不同的功能主函數,其他功能主函數和主函數的套路一樣。
int main(int argc, char *argv[]) { login(); //這是個的介紹自己的函數 Sleep(1000); //睡眠函數 while(1) { switch(menu()) //主菜單選擇函數 { case 1: system("CLS"); //清屏函數 printf("\t\t________學生成績添加_______"); head=luru(); cr(head); int choice; scanf("%d",&choice); if(choice==0) break; case 2: system("CLS"); //printf("\t\t_________學生成績排序和刪除___\d"); paxu(); int end; scanf("%d",&end); if(end==0) break; case 3: system("CLS"); printf("\t\t__________成績的修改__________"); xg(); int a; scanf("%d",&a); if(a==0) break; case 4: system("CLS"); printf("\t\t___________學生信息分析_______"); fx(); int b; scanf("%d",&b); if(b==0) break; case 5: system("CLS"); printf("\t\t___________謝謝使用哈_______\n"); Sleep(1000); exit(-1); break; } system("PAUSE"); } return 0; }
2.輸入模塊一定要注意輸入的格式,我采用的尾插法,每次處理好一個節點后就讓用戶判定是否繼續輸入,在格式控制方面,我當用戶輸入一次,就判定它是否符號規范,如果不規范,就重新輸入。最后存入文件中。
3.一定要把函數寫的利用率高一點,這樣就可以減少代碼的重復書寫,這個全憑自己的寫代碼的風格了,我自己在這點也沒有做太好,反省中,以前剛開始學c的時候有人說c語言的主體是函數,不太懂,等寫完這個,感覺說的真的很對,c語言的函數寫好后,在該用的時候直接引用,很方便。
完整代碼
主函數
main.c
#include <stdio.h> #include <stdlib.h> #include "student.h" #include <string.h> /* run this program using the console pauser or add your own getch, system("pause") or input loop */ int main(int argc, char *argv[]) { login(); //struct student *head; Sleep(1000); while(1) { switch(menu()) { case 1: system("CLS"); printf("\t\t________學生成績添加_______"); head=luru(); cr(head); int choice; scanf("%d",&choice); if(choice==0) break; case 2: system("CLS"); //printf("\t\t_________學生成績排序和刪除___\d"); paxu(); int end; scanf("%d",&end); if(end==0) break; case 3: system("CLS"); printf("\t\t__________成績的修改__________"); xg(); int a; scanf("%d",&a); if(a==0) break; case 4: system("CLS"); printf("\t\t___________學生信息分析_______"); fx(); int b; scanf("%d",&b); if(b==0) break; case 5: system("CLS"); printf("\t\t___________謝謝使用哈_______\n"); Sleep(1000); exit(-1); break; } system("PAUSE"); } return 0; }
函數聲明
student.h
#include <stdio.h> struct student *temp; struct student *head; struct student{ int num; char name[6]; int yw; int sx; int yy; struct student*next; }; int menu(); void login(); struct student* luru(); void cr(struct student *h); struct student*cc(); int menu1(); void paxu(); void px1(); void px2(); void px3(); void px4(); void sanch(); void tj(); void xg(); void fx();
函數定義
student.c
#include <stdio.h> #include "student.h" #include <stdlib.h> #include <string.h> int menu() { char n; do{ system("cls"); printf("\t\t\t|-------------------------------------------------|\n"); printf("\t\t\t| *****學生管理系統***** |\n"); printf("\t\t\t|-------------------------------------------------|\n"); printf("\t\t\t| 1.學生成績錄入 |\n"); printf("\t\t\t| 2.學生成績排序和刪除 |\n"); printf("\t\t\t| 3.成績的修改 |\n"); printf("\t\t\t| 4.學生成績分析 |\n"); printf("\t\t\t| 5.退出程序 |\n"); printf("\t\t\t---------------------------------------------------\n"); printf("請選擇1-5:"); n=getch(); }while(n<'0'||n>'5'); return(n-48); } void login() { printf("\n\n\n\t\t\t 學生信息管理系統\n\n"); printf("\t\t\t 版本號:0.2\n\n"); printf("\n\n\n\n\t\t\t 2017年5月10日\n\n"); printf("\n\n\t\t\t sakurakid\n"); } struct student *luru() { struct student*rhead ,*r,*t,*stu; rhead=(struct student*)malloc(sizeof(struct student)); t=rhead; rhead->next=NULL; char xx; int flag=1; printf("\t\t_____________________________\n"); printf("\t\t| 學生管理系統 |\n"); printf("\t\t_____________________________\n"); printf("\t\t| |\n"); printf("\t\t| 輸入1添加 |\n"); printf("\t\t| 輸入0退出 |\n"); printf("\t\t|___________________________|\n"); while(xx!='1'&&xx!='0') { xx=getch(); } if(xx=='1') { system("CLS"); do{ system("CLS"); stu=(struct student*)malloc(sizeof(struct student)); printf("\t\t_____________________________\n"); printf("\t\t| 學生管理系統 |\n"); printf("\t\t|___________________________|\n"); printf("\t\t| |\n"); printf("\t\t| 輸入1添加 |\n"); printf("\t\t| 輸入0退出 |\n"); printf("\t\t|___________________________|\n"); printf("\t\t學號: ");scanf("%d",&stu->num); if(stu->num > 99999999||stu->num < 10000000) { printf("\t\t______________________\n"); printf("\t\t_請輸入8位非負數__\n"); printf("\t\t______________________\n"); printf("\t\t重新輸入學號: ");scanf("%d",&stu->num); } printf("\t\t姓名: ");scanf("%s",stu->name); if(stu->name[0] > 0) { printf("\t\t______________________\n"); printf("\t\t______姓名請輸入漢字__\n"); printf("\t\t______________________\n"); printf("\t\t重新輸入姓名: ");scanf("%s",stu->name); } printf("\t\t語文: ");scanf("%d",&stu->yw); printf("\t\t數學: ");scanf("%d",&stu->sx); printf("\t\t英語: ");scanf("%d",&stu->yy); if(stu->sx<0||stu->sx>100||stu->yw<0||stu->yw>100||stu->yy<0||stu->yy>100) { printf("\t\t______________________\n"); printf("\t\t_成績請輸入0-100之間__\n"); printf("\t\t______________________\n"); printf("\t\t重新輸入成績\n"); printf("\t\t語文: ");scanf("%d",&stu->yw); printf("\t\t數學: ");scanf("%d",&stu->sx); printf("\t\t英語: ");scanf("%d",&stu->yy); } t->next=stu; t=stu; xx = getch(); printf("繼續輸入請按1,退出請按0:"); while(xx!='1'&&xx!='0') { xx=getch(); } }while(xx=='1'); t->next=NULL; } return (rhead); } void cr(struct student *h) { struct student *stu; FILE*fp; if((fp=fopen("學生信息.txt","wt"))==NULL) { printf("文件出錯233,按任意鍵退出!"); getch(); exit(1); } for(stu=h->next;stu!=NULL;stu=stu->next) { fprintf(fp,"%d %s %d %d %d\n",stu->num,stu->name,stu->yw,stu->sx,stu->yy); } fclose(fp); } struct student*cc() { struct student*ahead,*r,*stu; FILE*f1; f1=fopen("學生信息.txt","rt"); ahead=(struct student*)malloc(sizeof(struct student)); ahead->next=NULL; r=ahead; while(!feof(f1)) { stu=(struct student*)malloc(sizeof(struct student)); fscanf(f1,"%d %s %d %d %d\n",&stu->num,stu->name,&stu->yw,&stu->sx,&stu->yy); r->next=stu; r=stu; } r->next=NULL; fclose(f1); return ahead; } int menu1() { system("CLS"); char n; do{ system("CLS"); printf("\t\t__________________________________________________\n"); printf("\t\t| |\n"); printf("\t\t| 1.順序排序 2.按英語排序 3.按語文排序 |\n"); printf("\t\t| 4.按數學排序 5.刪除學生 6.添加學生 |\n"); printf("\t\t| |\n"); printf("\t\t|______________按0退出___________________________|\n"); printf("\t\t請選擇0-6:"); n=getch(); }while(n<'0'||n>'6'); return (n-48); } void paxu() { system("CLS"); char n; do{ switch(menu1()) { case 1: px1(); break; case 2: system("CLS"); px2(); break; case 3: system("CLS"); px3(); break; case 4: system("CLS"); px4(); break; case 5: system("CLS"); sanch(); break; case 6: system("CLS"); tj(); break; } printf("繼續輸入請按1,退出請按0:"); n=getch(); while(n!='1'&&n!='0') { n=getch(); } }while(n=='1'); if(n=='0') return; } void px1() { struct student *t,*h; h=cc(); printf("\n"); printf("\t\t%10s%10s%10s%10s%10s\n","學號","姓名","語文","數學","英語","平均分","總分"); printf("\t\t--------------------------------------------------------------\n"); for(t=h->next;t!=NULL;t=t->next) { printf("\t\t%10d%10s%10d%10d%10d%\n",t->num,t->name,t->yw,t->sx,t->yy); } } void px2() { struct student *p,*q,*head,*t; head=cc(); int tnum; char tname[6]; int tyw; int tsx; int tyy; for(p = head->next;p->next!=NULL;p=p->next){ for(q=p->next;q!=NULL;q=q->next){ if(p->yy < q->yy) { tnum=p->num; p->num=q->num; q->num=tnum; strcpy(tname,p->name); strcpy(p->name,q->name); strcpy(q->name,tname); tyw=p->yw; p->yw=q->yw; q->yw=tyw; tsx=p->sx; p->sx=q->sx; q->sx=tsx; tyy=p->yy; p->yy=q->yy; q->yy=tyy; } } } printf("\t\t 英語排序 \n"); printf("\t\t%10s%10s%10s%10s%10s\n","學號","姓名","語文","數學","英語","平均分","總分"); printf("\t\t--------------------------------------------------------------\n"); for(t=head->next;t!=NULL;t=t->next) { printf("\t\t%10d%10s%10d%10d%10d%\n",t->num,t->name,t->yw,t->sx,t->yy); } } void px3() { struct student *p,*q,*head,*t; head=cc(); int tnum; char tname[6]; int tyw; int tsx; int tyy; for(p = head->next;p->next!=NULL;p=p->next){ for(q=p->next;q!=NULL;q=q->next){ if(p->yw < q->yw) { tnum=p->num; p->num=q->num; q->num=tnum; strcpy(tname,p->name); strcpy(p->name,q->name); strcpy(q->name,tname); tyw=p->yw; p->yw=q->yw; q->yw=tyw; tsx=p->sx; p->sx=q->sx; q->sx=tsx; tyy=p->yy; p->yy=q->yy; q->yy=tyy; } } } printf("\t\t 語文排序 \n"); printf("\t\t%10s%10s%10s%10s%10s\n","學號","姓名","語文","數學","英語","平均分","總分"); printf("\t\t--------------------------------------------------------------\n"); for(t=head->next;t!=NULL;t=t->next) { printf("\t\t%10d%10s%10d%10d%10d%\n",t->num,t->name,t->yw,t->sx,t->yy); } } void px4() { struct student *p,*q,*head,*t; head=cc(); int tnum; char tname[6]; int tyw; int tsx; int tyy; for(p = head->next;p->next!=NULL;p=p->next){ for(q=p->next;q!=NULL;q=q->next){ if(p->sx < q->sx) { tnum=p->num; p->num=q->num; q->num=tnum; strcpy(tname,p->name); strcpy(p->name,q->name); strcpy(q->name,tname); tyw=p->yw; p->yw=q->yw; q->yw=tyw; tsx=p->sx; p->sx=q->sx; q->sx=tsx; tyy=p->yy; p->yy=q->yy; q->yy=tyy; } } } printf("\t\t 數學排序 \n"); printf("\t\t%10s%10s%10s%10s%10s\n","學號","姓名","語文","數學","英語","平均分","總分"); printf("\t\t--------------------------------------------------------------\n"); for(t=head->next;t!=NULL;t=t->next) { printf("\t\t%10d%10s%10d%10d%10d%\n",t->num,t->name,t->yw,t->sx,t->yy); } } void sanch() { px1(); struct student *p,*q,*phead,*t,*m; phead=cc(); int n; int flag=0; printf("\t\t____________________________________________________\n"); printf("\t\t|______________刪除學生信息_________________________|\n"); printf("\t\t|___________________________________________________|\n"); printf("\t\t請輸入要刪除的學生學號:"); scanf("%d",&n); p=phead; for(m=phead;m!=NULL;m=m->next) { if(m->num==n) { printf("\t\t已經查到改學生的信息\n"); flag=0; break; } else flag=1; } if(flag==1) printf("\t\t抱歉,沒有查到該學生的信息\n"); if(flag==0) { if(phead->num==n) { phead=phead->next; } else { while(p->num!=n && p->next!=NULL) { t=p; p=p->next; } if(p->num==n) { t->next=p->next; } } printf("\t\t該學生信息已刪除\n"); } cr(phead); } void tj() { struct student *phead,*stu; phead=cc(); printf("\t\t____________________________________________________\n"); printf("\t\t|______________添加學生信息_________________________|\n"); printf("\t\t|___________________________________________________|\n"); stu=(struct student*)malloc(sizeof(struct student)); printf("\t\t學號: ");scanf("%d",&stu->num); if(stu->num > 99999999||stu->num < 10000000) { printf("\t\t______________________\n"); printf("\t\t_請輸入8位非負數__\n"); printf("\t\t______________________\n"); printf("\t\t重新輸入學號: ");scanf("%d",&stu->num); } printf("\t\t姓名: ");scanf("%s",stu->name); if(stu->name[0] > 0) { printf("\t\t______________________\n"); printf("\t\t______姓名請輸入漢字__\n"); printf("\t\t______________________\n"); printf("\t\t重新輸入姓名: ");scanf("%s",stu->name); } printf("\t\t語文: ");scanf("%d",&stu->yw); printf("\t\t數學: ");scanf("%d",&stu->sx); printf("\t\t英語: ");scanf("%d",&stu->yy); if(stu->sx<0||stu->sx>100||stu->yw<0||stu->yw>100||stu->yy<0||stu->yy>100) { printf("\t\t______________________\n"); printf("\t\t_成績請輸入0-100之間__\n"); printf("\t\t______________________\n"); printf("\t\t重新輸入成績\n"); printf("\t\t語文: ");scanf("%d",&stu->yw); printf("\t\t數學: ");scanf("%d",&stu->sx); printf("\t\t英語: ");scanf("%d",&stu->yy); } stu->next=phead->next; phead->next=stu; printf("\t\t學生信息已存入\n"); cr(phead); } void xg() { system("CLS"); char n; do{ system("CLS"); px1(); struct student *p,*q,*phead,*t,*m; int flag=0; int timp; phead=cc(); printf("\t\t____________________________________________________\n"); printf("\t\t|______________修改學生信息_________________________|\n"); printf("\t\t|___________________________________________________|\n"); printf("\t\t請輸入要修改的學生學號:"); scanf("%d",&timp); p=phead; for(m=phead;m!=NULL;m=m->next) { if(m->num==timp) { printf("\t\t已經查到改學生的信息\n"); flag=0; break; } else flag=1; } if(flag==1) printf("\t\t抱歉,沒有查到該學生的信息\n"); if(flag==0) { printf("\t\t該學生的原信息如下\n"); printf("\t\t學號:%d\n",m->num); printf("\t\t姓名:%s\n",m->name); printf("\t\t語文:%d\n",m->yw); printf("\t\t數學:%d\n",m->sx); printf("\t\t英語:%d\n\n",m->yy); printf("\t\t請重新輸入該學生的信息\n"); printf("\t\t學號: ");scanf("%d",&m->num); if(m->num > 99999999||m->num < 10000000) { printf("\t\t______________________\n"); printf("\t\t_請輸入8位非負數__\n"); printf("\t\t______________________\n"); printf("\t\t重新輸入學號: ");scanf("%d",&m->num); } printf("\t\t姓名: ");scanf("%s",m->name); if(m->name[0] > 0) { printf("\t\t______________________\n"); printf("\t\t______姓名請輸入漢字__\n"); printf("\t\t______________________\n"); printf("\t\t重新輸入姓名: ");scanf("%s",m->name); } printf("\t\t語文: ");scanf("%d",&m->yw); printf("\t\t數學: ");scanf("%d",&m->sx); printf("\t\t英語: ");scanf("%d",&m->yy); if(m->sx<0||m->sx>100||m->yw<0||m->yw>100||m->yy<0||m->yy>100) { printf("\t\t______________________\n"); printf("\t\t_成績請輸入0-100之間__\n"); printf("\t\t______________________\n"); printf("\t\t重新輸入成績\n"); printf("\t\t語文: ");scanf("%d",&m->yw); printf("\t\t數學: ");scanf("%d",&m->sx); printf("\t\t英語: ");scanf("%d",&m->yy); } printf("\t\t該學生信息已修改\n"); cr(phead); } printf("繼續修改請按1,退出請按0:"); n=getch(); while(n!='1'&&n!='0') { n=getch(); } }while(n=='1'); if(n=='0') return; } void fx() { struct student *p,*q,*phead,*t,*m; int flag=0; int jyw=0; int jsx=0; int jyy=0; int timp; int n=0; phead=cc(); p=phead; for(t=p->next;t!=NULL;t=t->next) { n++; if(t->sx>=60) jsx++; if(t->yw>=60) jyw++; if(t->yy>=60) jyy++; } printf("\t\t____________________________________________________\n"); printf("\t\t|______________學生成績分析_________________________|\n"); printf("\t\t|___________________________________________________|\n"); printf("\t\t一共錄入了%d個學生成績\n",n); printf("\t\t語文及格人數為%d\n",jyw); printf("\t\t數學及格人數為%d\n",jsx); printf("\t\t英語及格人數為%d\n",jyy); printf("\t\t分析完畢\n"); printf("退出請按0:"); }
感謝各位的閱讀!關于“C語言如何實現簡單學生管理系統”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。