#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();
case 3:Find();clear();
case 4:Input(file_operate("ab"));clear();
case 5:Alter(file_operate("wb"));clear();
case 6:Save();clear();
case 7:set_psw();clear();
case 8:Sort_menu();
case 0:system("cls");
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");
void Init() /*初始化,檔案不存在,則建立,同時記錄下檔案中的記錄數 */
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;
if(student[i].no==no_temp)
result=1;
if(result==1)
return i;
else
return -1;
int Inputoneperson(int i)/*向數組增加一條記錄 */
int j,sum=0;
int no_temp;
printf("輸入學号:(如10141301)");
scanf("%d",&no_temp);
if(no_temp<10141301)
while(no_temp<10141301);
if(CheckNumber(no_temp)>0)
printf("Number repeatly!\n");
return 0;
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();
return 1;
void Input(FILE *fp)/*向管理系統中增加記錄 */
i=total;
if(Inputoneperson(i))
total++;
Inputfile(i,fp);
void Inputfile(int i,FILE *fp) /*把下标為i 的記錄寫入檔案 */
fprintf(fp,"%8d",student[i].no);
fprintf(fp,"%8s ",student[i].name);
fprintf(fp,"%1c ",student[i].sex);
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;
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;
fp=file_operate("rb");
printf("Input the number that someone you want to find:(如10141303)");
i=CheckNumber(no_temp);
if(i>=0)
printf("Nobody is the number:%d\n",no_temp);
void Save()/*備份資訊到檔案 */
char filename[10],ch;
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");
ch=getchar();
Show();
void Delete(FILE *fp)/*删除記錄 */
int i,j,k,no_temp,choice2;
printf("Inpute the number someone you needed:(如10141301)");
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;
student[j].mark[k]=student[j+1].mark[k];
total--;
for(k=0;k<total;k++)
Inputfile(k,fp);
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)");
printf("Input name(less than 20 numbers):");
scanf("%c",&student[i].sex);
for(j=0;j<total;j++)
Inputfile(j,fp);
int Sort_menu_choice()/*選擇是按學号還是成績排序*/
int choice;
printf("輸入排序方式(1 按學号 2 按平均成績): ");
scanf("%d",&choice);
if((choice<0)||(choice>2))
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()/*選擇是按成績排序*/
if(student[i].mark[3]<student[j].mark[3])
void Sort_menu()/*排序菜單*/
switch(Sort_menu_choice())
case 1:Sort_no();
Sort_save();
clear();
break;
case 2:Sort_mark();
void Sort_save()/*排序後資料寫入檔案*/
if((fp=fopen("student.txt","wb"))==NULL)
printf("Fail to save the file!\n");
void set_psw()/*設定密碼 */
char psw_set[20],psw_check[20],c;
unsigned int i;
{ 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:");
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);
printf("error!\n");
while(strcmp(psw_set,psw_check)!=0);
fp=fopen("password.txt","wb");
fprintf(fp,"%s",password);
int psw_check()/*密碼驗證 */
unsigned int i=1,j=1;
char pword[20],c;
if((fp=fopen("password.txt","rb"))==NULL)
fp=fopen("password.txt","a");
set_psw();
fscanf(fp,"%s",password);
printf("\nInput password:key'.'for end(%d/three times)",j);
for(j=0;(c=getch())!='.';j++)
pword[j]=c;
pword[j]='\0';
i++;
while(strcmp(pword,password)!=0&&i<=3);
if(i<=3)
printf("You have tryed for three times,fail to open the file!\n");
void main()/*主函數*/
if(psw_check())
Init();
menu();
==================== 迂者 丁小未 CSDN部落格專欄=================
MyTel:13262983383
====================== 互相學習,共同進步 ===================