/**
* 实验题目:
* 实现学生记录文件复杂的基本操作
* 实验目的:
* 掌握文件的基本操作及其算法设计
* 实验内容:
* 有若干个学生成绩记录,假设它们存放在结构体数组st中,
* 编写程序,完成如下功能:
* 1、将st数组中的学生记录写入到stud.dat二进制文件中
* 2、将stud.dat文件中的所有学生记录读入到st数组中
* 3、显示st数组中的所有学生记录
* 4、将st数组的学生记录复制到st1数组中,并对st1数组的所有学生记录求平均分
* 5、对st1数组的所有学生记录按平均分递减排序
* 6、将st1数组中的学生记录写入到stud1.dat文件中
* 7、将stud1.dat文件中的学生记录读入到st1数组中
* 8、显示st1数组中的学生记录
* 备注:
* 学生记录文件的主要操作是读写,要实现比较复杂的处理,需要将文件记录
* 读入内存,在处理完毕后再写入文件。
*/
#include <stdio.h>
#include <string.h>
#define N 10 // 最多学生人数
/*-------------设计学生记录类型--------------*/
typedef struct
{
int no; // 学号
char name[10]; // 姓名
int age; // 年龄
char sex[3]; // 性别
int chinese_deg; // 语文成绩
int math_deg; // 数学成绩
int english_deg; // 英语成绩
}stud_type;
typedef struct
{
int no; // 学号
char name[10]; // 姓名
int age; // 年龄
char sex[3]; // 性别
int chinese_deg; // 语文成绩
int math_deg; // 数学成绩
int english_deg; // 英语成绩
double avg; // 平均分
}stud_type1;
/*-------------显示学生记录--------------*/
static void display(stud_type st[], int n)
{
int i;
printf("-------------------学生成绩表-------------------\n");
printf(" 学号 姓名 年龄 性别 语文 数学 英语\n");
for(i = 0; i < n; i++)
printf("%5d%10s%6d%6s%6d%6d%6d\n", st[i].no, st[i].name, st[i].age, st[i].sex,
st[i].chinese_deg, st[i].math_deg, st[i].english_deg);
printf("\n");
}
/*-------------显示求平均分后的学生记录--------------*/
static void display1(stud_type1 st1[], int n)
{
int i;
printf("-----------------------学生成绩表-----------------------\n");
printf(" 学号 姓名 年龄 性别 语文 数学 英语 平均分\n");
for(i = 0; i < n; i++)
printf("%5d%10s%6d%6s%6d%6d%6d%7.1f\n", st1[i].no, st1[i].name, st1[i].age, st1[i].sex,
st1[i].chinese_deg, st1[i].math_deg, st1[i].english_deg, st1[i].avg);
printf("\n");
}
/*-------------将st数组中的学生记录写入到二进制文件stud.dat文件中--------------*/
static void write_file(stud_type st[], int n)
{
int i;
FILE *fp;
fp = fopen("stud.dat", "wb"); // 模式:wb以只写方式打开或新建一个二进制文件,只允许写数据
if(fp == NULL)
{
printf("\t提示:不能创建stud.dat文件\n");
return;
}
for(i = 0; i < n; i++)
fwrite(&st[i], 1, sizeof(stud_type), fp);
fclose(fp);
printf("\t提示:文件stud.dat创建完毕\n");
}
/*-------------将st1数组中的学生记录写入到二进制文件stud1.dat文件中--------------*/
static void write_file1(stud_type1 st1[], int n)
{
int i;
FILE *fp;
fp = fopen("stud1.dat", "wb"); // 模式:wb以只写方式打开或新建一个二进制文件,只允许写数据
if(fp == NULL)
{
printf("\t提示:不能创建stud1.dat文件\n");
return;
}
for(i = 0; i < n; i++)
fwrite(&st1[i], 1, sizeof(stud_type1), fp);
fclose(fp);
printf("\t提示:文件stud1.dat创建完毕\n");
}
/*-------------将stud.dat文件中的n个学生记录读入到st数组中--------------*/
static void read_file(stud_type st[], int &n)
{
FILE *fp;
fp = fopen("stud.dat", "rb");
if(fp == NULL)
{
printf("\t提示:不能打开stud.dat文件\n");
return;
}
n = 0;
while(fread(&st[n], sizeof(stud_type), 1, fp) == 1)
n++;
printf("\t提示:文件stud.dat读取完毕\n");
}
/*-------------将stud1.dat文件中的n个学生记录读入到st1数组中--------------*/
static void read_file1(stud_type1 st1[], int &n)
{
FILE *fp;
fp = fopen("stud1.dat", "rb");
if(fp == NULL)
{
printf("\t提示:不能打开stud.dat文件\n");
return;
}
n = 0;
while(fread(&st1[n], sizeof(stud_type1), 1, fp) == 1)
n++;
printf("\t提示:文件stud1.dat读取完毕\n");
}
/*-------------求学生的平均分并放在st1数组中--------------*/
static void average(stud_type st[], stud_type1 st1[], int n)
{
int i;
for(i = 0; i < n; i++)
{
st1[i].no = st[i].no;
strcpy(st1[i].name, st[i].name);
st1[i].age = st[i].age;
strcpy(st1[i].sex, st[i].sex);
st1[i].chinese_deg = st[i].chinese_deg;
st1[i].math_deg = st[i].math_deg;
st1[i].english_deg = st[i].english_deg;
st1[i].avg = (st1[i].chinese_deg + st1[i].math_deg + st1[i].english_deg) / 3.0;
}
}
/*-------------对st1数组按平均分递减排序--------------*/
static void sort_by_avg(stud_type1 st1[], int n)
{
int i, j;
stud_type1 temp;
for(i = 1; i < n; i++) // 直接插入排序算法
{
temp = st1[i];
for(j = i - 1; j >= 0 && temp.avg > st1[j].avg; j--)
st1[j + 1] = st1[j];
st1[j + 1] = temp;
}
}
int main(void)
{
int n = 8; // 实际学生人数
stud_type st[] = {
{1, "陈华", 20, "男", 78, 99, 84},
{5, "张明", 21, "男", 76, 89, 88},
{8, "王英", 22, "女", 78, 79, 80},
{3, "刘丽", 19, "女", 82, 59, 81},
{2, "许可", 18, "女", 90, 90, 90},
{4, "陈军", 23, "男", 88, 94, 94},
{7, "朱军", 24, "男", 87, 99, 95},
{6, "李鹏", 22, "男", 72, 93, 92},
};
stud_type1 st1[N];
printf("操作过程如下:\n");
printf(" (1)将st数组中学生记录写入stud.dat文件\n");
write_file(st, n);
printf(" (2)将stud.dat文件中的记录读入到st数组中\n");
read_file(st, n);
printf(" (3)显示st数组中的学生记录\n");
display(st, n);
printf(" (4)求学生的平均分并放在st1数组中\n");
average(st, st1, n);
printf(" (5)对st1数组按平均分递减排序\n");
sort_by_avg(st1, n);
printf(" (6)将st1数组中学生记录写入stud1.dat文件\n");
write_file1(st1, n);
printf(" (7)将stud1.dat文件中学生记录读入到st1数组中\n");
read_file1(st1, n);
printf(" (8)显示st1数组中的学生记录\n");
display1(st1, n);
return 0;
}
测试结果:
操作过程如下:
(1)将st数组中学生记录写入stud.dat文件
提示:文件stud.dat创建完毕
(2)将stud.dat文件中的记录读入到st数组中
提示:文件stud.dat读取完毕
(3)显示st数组中的学生记录
-------------------学生成绩表-------------------
学号 姓名 年龄 性别 语文 数学 英语
1 陈华 20 男 78 99 84
5 张明 21 男 76 89 88
8 王英 22 女 78 79 80
3 刘丽 19 女 82 59 81
2 许可 18 女 90 90 90
4 陈军 23 男 88 94 94
7 朱军 24 男 87 99 95
6 李鹏 22 男 72 93 92
(4)求学生的平均分并放在st1数组中
(5)对st1数组按平均分递减排序
(6)将st1数组中学生记录写入stud1.dat文件
提示:文件stud1.dat创建完毕
(7)将stud1.dat文件中学生记录读入到st1数组中
提示:文件stud1.dat读取完毕
(8)显示st1数组中的学生记录
-----------------------学生成绩表-----------------------
学号 姓名 年龄 性别 语文 数学 英语 平均分
7 朱军 24 男 87 99 95 93.7
4 陈军 23 男 88 94 94 92.0
2 许可 18 女 90 90 90 90.0
1 陈华 20 男 78 99 84 87.0
6 李鹏 22 男 72 93 92 85.7
5 张明 21 男 76 89 88 84.3
8 王英 22 女 78 79 80 79.0
3 刘丽 19 女 82 59 81 74.0