1. 程式人生 > >學生成績管理系統(C語言)

學生成績管理系統(C語言)

//////////////////////////////////////////////////////////////// 工程名稱:學生成績管理系統// 工程檔案:C語言課程設計// 作 者:張誠華// 日期:2016-06-18//////////////////////////////////////////////////////////////#include#include#include#include#define LEN sizeof(struct student)#define FORMAT " %2s %2s %s %s %4.1lf %4.1lf %4.1lf %4.1lf %4.2lf \n"#define DATA stu[i].Term,stu[i].Class,stu[i].name,stu[i].num,stu[i].score[0],\stu[i].score[1],stu[i].score[2],stu[i].sum,stu[i].avechar Name[15]; //使用者名稱儲存char Pass[15]; //使用者密碼儲存#define R 3struct student/*定義學生成績結構體*/{ char Class[15]; //專業班級 char Term[5]; // 學期char num[15]; /*學號*/char name[15]; /*姓名*/double score[3]; //學科成績double sum; /*總分*/double ave; /*平均分*/int mingci[50]; /*名次*/};struct student stu[300]; /*定義結構體陣列*//******使用者輸入使用者名稱與登陸密碼*******/struct NamePass {char name[15];char pass[15];};/**********密碼登陸模組************************/void Pass_File(); //建立密碼檔案int Name_Password(); //登陸密碼驗證void User_Amadeus();/* 密碼修改 *//***********************************************/void in();/*錄入學生成績資訊*/void show();/*顯示學生成績資訊*/void order();/*按總分排序*/void del();/*刪除學生成績資訊*/void modify();/*修改學生成績資訊*/void menu();/*主選單*/void insert();/*插入學生成績資訊*/void total();/*計算總人數*/void search();/*查詢學生資訊*/void ShowClassS(); // 顯示某班學生成績資訊void ShowFailS(); // 顯示不及格學生成績資訊/******************************************************************************************************************* 程式的開始部分********************************************************************************************************************/void main()/*主函式*/{ int n; Pass_File(); Name_Password();system("pause");system("cls");printf("\t\t******************************************************************\n");printf("\t\t* *\n");printf("\t\t* 歡迎使用由計算機1505班設計的學生成績管理系統 *\n");printf("\t\t* 祝您使用愉快!!!! *\n"); printf("\t\t* *\n"); printf("\t\t* 心靈雞湯: *\n");printf("\t\t* *\n"); printf("\t\t* 相信自己,依靠自己,要儘自己的能力,把每件事情都做得更好,*\n");printf("\t\t* 讓自己活的每一天都有意義。 *\n");printf("\t\t* 努力成為你最喜歡的那種人,就算不成功, *\n"); printf("\t\t* 至少你會喜歡這樣努力的自己。 *\n");printf("\t\t* *\n"); printf("\t\t* 指導老師 :毋小省 *\n");printf("\t\t* 課程設計 :C語言 *\n");printf("\t\t* 姓 名 :張誠華 *\n");printf("\t\t* 學 校 :河南理工大學 *\n");printf("\t\t* 學 號 :保密 *\n"); printf("\t\t* 專業班級 :計算機1505 *\n");printf("\t\t* *\n");printf("\t\t******************************************************************\n"); system("pause");//暫停 system("cls"); menu();scanf("%d",&n);/*輸入選擇功能的編號*/ while(n){ switch(n){case 1: in();break;case 2: search();break;case 3: del();break;case 4:modify();break;case 5:insert();break;case 6:order();break;case 7:total();break;case 8: User_Amadeus();break;case 9:ShowClassS();break;case 10:show(); break;case 11: ShowFailS();break;default:break;}getch();menu();/*執行完功能再次顯示選單介面*/scanf("%d",&n);}}/****************建立密碼檔案*************/void Pass_File(){FILE * PassFile;char s[8]="amadeus";PassFile = fopen("E:\\張誠華的課程設計(請將檔案存放在E盤)\\password.bat", "ab+");if(NULL == PassFile){printf("建立密碼檔案失敗!\n");exit(0);}fprintf(PassFile ,"%s\n",s);fwrite("amadeus" , sizeof("amadeus"), 1, PassFile);fclose(PassFile);}/******************登 陸 密 碼 驗 證 ***********************/int Name_Password(){struct NamePass PassW1;int num = 0;FILE * PassFile;PassFile = fopen("E:\\張誠華的課程設計(請將檔案存放在E盤)\\password.bat", "rb");//以只讀的方式開啟二進位制檔案password.batif(NULL == PassFile){Pass_File();}fscanf(PassFile, "%s", Name );//設定初始使用者名稱fscanf(PassFile, "%s", Pass);//設定初始密碼while(1){system("cls");printf("\n\n\t\t\t 歡迎使用學生成績管理系統\n\n");printf("\t\t\t\t學號是311509010516,密碼是010516\n\n\n");printf(" 請輸入學號: ");scanf("%s", &PassW1.name);printf("\n");printf(" 請輸入密碼: ");scanf("%s", &PassW1.pass);if(0 == strcmp(Name,PassW1.name) && 0 == strcmp(Pass,PassW1.pass)){ printf("\n\n\n\t\t\t密 碼 核 對 正 確 ! \1 \n"); printf("\n\n\n\t\t 歡 迎 您 使 用 學 生 管 理 系 統 ! \2\n");break;}else{printf("\n\n\t\t\t對不起,您輸入的使用者名稱或密碼有誤!\n\n");printf("\n\n\t\t 請 按 任 意 鍵 重 新 輸 入 !\n\n\n\n\n\n\n\n\n");getch();num++;if(3 == num){ system("cls");printf("\n\n\t\t\t 歡迎使用學生成績管理系統\n\n\n");printf("\n\n\n\t\t 對 不 起\n\n\n");printf("\n\t\t\t 你 已 經 三 次 輸 入 錯 誤\n\n");printf("\n\t\t\t\t\t\t\t再 見 ! \n\n\n\n\n\n");getch();fclose(PassFile);return 0;}}}printf("\n\n\n");fclose(PassFile);return 1;}/*******************密碼修改****************/void User_Amadeus(){FILE * PassFile;int a = 1;char name1[15];char passw1[15];char passw2[15];char passw3[15];system("cls");PassFile = fopen("E:\\張誠華的課程設計(請將檔案存放在E盤)\\password.bat", "wb");if(NULL == PassFile){printf("修改密碼檔案失敗!\n");exit(0);} printf("\t\t\t\t 用 戶 修 改 \n");while(1){printf("請輸入原密碼:"); scanf("%s", passw1);if(0 == strcmp(Pass, passw1)){printf("\n請輸入新使用者名稱:");scanf("%s", name1);while(1){while(1){printf("\n請輸入新密碼:");scanf("%s", passw2);if(0 == strcmp(Pass, passw2)){printf("\n新密碼不能與舊密碼相同!\n");}elsebreak;}printf("\n請確認新密碼:");scanf("%s", passw3);if(0 == strcmp(passw2, passw3)){strcpy(Pass , passw2);printf("\n\n\t\t\t密 碼 修 改 成 功 \1 請 重 新 登 陸 !\n\n");fprintf(PassFile, "%s\r\n", name1);fprintf(PassFile, "%s\r\n", passw2);break;}elseprintf("\n您輸入的密碼不一致, 請重新輸入!\n");}break;}else{if(a != 3){printf("\n密碼錯誤,請重新輸入!\n\n");a++;}else{printf("你已三次輸入密碼錯誤 !\n\n");printf("再見!\n");exit(0);}}}fclose(PassFile);system("pause");}/*********************錄入學生資訊********************/void in(){ int i,m=0;/*m是記錄的條數*/char ch[2];FILE *fp;/*定義檔案指標*/if((fp=fopen("mydata.txt","rb+"))==NULL)/*開啟指定檔案*/{ printf("can not open the file\n");return;}while(!feof(fp)) { if(fread(&stu[m] ,LEN,1,fp)==1)m++;/*統計當前記錄條數*/}fclose(fp);if(m==0) printf("No record!\n");else {system("cls");show();/*呼叫show函式,顯示原有資訊*/}if((fp=fopen("mydata.txt","wb"))==NULL){ printf("can not open the file\n");return;}printf("please input(Y/N):");scanf("%s",ch);while(strcmp(ch,"Y")==0||strcmp(ch,"y")==0)/*判斷是否要錄入新資訊*/{printf("輸入學期:");scanf("%s",stu[m].Term);printf("輸入專業班級:");scanf("%s",stu[m].Class);printf("輸入學生姓名:");scanf("%s",stu[m].name);/*輸入學生姓名*/printf("學號:");scanf("%s",stu[m].num);/*輸入學生學號*/printf("輸入工科數學成績:");scanf("%lf",&stu[m].score[0]);/*輸入工科數學成績*/printf("輸入大學英語成績:");scanf("%lf",&stu[m].score[1]);/*輸入大學英語成績*/printf("輸入C語言成績:");scanf("%lf",&stu[m].score[2]);/*輸入C語言成績*/stu[m].sum=stu[m].score[0]+stu[m].score[1]+stu[m].score[2];/*計算出總成績*/stu[m].ave=stu[m].sum/3.0;/*計算出平均分*/if(fwrite(&stu[m],LEN,1,fp)!=1)/*將新錄入的資訊寫入指定的磁碟檔案*/{printf("不能儲存!"); getch(); }else{ printf("%s 儲存成功!\n",stu[m].name);m++;}printf("是否繼續?(Y/N):");/*詢問是否繼續*/scanf("%s",ch);}fclose(fp);printf("OK!\n");}/**************************顯示學生成績*************************/void show(){FILE *fp;int i,m=0;fp=fopen("mydata.txt","rb+");while(!feof(fp)){if(fread(&stu[m] ,LEN,1,fp)==1) m++;} fclose(fp);printf(" Term PClass Name Number UMath UEnglish CLanguage Tscore Avescore\n");for(i=0;i<m;i++){ printf(FORMAT,DATA);/*將資訊按指定格式列印*/}}/*******************排序求學生成績名次函式***********************/void order(){ FILE *fp;struct student t;int i=0,j=0,m=0;if((fp=fopen("mydata.txt","rb+"))==NULL){ printf("can not open the file!\n");return;}while(!feof(fp)) if(fread(&stu[m] ,LEN,1,fp)==1) m++;fclose(fp);if(m==0) {printf("no record!\n");return;}for(i=0;i<m-1;i++)for(j=i+1;j<m;j++)/*雙重迴圈實現成績比較並交換*/if(stu[i].sum<stu[j].sum){ t=stu[i];stu[i]=stu[j];stu[j]=t;}for(i=0;i<m;i++)stu[i].mingci[i]=i+1; printf(" Term PClass Name Number UMath UEnglish CLanguage Tscore Avescore Mingci\n"); for(i=0;i<m;i++){ printf(" %2s %2s %s %s %4.1lf %4.1lf %4.1lf %4.1lf %4.2lf %d \n",\ stu[i].Term,stu[i].Class,stu[i].name,stu[i].num,stu[i].score[0],\stu[i].score[1],stu[i].score[2],stu[i].sum,stu[i].ave,stu[i].mingci[i]);/*將資訊按指定格式列印*/}if((fp=fopen("mydata.txt","a"))==NULL){ printf("can not open the file\n");return;}for(i=0;i<m;i++)/*將重新排好序的內容重新寫入指定的磁碟檔案中*/if(fwrite(&stu[i] ,LEN,1,fp)!=1){ printf("%s 沒有儲存成功!\n"); getch();}fclose(fp);}/********************刪除學生成績函式********************/void del(){FILE *fp;int i,j,m=0;char snum[15];char ch[2];if((fp=fopen("mydata.txt","rb+"))==NULL){ printf("can not open the file\n");return;}while(!feof(fp)) if(fread(&stu[m],LEN,1,fp)==1) m++;fclose(fp);if(m==0) {printf("沒有該學號!\n");return;}printf("請輸入學號:");scanf("%s",snum);for(i=0;i<m;i++)if(strcmp(snum,stu[i].num)==0)break; if(i==m){printf("沒有找到該學生的成績");getchar();return;}printf("找到該學生的成績,是否刪除?(Y/N)");scanf("%s",ch);if(strcmp(ch,"Y")==0||strcmp(ch,"y")==0)/*判斷是否要進行刪除*/{for(j=i;j<m;j++)stu[j]=stu[j+1];/*將後一個記錄移到前一個記錄的位置*/m--;/*記錄的總個數減1*/printf("刪除成功!\n");}if((fp=fopen("mydata.txt","wb"))==NULL){ printf("can not open the file\n");return;}for(j=0;j<m;j++)/*將更改後的記錄重新寫入指定的磁碟檔案中*/if(fwrite(&stu[j] ,LEN,1,fp)!=1){ printf("儲存失敗!\n");getch();}fclose(fp);}/*********************修改學生成績函式********************/void modify(){ FILE *fp;int i,j,m=0;char snum[15];if((fp=fopen("mydata.txt","rb+"))==NULL){ printf("can not open the file\n");return;}while(!feof(fp)) if(fread(&stu[m],LEN,1,fp)==1) m++;if(m==0){printf("沒有記錄!\n");fclose(fp);return;}printf("請輸入你要修改的成績的學生的學號!\n");scanf("%s",snum);for(i=0;i<m;i++)if(strcmp(snum,stu[i].num)==0)/*檢索記錄中是否有要修改的資訊*/break;if(i<m){printf("找到該學生的成績!你可以修改該成績!\n");printf("輸入工科數學成績:");scanf("%lf",&stu[m].score[0]);/*輸入工科數學成績*/ printf("輸入大學英語成績:"); scanf("%lf",&stu[m].score[1]);/*輸入大學英語成績*/ printf("輸入C語言成績:"); scanf("%lf",&stu[m].score[2]);/*輸入C語言成績*/printf("請按任意鍵繼續......");stu[m].sum=stu[m].score[0]+stu[m].score[1]+stu[m].score[2];/*計算出總成績*/stu[i].ave=stu[i].sum/3.0;}else {printf("沒有找到該學生的成績!");getchar();return;}if((fp=fopen("mydata.txt","wb"))==NULL){printf("can not open the file\n");return;}for(j=0;j<m;j++)/*將新修改的資訊寫入指定的磁碟檔案中*/if(fwrite(&stu[j] ,LEN,1,fp)!=1){ printf("儲存失敗!"); getch(); }fclose(fp);}/******************自定義函式實現選單功能*************************/void menu(){system("cls");printf("\n\n\n\n\n");printf("\t\t\t|-------------------學生成績管理系統------------|\n");printf("\t\t\t|\t\t *0 退出 |\n");printf("\t\t\t|\t\t *1 按學期、班級錄入學生成績 |\n");printf("\t\t\t|\t\t *2 查詢學生成績 |\n");printf("\t\t\t|\t\t *3 刪除學生成績 |\n");printf("\t\t\t|\t\t *4 修改學生成績 |\n");printf("\t\t\t|\t\t *5 插入學生成績 |\n");printf("\t\t\t|\t\t *6 學生成績名次 |\n");printf("\t\t\t|\t\t *7 統計儲存的學生人數 |\n"); printf("\t\t\t|\t\t *8 使用者密碼修改 |\n");printf("\t\t\t|\t\t *9 按班級輸出學生成績 |\n");printf("\t\t\t|\t\t *10 顯示學生成績資訊 |\n");printf("\t\t\t|\t\t *11 輸出不及格學生成績 |\n");printf("\t\t\t|-----------------------------------------------|\n\n");printf("\t\t\t請選擇(0-11):");}/****************插入學生成績函式*****************/void insert(){ FILE *fp;int i,j,k,m=0;char snum[15];if((fp=fopen("mydata.txt","ab+"))==NULL){ printf("can not open the file\n");return;}while(!feof(fp)) if(fread(&stu[m],LEN,1,fp)==1)m++;if(m==0) {printf("沒有記錄!\n");fclose(fp);return;}printf("請輸入你要插入的位置!(請輸入學號)\n");scanf("%s",snum);/*輸入要插入的位置*/for(i=0;ii;j--)stu[j+1]=stu[j];/*從最後一條記錄開始均向後移一位*/printf("現在請輸入新的資訊.\n");printf("學號:");scanf("%s",stu[i+1].num);for(k=0;k<m;k++)if(stu[k].num==stu[i+1].num&&k!=i+1){printf("the number is existing,press any to continue!");getch();fclose(fp);return;}printf("姓名:\n");scanf("%s",stu[i+1].name);printf("\n工科數學成績:");scanf("%lf",&stu[i+1].score[0]);printf("\n大學英語成績:");scanf("%lf",&stu[i+1].score[1]);printf("\nC語言成績:");scanf("%lf",&stu[i+1].score[2]);stu[i+1].sum=stu[i+1].score[0]+stu[i+1].score[1]+stu[i+1].score[2];/*計算出總成績*/stu[i+1].ave=stu[i+1].sum/3.0;if((fp=fopen("data","wb"))==NULL){ printf("can not open the file\n");return;}for(k=0;k<=m;k++)if(fwrite(&stu[k] ,LEN,1,fp)!=1)/*將修改後的記錄寫入磁碟檔案中*/{printf("儲存失敗!"); getch(); }fclose(fp);}/*************統計學生人數************/void total(){ FILE *fp;int m=0;if((fp=fopen("mydata.txt","rb+"))==NULL){ printf("can not open the file\n");return;}while(!feof(fp)) if(fread(&stu[m],LEN,1,fp)==1) m++;/*統計記錄個數即學生個數*/if(m==0){printf("沒有記錄!\n");fclose(fp);return;}printf("該年級有 %d 個學生!\n",m);/*將統計的個數輸出*/fclose(fp);}/**********************查詢學生成績函式*********************/void search(){ FILE *fp;int i,m=0;char snum[15];char ch[2];if((fp=fopen("mydata.txt","rb+"))==NULL){ printf("can not open the file\n");return;}while(!feof(fp)) if(fread(&stu[m],LEN,1,fp)==1) m++;fclose(fp);if(m==0){printf("沒有記錄!\n");return;}printf("請輸入學號:");scanf("%s",snum);for(i=0;i<m;i++)if(strcmp(snum,stu[i].num)==0)/*查詢輸入的學號是否在記錄中*/{ printf("找到該學生成績,是否顯示?(y/n)");scanf("%s",ch);if(strcmp(ch,"Y")==0||strcmp(ch,"y")==0) {printf(" Term PClass Name Number UMath UEnglish CLanguage Tscore Avescore\n");printf(FORMAT,DATA);/*將查找出的結果按指定格式輸出*/break;}elsereturn;} if(i==m) printf("沒有找到該學生的成績!\n");/*未找到要查詢的資訊*/}/*****************按班級輸出學生成績****************/void ShowClassS(){FILE *fp;int i,j,m=0;char k[15];printf("請輸入班級:\n");scanf("%s",k); printf(" Term PClass Name Number UMath UEnglish CLanguage Tscore Avescore\n");if((fp=fopen("mydata.txt","rb+"))==NULL){ printf("can not open the file\n");return;}while(!feof(fp)) if(fread(&stu[m],LEN,1,fp)==1) m++;fclose(fp);for(i=0;i<m;i++){ if(strcmp(k,stu[i].Class)==0) { stu[i].sum=stu[i].score[0]+stu[i].score[1]+stu[i].score[2];stu[i].ave=stu[i].sum/3.0; printf(FORMAT,DATA);//按班級輸出學生成績 }}}/***********輸出不及格學生成績****************/void ShowFailS(){FILE *fp;int i,j,m=0;printf("不及格學生名單及成績:\n\n");printf(" Term PClass Name Number UMath UEnglish CLanguage Tscore Avescore\n"); if((fp=fopen("mydata.txt","rb+"))==NULL){ printf("can not open the file\n");return;}while(!feof(fp)) if(fread(&stu[m],LEN,1,fp)==1) m++;fclose(fp);for(i=0;i<m;i++) { for(j=0;j<3;j++) if(stu[i].score[j]<60) { printf(FORMAT,DATA); /*輸出不及格學生名單及成績*/ break; }}printf("請按任意鍵繼續......");}