源代码:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct students{
char num[10];
double Chinese;
double Math;
double English;
double sum;
};
void menu();
void Appendrecord(struct students *p,int *i);
void Listrecord(struct students *p,int i);
void Deleterecord(struct students *p,int *i);
void Modifyrecord(struct students *p,int i);
void Searchrecord(struct students *p,int i);
void Sort(struct students *p,int q,int i);
void Write(struct students *p,FILE *q,int i);
void Read(struct students *p,FILE *q,int i);
int full=1;
int main()
{
int n;
printf("Please input the total number of students:");
scanf("%d",&n);
struct students *a;
a=(struct students*)malloc(n*sizeof(struct students));
menu();
char choice;
int flag=0,i=-1;
FILE *fp;
while(1)
{
printf("Please input your choice:");
scanf("%c",&choice);
switch(choice)
{
case '1':Appendrecord(a,&i);break;
case '2':Listrecord(a,i);break;
case '3':Deleterecord(a,&i);break;
case '4':Modifyrecord(a,i);break;
case '5':Searchrecord(a,i);break;
case '6':Sort(a,6,i);break;
case '7':Sort(a,7,i);break;
case '8':Sort(a,8,i);break;
case '9':Sort(a,9,i);break;
case '0':flag=1;break;
case 'W':if((fp=fopen("E:\\scores.txt", "w")) == NULL)
{
printf("can not open file cashbox.dat!\n");
exit(0);
}
Write(a,fp,i);break;
case 'R':if((fp=fopen("E:\\scores.txt", "w+")) == NULL)
{
printf("can not open file cashbox.dat!\n");
exit(0);
}
Read(a,fp,i);break;
default:printf("The input is illegal.Please re-enter.\n");break;
}
if(flag)break;
}
return 0;
}
void menu()
{
printf("Management for Students's scores\n");
printf("1.Append record\n");
printf("2.List record\n");
printf("3.Delete record\n");
printf("4.Modify record\n");
printf("5.Search record\n");
printf("6.Sort in descending order by sum\n");
printf("7.Sort in ascending order by sum\n");
printf("8.Sort in descending order by num\n");
printf("9.Sort in ascending order by num\n");
printf("W.Write to a File\n");
printf("R.Read from a File\n");
printf("0.Exit\n");
}
void Appendrecord(struct students *p,int *i)
{
(*i)++;
printf("Please input the student number, Chinese score, Math");
printf(" score and English score you want to add in turn, separated by spaces:\n");
scanf("%s%lf%lf%lf",p[*i].num,&p[*i].Chinese,&p[*i].Math,&p[*i].English);
p[*i].sum=p[*i].Chinese+p[*i].Math+p[*i].English;
printf("Added successfully.\n");
}
void Listrecord(struct students *p,int i)
{
if(i>=0)
for(int j=0;j<=i;j++)
{
printf("student number:%s,Chinese score:%lf,Math score:%lf,English score:%lf,sum:%lf\n",
p[j].num,p[j].Chinese,p[j].Math,p[j].English,p[j].sum);
}
else printf("At present, there is no student information\n");
}
void Deleterecord(struct students *p,int *i)
{
char q[8];
printf("Please enter the student number of the student information you want to delete:");
scanf("%s",q);
int j,k,flag=0;
if(*i>=0){
for(j=0;j<=*i;j++)
{
if(!strcmp(p[j].num,q)){
flag=1;
break;
}
}
if(flag){
for(k=j;k<*i;k++)
{
p[k]=p[k+1];
}
*i--;
}
else printf("This student was not found.\n");
}
else printf("At present, there is no student information\n");
}
void Modifyrecord(struct students *p,int i)
{
char q[8];
double a,b,c;
printf("Please enter in order the student number of the student information you want to modify、");
printf("Chinese, mathematics and English scores (if there are subjects that need not be revised, please use negative numbers instead):\n");
scanf("%s%lf%lf%lf",q,&a,&b,&c);
int j,flag=1;
if(i>=0){
for(j=0;j<=i;j++)
{
if(!strcmp(p[j].num,q))
{
if(a>=0)p[j].Chinese=a;
if(b>=0)p[j].Math=b;
if(c>=0)p[j].English=c;
p[j].sum=p[j].Chinese+p[j].Math+p[j].English;
flag=0;
}
}
if(flag) printf("This student was not found.\n");
}
else printf("At present, there is no student information\n");
}
void Searchrecord(struct students *p,int i)
{
char q[8];
printf("Please enter the student number of the student information you want to search:");
scanf("%s",q);
int j,flag=1;
if(i>=0){
for(j=0;j<=0;j++)
{
if(!strcmp(p[j].num,q))
{
printf("number:%s,Chinese:%.1f,Math:%.1f,English:%.1f,sum:%.1f",
q,p[j].Chinese,p[j].Math,p[j].English,p[j].Chinese);
flag=0;
break;
}
}
if(flag)printf("This student was not found.\n");
}
else printf("At present, there is no student information\n");
}
void Sort(struct students *p,int q,int i)
{
int j,k,index;
struct students t;
for(j=0;j<i-1;j++)
{
index=j;
for(k=j+1;k<i;k++)
{
switch(q){
case 6:if(p[k].sum>p[index].sum)index=k;
case 7:if(p[k].sum<p[index].sum)index=k;
case 8:if(strcmp(p[k].num,p[index].num)>0)index=k;
case 9:if(strcmp(p[k].num,p[index].num)<0)index=k;
}
}
t=p[index];
p[index]=p[j];
p[j]=t;
}
}
void Write(struct students *p,FILE *q,int i)
{
fwrite(p,sizeof(struct students),i+1,q);
rewind(q);
fclose(q);
full=0;
} \
void Read(struct students *p,FILE *q,int i)
{
if(full)printf("The data in the current memory is not saved.\n");
else {
fread(p,sizeof(struct students),i+1,q);
rewind(q);
fclose(q);
full=0;
}
}