#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 =
= =
= =
\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: =\n english: =\n
physics: =\n chemistry: =\n total: =\n
average: =\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)
{