该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
#define LEN sizeof(struct student)
#include
#include
#include
struct student
{
int num;
char name[20];
int score[3];
int aver;
}; void personal_information_input(FILE *fp)
{
struct student *p;
int a,i,n;
printf("请输入将要输入的学生个数: ");
scanf("%d",&n);
printf("请输入学生的学号姓名和三科成绩:\n");
for(i=0;i
{
a=i+1;
printf("%d:",a);
p=(struct student *)malloc(LEN);
scanf("%d%s%d%d%d",&p->num,&p->name,&p->score[0],&p->score[1],&p->score[2]);
fwrite(p,LEN,1,fp);
p++;
}
} void personal_information_print(FILE *fp)
{
struct student *p,*p1;
int i=0;
p=(struct student *)malloc(LEN);
printf("请输入需要查找的学生的学号: ");
scanf("%d",&p->num);
p1=(struct student *)malloc(LEN);
fread(p1,LEN,1,fp);
while(!feof(fp))
{
fread(p1,LEN,1,fp);
if(p1->num==p->num)
printf("学号:%d 姓名:%s 成绩:%d %d %d 平均成绩:%d\n",
p->num,p->name,p->score[0],p->score[1],p->score[2],p->aver);
p1++;
fread(p1,LEN,1,fp);
}
} void average(FILE *fp)
{
struct student *p1;
p1=(struct student *)malloc(LEN);
fread(p1,LEN,1,fp);
while(!feof(fp))
{
p1->aver=(p1->score[0]+p1->score[1]+p1->score[2])/3.0;
fwrite(p1,LEN,1,fp);
p1++;
// p1=(struct student *)malloc(LEN);
fread(p1,LEN,1,fp);
}
}
void score_output(FILE *fp)
{
struct student *p1;
p1=(struct student *)malloc(LEN);
fread(p1,LEN,1,fp);
while(!feof(fp))
{
printf("学号:%d 姓名:%s 成绩:%d %d %d 平均成绩:%d\n",
p1->num,p1->name,p1->score[0],p1->score[1],p1->score[2],p1->aver);
// p1=(struct student *)malloc(LEN);
p1++;
fread(p1,LEN,1,fp);
}
}
void score_search(FILE *fp)
{
struct student *p,*p1;
p=(struct student *)malloc(LEN);
printf("输入要查找的学生成绩: ");
scanf("%d",&p->score[0]);
p1=(struct student *)malloc(LEN);
fread(p1,LEN,1,fp);
while(!feof(fp))
{
if(p1->score[0]==p->score[0])
printf("学号:%d 姓名:%s 成绩:%d %d %d 平均成绩:%d\n",
p1->num,p1->name,p1->score[0],p1->score[1],p1->score[2],p1->aver);
else if(p1->score[1]==p->score[0])
printf("学号:%d 姓名:%s 成绩:%d %d %d 平均成绩:%d\n",
p1->num,p1->name,p1->score[0],p1->score[1],p1->score[2],p1->aver);
else if(p1->score[2]==p->score[0])
printf("学号:%d 姓名:%s 成绩:%d %dd %d 平均成绩:%d\n",
p1->num,p1->name,p1->score[0],p1->score[1],p1->score[2],p1->aver);
p1=(struct student *)malloc(LEN);
fread(p1,LEN,1,fp);
p1++;
}
} void main()
{
char ch;
FILE *fp;
char filename[20];
printf("请输入所用的文件名:");
scanf("%s",filename);
if((fp=fopen(filename,"ab+"))==NULL)
{
printf("无法打开此文件!\n");
exit(0);
}
else
{ fp=fopen(filename,"wb+"); }
ch=getchar();
while(1)
{
printf("\n******************欢迎使用学生信息管理系统********************\n");
printf("\n******************** 1. 个人信息的录入 ********************\n");
printf("\n******************** 2. 个人信息的输出 ********************\n");
printf("\n******************** 3. 计算平均成绩 ********************\n");
printf("\n******************** 4. 成绩的查询 ********************\n");
printf("\n******************** 5. 成绩的输出 ********************\n");
printf("\n******************** 6. 退出程序 ********************\n");
printf("\n************ 请选择上述选项执行相应的操作 ************\n");
printf("************** 你选择执行的选项是: ");
ch=getchar();
switch (ch)
{
case '1':personal_information_input(fp);break;
case '2':personal_information_print(fp);break;
case '3':average(fp);break;
case '4':score_search(fp);break;
case '5':score_output(fp);break;
case '6':exit(0);
default: break;
}
getchar();
}
}