(1)有5个学生,每个学生有3门课程的成绩,从键盘输入以上数据(包括学号、姓名、3门课程成绩),计算出平均成绩,将原有数据和计算出的平均分数存放在磁盘文件stud中。
设5名学生的学号、姓名和3门课程成绩如下:
99101 Wang 89,98,67.5
99103 Li 60,80,90
99106 Fun 75.5,91.5,99
99110 Ling 100,50,62.5
99113 Yuan 58,68,71
在向文件stud写入数据后,应检查验证stud文件中的内容是否正确。
(2)将(1)题stud文件中的学生数据按平均分进行排序处理,将已排序的学生数据存入一个新文件stu_sort中。
在向文件stu_sort写人数据后,应检查验证stu_sort文件中的内容是否正确。
ps:第一题见31
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#pragma warning(disable:4996)
// 学生结构体
typedef struct Student {
char id[10];
char name[20];
double scores[3];
double average;
} Student;
// 计算平均成绩
void calculateAverage(Student* s) {
s->average = (s->scores[0] + s->scores[1] + s->scores[2]) / 3;
}
// 交换两个学生结构体
void swap(Student* a, Student* b) {
Student temp = *a;
*a = *b;
*b = temp;
}
// 冒泡排序
void bubbleSort(Student* students, int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (students[j].average > students[j + 1].average) {
swap(&students[j], &students[j + 1]);
}
}
}
}
// 将学生数据写入文件
void writeToFile(Student* students, int n, char* filename) {
FILE* fp = fopen(filename, "w");
if (fp == NULL) {
perror("Error opening file");
exit(1);
}
for (int i = 0; i < n; i++) {
fprintf(fp, "%s %s %.2lf %.2lf %.2lf %.2lf\n", students[i].id, students[i].name,
students[i].scores[0], students[i].scores[1], students[i].scores[2], students[i].average);
}
fclose(fp);
}
// 从文件读取学生数据
int readStudentsFromFile(Student* students, char* filename) {
FILE* fp = fopen(filename, "r");
if (fp == NULL) {
perror("Error opening file");
exit(1);
}
int count = 0;
while (fscanf(fp, "%s %s %lf %lf %lf", students[count].id, students[count].name,
&students[count].scores[0], &students[count].scores[1], &students[count].scores[2]) == 5) {
calculateAverage(&students[count]);
count++;
}
fclose(fp);
return count;
}
// 插入新学生数据并保持顺序
void insertStudent(Student* students, int* n, Student newStudent) {
int i;
for (i = 0; i < *n; i++) {
if (newStudent.average > students[i].average) {
break;
}
}
for (int j = *n; j > i; j--) {
students[j] = students[j - 1];
}
students[i] = newStudent;
(*n)++;
}
int main() {
// (1)
Student students[5];
students[0].scores[0] = 89;
students[0].scores[1] = 98;
students[0].scores[2] = 67.5;
strcpy(students[0].id, "99101");
strcpy(students[0].name, "Wang");
students[1].scores[0] = 60;
students[1].scores[1] = 80;
students[1].scores[2] = 90;
strcpy(students[1].id, "99103");
strcpy(students[1].name, "Li");
students[2].scores[0] = 75.5;
students[2].scores[1] = 91.5;
students[2].scores[2] = 99;
strcpy(students[2].id, "99106");
strcpy(students[2].name, "Fun");
students[3].scores[0] = 100;
students[3].scores[1] = 50;
students[3].scores[2] = 62.5;
strcpy(students[3].id, "99110");
strcpy(students[3].name, "Ling");
students[4].scores[0] = 58;
students[4].scores[1] = 68;
students[4].scores[2] = 71;
strcpy(students[4].id, "99113");
strcpy(students[4].name, "Yuan");
for (int i = 0; i < 5; i++) {
calculateAverage(&students[i]);
}
writeToFile(students, 5, "stud");
// (2)
Student sortedStudents[5];
int count = readStudentsFromFile(sortedStudents, "stud");
bubbleSort(sortedStudents, count);
writeToFile(sortedStudents, count, "stu_sort");
// (3)
Student newStudent;
newStudent.scores[0] = 90;
newStudent.scores[1] = 95;
newStudent.scores[2] = 60;
strcpy(newStudent.id, "99108");
strcpy(newStudent.name, "Xin");
calculateAverage(&newStudent);
Student stuNew[6];
count = readStudentsFromFile(stuNew, "stu_sort");
insertStudent(stuNew, &count, newStudent);
writeToFile(stuNew, count, "stu_new");
// (4)
// 这里直接将stu_new中的数据复制到stu_sort(实际操作可优化)
count = readStudentsFromFile(stuNew, "stu_new");
writeToFile(stuNew, count, "stu_sort");
return 0;
}