//就是用这个程序把结构写到这个文件中的,这个程序我试了好几次都是可以运行的,上面那个程序就不行了
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
#define MAX 20
#define SUB 3
typedef struct Student Student;
typedef struct Class Class;
struct Student
{
char name[MAX];
double subject[SUB];
double student_average;
};
struct Class
{
char class_name[MAX];
Student** student;
Class* pnext;
double class_average[SUB];
int student_number;
};
FILE* pfile=NULL;
char* filename="D:\\蒋臣健\\学生成绩.bin";
double get_student(Student* student,double class_average[],size_t n);
//返回一个学生的三个科目的分数平均值
Class* get_class(void);
void free_memory(Class* pfirst);
void list_class(Class* first);
int camp(char* name,Class* first);
Class* get_class(void)
{
int number=0;
char ch='a';
Class* pclass=NULL;
if(!(pclass=calloc(1,sizeof(Class))))
{
printf("班级分配内存失败");
exit(1);
}
pclass->student=calloc(MAX,sizeof(Student*));
if(!(pclass->student))
{
printf("存放结构的指针的内存失败");
exit(1);
}
printf("请输入班级名字:");
scanf("%s",pclass->class_name);
do
{
pclass->student[number]=calloc(1,sizeof(Student));
if(!(pclass->student[number]))
{
printf("学生结构内存分配失败");
exit(2);
}
pclass->student[number]->student_average=get_student(pclass->student[number],pclass->class_average,MAX);
++number;
++pclass->student_number;
fflush(stdin);
printf("还要输入学生成绩吗?");
scanf("%c",&ch);
}while(ch=='y');
return pclass;
}
double get_student(Student* student,double class_average[],size_t n)
{
int i=0;
double average=0.0;
double sum=0.0;
fflush(stdin);
printf("请输入学生姓名:");
scanf("%s",student->name);
for(i=0;i<SUB;i++)
{
switch(i)
{
case 0:
printf("please enter %s Chinese scores",student->name);
break;
case 1:
printf("please enter %s Maths scores",student->name);
break;
case 2:
printf("please enter %s English scores",student->name);
break;
default:
break;
}
scanf("%lf",&student->subject[i]);
class_average[i]+=student->subject[i];
sum+=student->subject[i];
}
average=sum/SUB;
return average;
}
void list_class(Class* first)
{
Class* pcurrent=first;
int b=0;
int i=0;
while(pcurrent!=NULL)
{
printf("%s
",pcurrent->class_name);
for(b=0;b<pcurrent->student_number;b++)
{
for(i=0;i<SUB;i++)
{
switch(i)
{
case 0:
printf("%s的语文成绩为%.2lf
",pcurrent->student[b]->name,pcurrent->student[b]->subject[i]);
break;
case 1:
printf("%s的数学成绩为%.2lf
",pcurrent->student[b]->name,pcurrent->student[b]->subject[i]);
break;
case 2:
printf("%s的英语成绩为%.2lf\n",pcurrent->student[b]->name,pcurrent->student[b]->subject[i]);
break;
default:
break;
}
}
printf("%s的平均成绩为%.2lf\n",pcurrent->student[b]->name,pcurrent->student[b]->student_average);
}
for(i=0;i<SUB;i++)
{
switch(i)
{
case 0:
printf("%s的语文总平均分为%.2lf
",pcurrent->class_name,pcurrent->class_average[i]);
break;
case 1:
printf("%s的数学总平均分为%.2lf
",pcurrent->class_name,pcurrent->class_average[i]);
break;
case 2:
printf("%s的英文总平均分为%.2lf\n",pcurrent->class_name,pcurrent->class_average[i]);
break;
default:
break;
}
}
printf("班级总人数%d",pcurrent->student_number);
pcurrent=pcurrent->pnext;
}
}
void free_memory(Class* pfirst)
{
int b=0;
Class* pcurrent=pfirst;
while(pcurrent!=NULL)
{
Class* ptemp=NULL;
ptemp=pcurrent;
for(b=0;b<pcurrent->student_number;b++)
{
free(pcurrent->student[b]);
pcurrent->student[b]=NULL;
}
free(pcurrent->student);
pcurrent->student=NULL;
pcurrent=pcurrent->pnext;
free(ptemp);
ptemp=NULL;
}
}
int main()
{
char word='y';
Class class_one;
do
{
Class* pfirst=NULL;
Class* previous=NULL;
Class* pcurrent=NULL;
int i=0;
int b=0;
char ch=0;
int student_number=0;
int dex=0;
char name[MAX];
if(!(pfile=fopen(filename,"wb")))
{
printf("Failed to open");
exit(1);
}
if(setvbuf(pfile,NULL,_IOFBF,BUFSIZ))
{
printf("file buffering failed");
exit(2);
}
do
{
pcurrent=get_class();
if(fwrite(pcurrent,sizeof(Class),1,pfile)<1)
printf("写入失败");
for(b=0;b<SUB;b++)
pcurrent->class_average[b]=pcurrent->class_average[b]/pcurrent->student_number;
printf("sizeof(pcurrent)=%d",sizeof(Student));
if(!pfirst)
pfirst=previous=pcurrent;
else
{
previous->pnext=pcurrent;
previous=pcurrent;
previous->pnext=NULL;
}
fflush(stdin);
printf("还想输入班级吗");
scanf("%c",&ch);
}while(ch=='y');
list_class(pfirst);
fflush(stdin);
printf("请输入你想要找的学生:");
scanf("%s",name);
if(!(dex=camp(name,pfirst)))
printf("没有这个人");
pfirst=NULL;
fclose(pfile);
if(!(pfile=fopen(filename,"rb")))
{
printf("Failed to open");
exit(1);
}
if(setvbuf(pfile,NULL,_IOFBF,BUFSIZ))
{
printf("file buffering failed");
exit(2);
}
fread(&class_one,sizeof(Class),1,pfile);
printf("sizeof=%d",sizeof(class_one));
for(i=0;i<class_one.student_number;i++)
{
printf("%lf ",class_one.student[i]->student_average);
}
free_memory(pfirst);
pfirst=NULL;
fclose(pfile);
pfile=NULL;
fflush(stdin);
printf("还想再来一次");
scanf("%c",&word);
}while(tolower(word)=='y');
return 0;
}
int camp(char* name,Class* first)
{
Class* pcurrent=first;
int i=0;
int b=0;
while(pcurrent!=NULL)
{
for(b=0;b<pcurrent->student_number;b++)
{
if(strcmp(name,pcurrent->student[b]->name)==0)
{
printf("%s在%s\n",pcurrent->student[b]->name,pcurrent->class_name);
for(i=0;i<SUB;i++)
{
switch(i)
{
case 0:
printf("%s的语文成绩为%.2lf
",pcurrent->student[b]->name,pcurrent->student[b]->subject[i]);
break;
case 1:
printf("%s的数学成绩为%.2lf
",pcurrent->student[b]->name,pcurrent->student[b]->subject[i]);
break;
case 2:
printf("%s的英语成绩为%.2lf\n",pcurrent->student[b]->name,pcurrent->student[b]->subject[i]);
break;
default:
break;
}
}
return 1;
}
}
pcurrent=pcurrent->pnext;
}
return 0;
}