#include<stdio.h> #include<stdlib.h> #include<string.h> #include<conio.h> #define MAX 2000 struct Student { int no; /*學號*/ char name[8]; /*姓名*/ char sex; /*性別*/ char phone[8];/*聯繫電話*/ int mark[4]; /*語文、數學、外語、考試平均成績*/ }; int total; /*總人數*/ char password[20];/*密碼*/ struct Student student[MAX]; /**************************************************************************/ void Init(); /*初始化,文件不存在,則創建,同時記錄下文件中的記錄數 */ int get_menu_choice() ;/*接受菜單選擇 */ void menu() ;/*菜單響應 */ void Show_menu(); /*顯示菜單 */ FILE *file_operate(char *mode); /*文件操做 */ void Delete(FILE *fp); /*刪除記錄 */ void Alter(FILE *fp); /*修改記錄信息 */ void Show(); /*顯示打印全部的信息 */ void Save(); /*備份信息到文件 */ void Find(); /*查找記錄的函數 */ void Input(FILE *fp); /*向管理系統中增長記錄 */ int Inputoneperson(int i); /*向數組增長一條記錄 */ void set_psw(); /*設置密碼 */ int psw_check(); /*密碼驗證 */ /*下面函數爲了簡化部分工做從上面函數中劃分出來的功能 */ /**************************************************************************/ void exchange(int i,int j);/*交換兩我的的全部信息 */ void PrintTitle(); /*打印頭信息 */ void clear(); /*清屏 */ void Showoneperson(int i); /*顯示打印一我的的信息 */ int CheckNumber(int no_temp); /*檢查學號是否存在,存在返回序號,不存在返回-1 */ void Inputfile(int i,FILE *fp); /*把下標爲i 的記錄寫入文件 */ void Readfile(int i,FILE *fp); /*讀一條記錄從文件 */ int Sort_menu_choice();/*選擇是按學號仍是成績排序*/ void Sort_menu();/*排序菜單*/ void Sort_save();/*排序後數據寫入文件*/ void Sort_no();/*選擇是按學號排序*/ void Sort_mark();/*選擇是按成績排序*/ int get_menu_choice()/*接受菜單選擇 */ { int menu_ch; /*菜單選項 */ do { printf("Select the choice:"); scanf("%d",&menu_ch); if((menu_ch<0)||(menu_ch>9)) printf("error!"); } while((menu_ch<0)||(menu_ch>9)); return(menu_ch); } void Show_menu()/*顯示菜單 */ { printf(" 歡迎使用學生成績管理系統 \n"); printf("*************************************************************\n"); printf(" 1.顯示數據 | 2.刪除數據 \n"); printf(" 3 查詢數據 | 4.輸入數據 \n"); printf(" 5.修改數據 | 6.備份數據 \n"); printf(" 7.設置口令 | 8.數據排序 \n"); printf(" 0.退出 \n"); printf("*************************************************************\n"); } void clear()/*清屏 */ { system("pause"); system("cls"); } void menu()/*菜單響應 */ { while(1) { Show_menu(); switch(get_menu_choice()) { case 1:Show();clear(); break; case 2:Delete(file_operate("wb"));clear(); break; case 3:Find();clear(); break; case 4:Input(file_operate("ab"));clear(); break; case 5:Alter(file_operate("wb"));clear(); break; case 6:Save();clear(); break; case 7:set_psw();clear(); break; case 8:Sort_menu(); break; case 0:system("cls"); printf("*****************************\n"); printf(" 歡迎使用本程序! \n"); printf("*****************************\n"); exit(0); } } } void Show()/*顯示打印全部的信息 */ { int i; printf("有%d個記錄:\n",total); PrintTitle(); for(i=0;i<total;i++) Showoneperson(i); } void Showoneperson(int i) /*顯示打印一我的的信息 */ { printf(" %8d %8s %1c %3d %3d %3d %3d \n",student[i].no,student[i].name,student[i].sex,student[i].mark[0],student[i].mark[1],student[i].mark[2],student[i].mark[3]); } void PrintTitle()/*打印頭信息 */ { printf("-----------------------------------------------------------------\n"); printf(" 學號 姓名 性別 語文 數學 英語 平均成績 \n"); printf("-----------------------------------------------------------------\n"); } void Init() /*初始化,文件不存在,則創建,同時記錄下文件中的記錄數 */ { int i; FILE *fp; total=0; if((fp=fopen("student.txt","rb"))==NULL) { fp=fopen("student.txt","ab"); clear(); menu(); } else { for(i=0;feof(fp)==0;i++) Readfile(i,fp); } total=i-1; fclose(fp); } void Readfile(int i,FILE *fp)/*讀一條記錄從文件 */ { int j; fscanf(fp,"%8d",&student[i].no); fscanf(fp,"%8s ",&student[i].name); fscanf(fp,"%1c",&student[i].sex); for(j=0;j<4;j++) fscanf(fp,"%3d",&student[i].mark[j]); } int CheckNumber(int no_temp)/*檢查學號是否存在,存在返回序號,不存在返回-1 */ { int i,result; for(i=0;i<total;i++) { if(student[i].no==no_temp) { result=1; break; } } if(result==1) return i; else return -1; } int Inputoneperson(int i)/*向數組增長一條記錄 */ { int j,sum=0; int no_temp; do { printf("輸入學號:(如10141301)"); scanf("%d",&no_temp); if(no_temp<10141301) printf("error!"); } while(no_temp<10141301); if(CheckNumber(no_temp)>0) { printf("Number repeatly!\n"); return 0; } else { student[i].no=no_temp; printf("Input name(lessthan 20 numbers):"); scanf("%s",student[i].name); printf("Sex(M/W):"); scanf("%s",&student[i].sex); printf("\n 語文\t 數學\t 英語\n"); for(j=0;j<3;j++) { scanf("%d",&student[i].mark[j]); sum=sum+student[i].mark[j]; } student[i].mark[3]=sum/3; PrintTitle(); Showoneperson(i); return 1; } } void Input(FILE *fp)/*向管理系統中增長記錄 */ { int i; i=total; if(Inputoneperson(i)) { total++; Inputfile(i,fp); } fclose(fp); } void Inputfile(int i,FILE *fp) /*把下標爲i 的記錄寫入文件 */ { int j; fprintf(fp,"%8d",student[i].no); fprintf(fp,"%8s ",student[i].name); fprintf(fp,"%1c ",student[i].sex); for(j=0;j<4;j++) fprintf(fp,"%3d ",student[i].mark[j]); } void exchange(int i,int j)/*交換兩我的的全部信息 */ { int k; int no_temp,mark_temp; char name_temp[20],sex_temp; no_temp=student[i].no; student[i].no=student[j].no; student[j].no=no_temp; for(k=0;k<4;k++) { mark_temp=student[i].mark[k]; student[i].mark[k]=student[j].mark[k]; student[j].mark[k]=mark_temp; } strcpy(name_temp,student[i].name); strcpy(student[i].name,student[j].name); strcpy(student[j].name,name_temp); sex_temp=student[i].sex; student[i].sex=student[j].sex; student[j].sex=sex_temp; } FILE *file_operate(char *mode)/*文件操做 */ { char choice; FILE *fp; do { fflush(stdin); if((fp=fopen("student.txt",mode))==NULL) { puts("Fail to open the file!"); puts("Try again!(Y/N)?"); scanf("%c",&choice); } } while(choice=='y'||choice=='Y'); if(choice=='n'||choice=='N') exit(1); return(fp); } void Find()/*查找記錄的函數 */ { int i,no_temp; FILE *fp; fp=file_operate("rb"); for(i=0;feof(fp)==0;i++) Readfile(i,fp); total=i-1; fclose(fp); printf("Input the number that someone you want to find:(如10141303)"); scanf("%d",&no_temp); i=CheckNumber(no_temp); if(i>=0) { PrintTitle(); Showoneperson(i); } else { printf("Nobody is the number:%d\n",no_temp); } } void Save()/*備份信息到文件 */ { int i; char filename[10],ch; FILE *fp; printf("Name the new file:(less than ten bits)"); scanf("%s",filename); if((fp=fopen(filename,"wb"))==NULL) { printf("Fail to build the file!\n"); exit(0); } ch=getchar(); for(i=0;i<total;i++) Inputfile(i,fp); Show(); fclose(fp); } void Delete(FILE *fp)/*刪除記錄 */ { int i,j,k,no_temp,choice2; printf("Inpute the number someone you needed:(如10141301)"); scanf("%d",&no_temp); i=CheckNumber(no_temp); if(i>=0) { PrintTitle(); Showoneperson(i); printf("Sure to delete the person?(1,Y/2,N)"); scanf("%d",&choice2); if(choice2==1) { for(j=i;j<total;j++) { strcpy(student[j].name,student[j+1].name); student[j].sex=student[j+1].sex; student[j].no=student[j+1].no; for(k=0;k<4;k++) { student[j].mark[k]=student[j+1].mark[k]; } } total--; for(k=0;k<total;k++) Inputfile(k,fp); } } else printf("Nobody is the number:%d\n",no_temp); } void Alter(FILE *fp)/*修改記錄信息 */ { int i,j,no_temp,sum=0; printf("Inpute the number somesoe you want to alter:(如10141301)"); scanf("%d",&no_temp); i=CheckNumber(no_temp); if(i>=0) { student[i].no=no_temp; printf("Input name(less than 20 numbers):"); scanf("%s",student[i].name); printf("Sex(M/W):"); scanf("%c",&student[i].sex); printf("\n 語文\t 數學\t 英語\n"); for(j=0;j<3;j++) { scanf("%d",&student[i].mark[j]); sum=sum+student[i].mark[j]; } student[i].mark[3]=sum/3; PrintTitle(); Showoneperson(i); for(j=0;j<total;j++) Inputfile(j,fp); } else printf("Nobody is the number:%d\n",no_temp); } int Sort_menu_choice()/*選擇是按學號仍是成績排序*/ { int choice; do { printf("輸入排序方式(1 按學號 2 按平均成績): "); scanf("%d",&choice); if((choice<0)||(choice>2)) printf("error!"); } while((choice<0)||(choice>2)); return(choice); } void Sort_no()/*選擇是按學號排序*/ { int i,j; for(i=0;i<total-1;i++) { for(j=i+1;j<total;j++) { if(student[i].no>student[j].no) exchange(i,j); } } } void Sort_mark()/*選擇是按成績排序*/ { int i,j; for(i=0;i<total-1;i++) { for(j=i+1;j<total;j++) { if(student[i].mark[3]<student[j].mark[3]) exchange(i,j); } } } void Sort_menu()/*排序菜單*/ { switch(Sort_menu_choice()) { case 1:Sort_no(); Sort_save(); clear(); break; case 2:Sort_mark(); Sort_save(); clear(); break; } } void Sort_save()/*排序後數據寫入文件*/ { int i; FILE *fp; if((fp=fopen("student.txt","wb"))==NULL) { printf("Fail to save the file!\n"); exit(0); } for(i=0;i<total;i++) Inputfile(i,fp); Show(); fclose(fp); } void set_psw()/*設置密碼 */ { char psw_set[20],psw_check[20],c; unsigned int i; FILE *fp; do { printf("You must set password first!\n"); printf("Enter password:(lessthan 12 numbers and key'.'for end)\n"); for(i=0;(c=getch())!='.';i++) { putchar('*'); psw_set[i]=c; } psw_set[i]='\0'; printf("\n------------\n"); printf("conform password:"); for(i=0;(c=getch())!='.';i++) { putchar('*'); psw_check[i]=c; } psw_check[i]='\0'; printf("\n------------\n"); if(strcmp(psw_set,psw_check)==0) {printf("Set password success!"); strcpy(password,psw_set); } else printf("error!\n"); } while(strcmp(psw_set,psw_check)!=0); clear(); fp=fopen("password.txt","wb"); fprintf(fp,"%s",password); fclose(fp); } int psw_check()/*密碼驗證 */ { unsigned int i=1,j=1; FILE *fp; char pword[20],c; if((fp=fopen("password.txt","rb"))==NULL) { fp=fopen("password.txt","a"); set_psw(); } fscanf(fp,"%s",password); fclose(fp); do { printf("\nInput password:key'.'for end(%d/three times)",j); for(j=0;(c=getch())!='.';j++) { putchar('*'); pword[j]=c; } pword[j]='\0'; i++; } while(strcmp(pword,password)!=0&&i<=3); if(i<=3) return 1; else { printf("You have tryed for three times,fail to open the file!\n"); return 0; } } void main()/*主函數*/ { if(psw_check()) { Init(); clear(); menu(); } }