#include <stdio.h> #include <stdlib.h> #include <string.h> #include <conio.h> #include <ctype.h> #define N 100 struct scorce { char admi[6]; char password[6]; char num[10]; char name[30]; int scorce[6]; char key[5]; }data,info[N],a; /*data放数据,info放排序后的数据*/ int i,j,k,m,l=0; char s[20]; /*存储参数*/ FILE *fp,*fp1,*fp2;
void input() { char c; if((fp=fopen("s_scorce.txt","ab+"))==NULL) /*以读写打开文件,并判断是否打开*/ { printf("cannot open this file.\n"); getch(); } for(i=0;i<=100;i++) { char c; printf("Please input student's password:"); /*资料录入*/ gets(data.password);
printf("Please input number:"); gets(data.num);
printf("Please input name:"); gets(data.name);
printf("Please input maths scorce:"); gets(s); data.scorce[1]=atoi(s);
printf("Please input english scorce:"); gets(s); data.scorce[2]=atoi(s);
printf("Please input physics scorce:"); gets(s); data.scorce[3]=atoi(s);
printf("Please input chemistry scorce:"); gets(s); data.scorce[4]=atoi(s);
data.scorce[5]=data.scorce[1]+data.scorce[2]+data.scorce[3]+data.scorce[4]; /*总分*/
data.scorce[6]=((data.scorce[1]+data.scorce[2]+data.scorce[3]+data.scorce[4])/4); /*平均分*/ fwrite(&data,sizeof(data),1,fp); /*写入文件*/ printf("another? (y/n)\n"); c=getch(); if(c=='n'||c=='N') break; }fclose(fp); }
void sort_maths() /*数学排序*/ { float n; if((fp=fopen("s_scorce.txt","r"))==NULL) /*只读打开并判断是否打开*/ { printf("Cannot reading this file.\n"); exit(0); } printf("\nfile:"); for(i=0;fread(&info,sizeof(struct scorce),1,fp)!=0;i++) {printf("\n%8s%8s",info.num,info.name); printf("%d",info.scorce[1]); }fclose(fp); n=i; for(i=0;i<n;i++) /*冒泡排序*/ for(j=i+1;j<n;j++) if(info.scorce[1]<info[j].scorce[1]) {a=info; info=info[j]; info[j]=a; } printf("\nnow:"); fp=fopen("stu_sort.txt","w"); for(i=0;i<n;i++) {fwrite(&info,sizeof(struct scorce),1,fp); printf("\n%8s%8s",info.num,info.name); printf("%d",info.scorce[1]); } getch(); fclose(fp); } void sort_english() { float n; if((fp=fopen("s_scorce.txt","r"))==NULL) { printf("Cannot reading this file.\n"); exit(0); } printf("\nfile:"); for(i=0;fread(&info,sizeof(struct scorce),1,fp)!=0;i++) {printf("\n%8s%8s",info.num,info.name); printf("%d",info.scorce[2]); }fclose(fp); n=i; for(i=0;i<n;i++) for(j=i+1;j<n;j++) if(info.scorce[2]<info[j].scorce[2]) {a=info; info=info[j]; info[j]=a; } printf("\nnow:"); fp=fopen("stu_sort.txt","w"); for(i=0;i<n;i++) {fwrite(&info,sizeof(struct scorce),1,fp); printf("\n%8s%8s",info.num,info.name); printf("%d",info.scorce[2]); } getch(); fclose(fp); } void sort_physics() { float n; if((fp=fopen("s_scorce.txt","r"))==NULL) { printf("Cannot reading this file.\n"); exit(0); } printf("\nfile:"); for(i=0;fread(&info,sizeof(struct scorce),1,fp)!=0;i++) {printf("\n%8s%8s",info.num,info.name); printf("%d",info.scorce[3]); }fclose(fp); n=i; for(i=0;i<n;i++) for(j=i+1;j<n;j++) if(info.scorce[3]<info[j].scorce[3]) {a=info; info=info[j]; info[j]=a; } printf("\nnow:"); fp=fopen("stu_sort.txt","w"); for(i=0;i<n;i++) {fwrite(&info,sizeof(struct scorce),1,fp); printf("\n%8s%8s",info.num,info.name); printf("%d",info.scorce[3]); } getch(); fclose(fp); } void sort_chemistry() { float n; if((fp=fopen("s_scorce.txt","r"))==NULL) { printf("Cannot reading this file.\n"); exit(0); } printf("\nfile:"); for(i=0;fread(&info,sizeof(struct scorce),1,fp)!=0;i++) {printf("\n%8s%8s",info.num,info.name); printf("%d",info.scorce[4]); }fclose(fp); n=i; for(i=0;i<n;i++) for(j=i+1;j<n;j++) if(info.scorce[4]<info[j].scorce[4]) {a=info; info=info[j]; info[j]=a; } printf("\nnow:"); fp=fopen("stu_sort.txt","w"); for(i=0;i<n;i++) {fwrite(&info,sizeof(struct scorce),1,fp); printf("\n%8s%8s",info.num,info.name); printf("%d",info.scorce[4]); } getch(); fclose(fp); } void sort_total() { float n; if((fp=fopen("s_scorce.txt","r"))==NULL) { printf("Cannot reading this file.\n"); exit(0); } printf("\nfile:"); for(i=0;fread(&info,sizeof(struct scorce),1,fp)!=0;i++) {printf("\n%8s%8s",info.num,info.name); printf("%d",info.scorce[5]); }fclose(fp); n=i; for(i=0;i<n;i++) for(j=i+1;j<n;j++) if(info.scorce[5]<info[j].scorce[5]) {a=info; info=info[j]; info[j]=a; } printf("\nnow:"); fp=fopen("stu_sort.txt","w"); for(i=0;i<n;i++) {fwrite(&info,sizeof(struct scorce),1,fp); printf("\n%8s%8s",info.num,info.name); printf("%d",info.scorce[5]); } getch(); fclose(fp); }
void display() /*显示数据*/ { float s;int n; if((fp=fopen("s_scorce.txt","rb+"))==NULL) /*只读打开*/ { printf("Cannot reading this file.\n"); exit(0); } for(i=0;i<=100;i++) { if((fread(&info,sizeof(info),1,fp))!=1) break; } printf("\nclass number name chinese maths english physics chemistry total average\n"); for(j=0,k=1;j<i;j++,k++) { printf(" %6s %5s %3d %3d %3d %3d %3d \n",info[j].num,info[j].name,info[j].scorce[1],info[j].scorce[2],info[j].scorce[3],info[j].scorce[4],info[j].scorce[5]); } fclose(fp); getch(); } void showone() /*显示个人数据*/ { if((fp=fopen("s_scorce.txt","rb"))==NULL) { printf("\nCannot open this file.\n"); exit(0); } printf("\nPlease input number:"); scanf("%d",&i); printf("Please input password:"); scanf("%d",&k); while(fread(&data,sizeof(data),1,fp)==1) { j=atoi(data.num); l=atoi(data.password); if((i==j)&&(k==l)) { printf("num:%7s name:%7s\n maths: %3d\n english: %3d\n physics: %3d\n chemistry: %3d\n total: %3d\n average: %3d\n ",data.num,data.name,data.scorce[1],data.scorce[2],data.scorce[3],data.scorce[4],data.scorce[5],data.scorce[6]); }fclose(fp); getch(); } } void change() /*修改数据*/ { if((fp=fopen("s_scorce.txt","rb+"))==NULL||(fp1=fopen("temp.txt","wb+"))==NULL) { printf("Cannot open this file.\n"); exit(0); } printf("\nPlease input the change number:"); scanf("%d",&i); getchar(); while((fread(&data,sizeof(data),1,fp))==1) { j=atoi(data.num); if(j==i) { printf("Please input name:"); gets(data.name);
printf("Please inpu maths scorce:"); gets(s); data.scorce[1]=atoi(s);
printf("Please input english scorce:"); gets(s); data.scorce[2]=atoi(s);
printf("Please input physics scorce:"); gets(s); data.scorce[3]=atoi(s);
printf("Please input chemistry scorce:"); gets(s); data.scorce[4]=atoi(s);
data.scorce[5]=data.scorce[1]+data.scorce[2]+data.scorce[3]+data.scorce[4]; data.scorce[6]=((data.scorce[1]+data.scorce[2]+data.scorce[3]+data.scorce[4])/4); } fwrite(&data,sizeof(data),1,fp1); } fseek(fp,0L,0); /*指针移至文件头*/ fseek(fp1,0L,0); while((fread(&data,sizeof(data),1,fp1))==1) /*可以读文件*/ { fwrite(&data,sizeof(data),1,fp); }
fclose(fp); fclose(fp1); } void search() /*查询*/ { if((fp=fopen("s_scorce.txt","rb"))==NULL) { printf("\nCannot open this file.\n"); exit(0); } printf("\nPlease input number:"); scanf("%d",&i); while(fread(&data,sizeof(data),1,fp)==1) { j=atoi(data.num); if(i==j) { printf(" number:%10s\n name:%8s %3d\n maths: %3d\n english: %3d\n physics: %3d\n chemistry: %3d\n total: %3d\n average: %3d\n",data.num,data.name,data.scorce[1],data.scorce[2],data.scorce[3],data.scorce[4],data.scorce[5],data.scorce[6]); } fclose(fp); getch(); } }
void change_password() { if((fp=fopen("s_scorce.txt","rb+"))==NULL||(fp1=fopen("temp.txt","wb+"))==NULL) { printf("\nCannot open this file.\n"); exit(0); } printf("\nPlease input number:"); scanf("%d",i); getchar(); printf("Please input password:"); scanf("%d",&k); getchar(); while(fread(&data,sizeof(data),1,fp)==1) { j=atoi(data.num); l=atoi(data.password); if((i==j)&&(k==l)) { printf("number:%s\n name:%s\n password:%s\n",data.num,data.name,data.password); printf("Please input new password:"); gets(data.password); } fwrite(&data,sizeof(data),1,fp1); } fseek(fp,0L,0); fseek(fp1,0L,0); while((fread(&data,sizeof(data),1,fp1))==1) { fwrite(&data,sizeof(data),1,fp); }
fclose(fp); fclose(fp1); } void menu() { char c; while(1) { char c; clrscr(); printf("**********************Scorce Manage System******************\n"); printf(" 1.diaplay the maths scorce \n"); printf(" 2.display the english scorce \n " ); printf(" 3.display rhe physics scorce \n"); printf(" 4.display the chemistry \n"); printf(" 5.display the total "); printf(" 0.quit \n"); printf("************************************************************\n"); printf(" Please enter your choice(0-5):"); c=getch(); switch(c) { case '1':sort_maths();break; case '2':sort_english();break; case '3':sort_physics();break; case '4':sort_chemistry();break; case '5':sort_total();break; case '0':exit(0); default: continue; } } }