任务描述
本关任务与第
2
关任务相似。(按格式读写文件存取学生信息)
相关知识(略)
编程要求
本关的编程任务是补全step2/fileBin.c
文件中ReadStudInfo
函数和SaveResult
函数,以实现按数据块使用FILE结构存取学生信息的功能。具体要求如下:
1.ReadStudInfo
函数功能:实现从二进制文件中读取学生的基本信息。函数ReadStudInfo
函数原型为:
void ReadStudInfo(const char *fileName, STUDENT stud[]);
其中:
- 参数
fileName
是一个二进制格式的文件,位于当前目录,即与源程序文件在同一文件夹下。文件中首先是以二进制数据形式写入的学生人数(整数n
,不超过30
)和课程数量(整数n
,不超过5
),然后是n
个学生的信息,学生信息在结构STUDENT
中定义:struct student { long studentID; char studentName[20]; char sex[4]; struct { int year; int month; int day; }birthday; float score[COURSE_MAXNUM]; float total; float average; int rank; }; typedef struct student STUDENT;
- 文件中存储了 total,average,rank 三项信息,但均为初始值0。
- 函数
ReadStudInfo
的第二个参数stud
存放读取出来的学生信息。
2.SaveResult
函数功能:实现从二进制格式文件中读取学生的基本信息。函数SaveResult
函数原型为:
void SaveResult(const char *fileName, STUDENT stud[],int n,int m);
其中:
- 参数
fileName
是一个二进制格式文件,位于当前目录,即与源程序文件在同一文件夹下。 - 文件中首先要求以二进制形式写入学生人数(整数
n
,不超过30
)和课程数量(整数n
,不超过5
),然后是n
个学生的信息。n
个学生的信息存放在第二个参数stud
开始的n
个结构体数据单元中。 - 每个学生的信息包括学号、姓名、性别、出生日期,若干门课程的成绩、总分、平均分、名次。
测试说明
本关的测试文件是
step3/main.c
,其中将会调用你在step3/fileBin.c
文件中完成的ReadStudInfo
函数和SaveResult
函数。除此之后,step3/main.c
中还对学生信息进行了处理:包括计算总分、平均分和名次。其次还实现了输入测试数据、输出测试结果、写入测试文件(student.dat
)、读取测试文件(result.dat
):
1.调用函数Input()
将标准输入的数据读入内存,然后调用SaveStudInfo()
函数将数据再写入到student.dat
文本文件,以便为**ReadStudInfo()
**函数从文本文件读取学生信息准备测试数据。
2.调用函数ReadResult()
将SaveResult()
函数保存的result.dat
中的学生信息读入内存,并调用Print()
函数进行输出。**SaveResult()
**函数需要将计算出总分、平均分和名次信息的学生信息存入result.dat
。
3.student.dat
的示例如下:
4.result.dat
的示例如下:
平台会对你编写的代码进行测试:
测试样例
预期文件内容(二进制打开)
代码实现
//main.cpp
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#define COURSE_MAXNUM 5
struct student
{
long studentID;
char studentName[20];
char sex[4];
struct {
int year;
int month;
int day;
}birthday;
float score[COURSE_MAXNUM];
float total;
float average;
int rank;
};
typedef struct student STUDENT;
extern void ReadStudInfo(const char* fileName, STUDENT stud[]);
extern void SaveResult(const char* fileName, STUDENT stud[], int n, int m);
void Input(STUDENT* stud, int n, int m);
void Print(STUDENT* stud, int n, int m);
void TotalAndAverage(STUDENT* stud, int n, int m);
void RankByTotal(STUDENT* stud, int n, int m);
void SaveStudInfo(const char* fileName, STUDENT* stud, int n, int m);
void ReadResult(const char* fileName, STUDENT* stud);
int main()
{
int n = 0, m = 0;
STUDENT* stud = NULL;
(void)scanf("%d%d", &n, &m);
stud = (STUDENT*)malloc(n * sizeof(STUDENT));
if (NULL == stud)
{
printf("%s", strerror(errno));
return 0;
}
Input(stud, n, m);
SaveStudInfo("student.dat", stud, n, m);
free(stud);
stud = NULL;
stud = (STUDENT*)malloc(n * sizeof(STUDENT));
if (NULL == stud)
{
printf("%s", strerror(errno));
return 0;
}
ReadStudInfo("student.dat", stud);
TotalAndAverage(stud, n, m);
RankByTotal(stud, n, m);
SaveResult("result.dat", stud, n, m);
free(stud);
stud = NULL;
stud = (STUDENT*)malloc(n * sizeof(STUDENT));
if (NULL == stud)
{
printf("%s", strerror(errno));
return 0;
}
ReadResult("result.dat", stud);
Print(stud, n, m);
free(stud);
stud = NULL;
return 0;
}
void Input(STUDENT* stud, int n, int m)
{
int i = 0;
for (i = 0; i < n; i++)
{
(void)scanf("%ld", &stud[i].studentID);
(void)scanf("%s", stud[i].studentName);
(void)scanf("%s", stud[i].sex);
(void)scanf("%d-%d-%d", &stud[i].birthday.year, &stud[i].birthday.month, &stud[i].birthday.day);
for (int j = 0; j < m; j++)
(void)scanf("%f", &stud[i].score[j]);
stud[i].total = 0;
stud[i].average = 0;
stud[i].rank = 0;
}
}
void Print(STUDENT* stud, int n, int m)
{
printf("%8s%12s%4s%12s%10s%10s%10s%10s%10s%10s%5s\n",
" NO. ", "Name", "Sex", " Birthday ", "Computer", "English", "Math", "Music", "Total", "Average", "Rank");
for (int i = 0; i < n; i++)
{
printf("%8ld", stud[i].studentID);
printf("%15s", stud[i].studentName);
printf("%5s", stud[i].sex);
printf("%6d-%02d-%02d", stud[i].birthday.year, stud[i].birthday.month, stud[i].birthday.day);
for (int j = 0; j < m; j++)
{
printf("%10.0f", stud[i].score[j]);
}
printf("%10.0f", stud[i].total);
printf("%10.0f", stud[i].average);
printf("%5d\n", stud[i].rank);
}
}
void TotalAndAverage(STUDENT* stud, int n, int m)
{
for (int i = 0; i < n; i++)
{
stud[i].total = 0;
for (int j = 0; j < m; j++)
stud[i].total += stud[i].score[j];
stud[i].average = stud[i].total / 4;
}
}
void RankByTotal(STUDENT* stud, int n, int m)
{
for (int i = 0; i < n - 1; i++)
{
int k = i;
for (int j = i + 1; j < n; j++)
{
if (stud[j].total > stud[k].total)
{
k = j;
}
}
if (k != i)
{
STUDENT temp = stud[i];
stud[i] = stud[k];
stud[k] = temp;
}
stud[i].rank = i + 1;
if (i > 0 && stud[i].total == stud[i - 1].total)
stud[i].rank = stud[i - 1].rank;
}
stud[n - 1].rank = n;
if (n - 1 > 0 && stud[n - 1].total == stud[n - 2].total)
stud[n - 1].rank = stud[n - 2].rank;
}
void SaveStudInfo(const char* fileName, STUDENT* stud, int n, int m)
{
FILE* fp = fopen(fileName, "w");
if (fp == NULL)
{
printf("Failure to open %s!\n", fileName);
exit(0);
}
fwrite(&n, sizeof(n), 1, fp);
fwrite(&m, sizeof(m), 1, fp);
fwrite(stud, sizeof(STUDENT), n, fp);
fclose(fp);
}
void ReadResult(const char* fileName, STUDENT* stud)
{
FILE* fp = fopen(fileName, "r");
if (fp == NULL)
{
printf("Failure to open %s!\n", fileName);
exit(0);
}
int n = 0, m = 0;
fread(&n, sizeof(n), 1, fp);
fread(&m, sizeof(m), 1, fp);
fread(stud, sizeof(STUDENT), n, fp);
fclose(fp);
}
//filetxt.cpp
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#define COURSE_MAXNUM 5
struct student
{
long studentID;
char studentName[20];
char sex[4];
struct {
int year;
int month;
int day;
}birthday;
float score[COURSE_MAXNUM];
float total;
float average;
int rank;
};
typedef struct student STUDENT;
extern void ReadStudInfo(const char* fileName, STUDENT stud[]);
extern void SaveResult(const char* fileName, STUDENT stud[], int n, int m);
void ReadStudInfo(const char* fileName, STUDENT stud[])
{
FILE* fp = fopen(fileName, "r");
if (fp == NULL)
{
printf("Failure to open %s!\n", fileName);
exit(0);
}
int n = 0, m = 0;
fread(&n, sizeof(n), 1, fp);
fread(&m, sizeof(m), 1, fp);
fread(stud, sizeof(STUDENT), sizeof(n) - 1, fp);
fclose(fp);
}
void SaveResult(const char* fileName, STUDENT stud[], int n, int m)
{
FILE* fp = fopen(fileName, "w");
if (fp == NULL)
{
printf("Failure to open %s!\n", fileName);
exit(0);
}
fwrite(&n, sizeof(n), 1, fp);
fwrite(&m, sizeof(m), 1, fp);
fwrite(stud, sizeof(STUDENT), n, fp);
fclose(fp);
}