第1个回答 2008-04-14
下面是我以前写的一个,你自己在上面改一下吧,主要是查询的地方,我只写了按学号查询的代码,其它的你要自己写了,其它方面的都有
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define N 10
typedef struct //将学生信息定义为结构体
{
char number[20];
char name[20];
int age;
float grade[3]; //3个数组元素分别代表数学、英语、物理成绩
}student;
student whole[N]; //储存学生信息
int row=0;
FILE *fp;
void FileWrite() //输出到文件中
{
int i;
fclose(fp);
if((fp=fopen("student.txt","w"))==0)
{
printf("can't open student.txt");
exit(1);
}
for(i=0;i<row-1;i++)
fprintf(fp,"%-16s%-16s%d\t%3.1f\t%3.1f\t%3.1f\n",whole[i].number,whole[i].name,whole[i].age,whole[i].grade[0],whole[i].grade[1],whole[i].grade[2]);
fprintf(fp,"%-16s%-16s%d\t%3.1f\t%3.1f\t%3.1f",whole[i].number,whole[i].name,whole[i].age,whole[i].grade[0],whole[i].grade[1],whole[i].grade[2]);
fclose(fp);
if((fp=fopen("student.txt","r"))==0)
{
printf("can't open student.txt");
exit(1);
}
return;
}
void CopyAll(student *to,student from) //复制学生信息
{
int i;
strcpy(to->name,from.name);
strcpy(to->number,from.number);
to->age=from.age;
for(i=0;i<3;i++)
to->grade[i]=from.grade[i];
}
int Find(char *number) //查找学生
{
int i;
for(i=0;i<row;i++)
if(strcmp(whole[i].number,number)==0)
return i;
return -1;
}
void Modify(student change) //修改学生信息
{
int n;
n=Find(change.number);
if(n<0)
{
printf("can't find the student of the number\n");
return;
}
CopyAll(&whole[n],change);
puts("Modify the information successful");
}
void Insert(student add) //插入学生信息
{
int i,n;
char choose;
if(Find(add.number)>=0)
{
printf("The student is exist.Do you want to Modify the student's information? Y/N : ");
fflush(stdin);
scanf("%c",&choose);
if(choose=='Y')
Modify(add);
return ;
}
for(i=0;i<row&&strcmp(add.number,whole[i].number)>0;i++);
n=i;
for(i=row;i>n;i--)
CopyAll(&whole[i],whole[i-1]);
CopyAll(&whole[n],add);
printf("Add the information seccessful\n");
row++;
}
void Delete(char *number) //删除学生信息
{
char choose;
int n,i;
n=Find(number);
if(n<0)
{
printf("can't find the student of the number\n");
return;
}
puts("the information of the student:\n");
puts("\tnumber\t\tname\t\tage\tmath\tenglish\tphysics\n");
printf("\t%-16s%-16s%d\t%3.1f\t%3.1f\t%3.1f\n",whole[n].number,whole[n].name,whole[n].age,whole[n].grade[0],whole[n].grade[1],whole[n].grade[2]);
printf("Are you sure to delete the student.Y/N : ");
fflush(stdin);
scanf("%c",&choose);
if(choose=='Y')
{
for(i=n;i<row-1;i++)
CopyAll(&whole[i],whole[i+1]);
printf("the student is delete successful\n");
row--;
}
}
void Score(char *number,float grade[3]) //改变成绩
{
int n,i;
student change;
n=Find(number);
strcpy(change.number,number);
strcpy(change.name,whole[n].name);
change.age=whole[n].age;
for(i=0;i<3;i++)
change.grade[i]=grade[i];
CopyAll(&whole[n],change);
}
void RegisterScore(char *number,float grade[3]) //登记成绩
{
int n;
n=Find(number);
if(n<0)
{
printf("can't find the student of the number\n");
return;
}
Score(number,grade);
puts("Register the information successful");
}
void ModifyScore(char *number,float grade[3]) //修改成绩
{
int n;
n=Find(number);
if(n<0)
{
printf("can't find the student of the number\n");
return;
}
Score(number,grade);
puts("Modify the information successful");
}
void BrowseScore() //浏览全班学生成绩
{
int i;
puts("the information of the score:\n");
puts("\tnumber\t\tmath\tenglish\tphysics\n");
for(i=0;i<row;i++)
printf("\t%-16s%3.1f\t%3.1f\t%3.1f\n\n",whole[i].number,whole[i].grade[0],whole[i].grade[1],whole[i].grade[2]);
}
void CountScore(student *all,float *total) //计算学生总成绩
{
int i,j;
for(i=0;i<row;i++)
{
total[i]=0;
for(j=0;j<3;j++)
total[i]+=all[i].grade[j];
}
puts("the information of the score:\n");
puts("\tnumber\t\tmath\tenglish\tphysics\ttotal\n");
for(i=0;i<row;i++)
printf("\t%-16s%3.1f\t%3.1f\t%3.1f\t%3.1f\n\n",all[i].number,all[i].grade[0],all[i].grade[1],all[i].grade[2],total[i]);
}
void PrintTotal() //输出所有学生信息
{
int i;
puts("the information of the student:\n");
puts("\tnumber\t\tname\t\tage\tmath\tenglish\tphysics\n");
for(i=0;i<row;i++)
printf("\t%-16s%-16s%d\t%3.1f\t%3.1f\t%3.1f\n\n",whole[i].number,whole[i].name,whole[i].age,whole[i].grade[0],whole[i].grade[1],whole[i].grade[2]);
}
void Menu() //菜单
{
puts("*****************************************************\n");
puts("please select a choose:(End by 0)\n");
puts("A:\n\n\tInsert a new recode.");
puts("B:\n\n\tModify a recode.");
puts("C:\n\n\tDelete a recode.");
puts("D:\n\n\tRegister score.");
puts("E:\n\n\tModify score.");
puts("F:\n\n\tBrowse all the score of the class.");
puts("\n*****************************************************\n");
}
int main() //主函数
{
char choose,number[20];
float total[N];
int i=0;
float NewScore[3],MdScore[3];
student NewRecode,ModifyRecode;
if((fp=fopen("student.txt","r"))==0)
{
printf("can't open the file student.txt\n");
exit(1);
}
while(!feof(fp)) //将文件信息读入数组whole
{
fscanf(fp,"%s%s%d%f%f%f",whole[i].number,whole[i].name,&whole[i].age,&whole[i].grade[0],&whole[i].grade[1],&whole[i].grade[2]);
row++; //记录行数(即学生数)
i++;
}
PrintTotal();
Menu();
while(scanf("%c",&choose)) //选择操作
{
if(choose=='0')
break;
system("cls");
switch(choose)
{
case 'A':
puts("please enter the information of the new student like this:\n");
puts("number name age math english physycs\n");
fflush(stdin);
scanf("%s%s%d%f%f%f",NewRecode.number,NewRecode.name,&NewRecode.age,&NewRecode.grade[0],&NewRecode.grade[1],&NewRecode.grade[2]);
Insert(NewRecode);
break;
case 'B':
puts("please enter the new information of the student you want to Modify like this:\n");
puts("number name age math english physycs\n");
fflush(stdin);
scanf("%s%s%d%f%f%f",ModifyRecode.number,ModifyRecode.name,&ModifyRecode.age,&ModifyRecode.grade[0],&ModifyRecode.grade[1],&ModifyRecode.grade[2]);
Modify(ModifyRecode);
break;
case 'C':
puts("please enter the number of the student you want to delete:\n");
fflush(stdin);
scanf("%s",number);
Delete(number);
break;
case 'D':
puts("please enter the number and the score of the new student like this:\n");
puts("number math english physycs\n");
fflush(stdin);
scanf("%s%f%f%f",number,&NewScore[0],&NewScore[1],&NewScore[2]);
RegisterScore(number,NewScore);
break;
case 'E':
puts("please enter the number and the score of the student like this:\n");
puts("number math english physycs\n");
fflush(stdin);
scanf("%s%f%f%f",number,&MdScore[0],&MdScore[1],&MdScore[2]);
ModifyScore(number,MdScore);
break;
case 'F':
BrowseScore();
break;
default:
printf("please attention to the Case\n");
break;
}
FileWrite();
fflush(stdin); //清空缓存
system("pause");
system("cls");
Menu();
puts("please select a choose:(End by 0)\n");
}
CountScore(whole,total);
PrintTotal();
fclose(fp);
return 0;
}