#define SIZE 5
struct Student_type
{
int num;
char name[10];
float score[3];
float avg;
}stud[SIZE];
void save()
{
int i = 0;
FILE* pf = fopen("stud.txt", "wb");
if (pf == NULL)
{
perror("error:");
return 1;
}
for (i = 0; i < SIZE; i++)
{
if (fwrite(&stud[i], sizeof(struct Student_type), 1, pf) != 1)
{
printf("file write error\n");
}
}
fclose(pf);
}
int main()
{
int i = 0;
printf("请输入五个学生信息\n");
for (i = 0; i < SIZE; i++)
{
scanf("%d%s%f%f%f", &stud[i].num, stud[i].name, &stud[i].score[0], &stud[i].score[1], &stud[i].score[2]);
stud[i].avg = (stud[i].score[0] + stud[i].score[1] + stud[i].score[2]) / 3.0;
}
save();
FILE* pf = fopen("stud.txt", "rb");
if (pf == NULL)
{
perror("error:");
return 1;
}
for (i = 0; i < SIZE; i++)
{
fread(&stud[i], sizeof(struct Student_type), 1, pf);
printf("%d %s %.2f %.2f %.2f %.2f\n", stud[i].num, stud[i].name, stud[i].score[0], stud[i].score[1], stud[i].score[2], stud[i].avg);
}
fclose(pf);
return 0;
}
其中这道题还有第二问题,如下10.7问
代码如下,其中我的平均成绩排序是从小到大
#define SIZE 5
struct Student_type
{
int num;
char name[10];
float score[3];
float avg;
}stud[SIZE];
void save()
{
FILE* fp;
int i;
if ((fp = fopen("stu_sort.txt", "wb")) == NULL)
{
perror("error:");
return 1;
}
for (i = 0; i < SIZE; i++)
{
if (fwrite(&stud[i], sizeof(struct Student_type), 1, fp) != 1)
{
printf("文件写入错误\n");
}
}
fclose(fp);
}
int main()
{
FILE* pf = fopen("stud.txt", "rb");
if (pf == NULL)
{
perror("1error:");
return 1;
}
int i = 0;
for (i = 0; i < SIZE; i++)
{
fread(&stud[i], sizeof(struct Student_type), 1, pf);
}
fclose(pf);
int j = 0;
struct Student_type temp;
for (i = 0; i < SIZE - 1; i++)
{
for (j = 0; j < SIZE - 1 - i; j++)
{
if (stud[j].avg>stud[j+1].avg)//我这里是从小到大
{
temp = stud[j];
stud[j] = stud[j + 1];
stud[j + 1] = temp;
}
}
}
save();
pf = fopen("stu_sort.txt", "rb");
if (pf == NULL)
{
perror("2error:");
return 1;
}
for (i = 0; i < SIZE; i++)
{
fread(&stud[i], sizeof(struct Student_type), 1, pf);
printf("%d %s %.2f %.2f %.2f %.2f\n", stud[i].num, stud[i].name, stud[i].score[0], stud[i].score[1], stud[i].score[2], stud[i].avg);
}
fclose(pf);
return 0;
}
其中10.8的代码如下
#define SIZE 5
struct Student_type
{
int num;
char name[10];
float score[3];
float avg;
}stud[SIZE + 1];
void save()
{
FILE* pf = fopen("stud_six2.txt", "wb");
if (pf == NULL)
{
perror("error:");
return 1;
}
int i = 0;
for (i = 0; i < SIZE; i++)
{
if (fwrite(&stud[i], sizeof(struct Student_type), 1, pf)!=1)
{
printf("文件写入错误\n");
}
}
fclose(pf);
}
int main()
{
struct Student_type temp, add;
int i = 0, j = 0;
printf("请输入第六个学生信息\n");
scanf("%d%s%f%f%f", &add.num, add.name, &add.score[0], &add.score[1], &add.score[2]);
add.avg = (add.score[0] + add.score[1] + add.score[2]) / 3.0;
//写入第六个就要将信息写入
FILE* pf = fopen("stud3.txt", "ab");
if (pf == NULL)
{
perror("error:");
return 1;
}
fwrite(&add, sizeof(struct Student_type), 1, pf);
fclose(pf);
pf = fopen("stud3.txt", "rb");
for (i = 0; i < SIZE + 1; i++)
{
fread(&stud[i], sizeof(struct Student_type), 1, pf);
}
fclose(pf);
for (i = 0; i < SIZE + 1 - 1; i++)
{
for (j = 0; j < SIZE + 1 - 1 - i; j++)
{
if (stud[j].avg>stud[j+1].avg)
{
temp = stud[j];
stud[j] = stud[j + 1];
stud[j + 1] = temp;
}
}
}
save();
printf("插入一个学生信息后按平均成绩排序\n");
pf = fopen("stud_six2.txt", "rb");
for (i = 0; i < SIZE + 1; i++)
{
fread(&stud[i], sizeof(struct Student_type), 1, pf);
printf("%d %s %.2f %.2f %.2f %.2f\n", stud[i].num, stud[i].name, stud[i].score[0], stud[i].score[1], stud[i].score[2], stud[i].avg);
}
fclose(pf);
return 0;
}