题目描述

一份班级成绩表存储在一个TXT文件中,每行由学号、姓名、数学成绩、英语成绩、C语言成绩组成,如下:

192000101 张天天 86 76 85

192000102 李笑笑 91 88 76

192000130 王维维 88 75 91

编制一个程序,完成下列任务。

(1)构建一个结构体数组,来存储这个成绩表;(4分)

(2)能够正确的从文件读取数据到该结构体数组;(5分)

(3)能够将成绩表输出到屏幕;(4分)

(4)能输出总成绩进行排序并输出排序后的结果;(7分)

(5)程序架构良好、代码清晰、能够全面正确运行;(5分)

源码

  • 单文件版
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <stdio.h>

struct student {
long number;
char name[10];
int math;
int en;
int c;
int sum;
};

void output(struct student a) {
printf("%ld ", a.number);
printf("%s ", a.name);
printf("%d ", a.math);
printf("%d ", a.en);
printf("%d ", a.c);
printf("%d\n", a.sum);
}

void calculate_sum(struct student *b) {
b->sum = b->math + b->en + b->c;
}

void sort(struct student *arr, int size) {
int i, j;
struct student temp;
for (i = 0; i < size - 1; i++) {
for (j = 0; j < size - i - 1; j++) {
if (arr[j].sum > arr[j + 1].sum) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

int main() {
struct student students[3] = {
{123, "Alice", 80, 75, 85, 0},
{456, "Bob", 70, 65, 75, 0},
{789, "Charlie", 85, 90, 95, 0}
};

for (int i = 0; i < 3; i++) {
calculate_sum(&students[i]);
}

printf("Before sorting:\n");
for (int i = 0; i < 3; i++) {
output(students[i]);
}

sort(students, 3);

printf("\nAfter sorting:\n");
for (int i = 0; i < 3; i++) {
output(students[i]);
}

return 0;
}

  • 多文件版(txt文件读取)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// 结构体定义
struct Student {
long number;
char name[50];
int math;
int en;
int c;
int total;
};

// 读取文件数据到结构体数组
void readData(struct Student students[], int *count) {
FILE *file = fopen("grades.txt", "r");
if (file == NULL) {
printf("Unable to open the file.\n");
exit(1);
}

*count = 0;
while (fscanf(file, "%ld%s%d%d%d", &students[*count].number, students[*count].name,
&students[*count].math, &students[*count].en, &students[*count].c) != EOF) {
students[*count].total = students[*count].math + students[*count].en + students[*count].c;
(*count)++;
}

fclose(file);
}

// 输出结构体数组数据到屏幕
void printData(struct Student students[], int count) {
printf("Student Number\tName\tMath\tEnglish\tC Language\tTotal\n");
for (int i = 0; i < count; i++) {
printf("%ld\t%s\t%d\t%d\t%d\t%d\n", students[i].number, students[i].name,
students[i].math, students[i].en, students[i].c, students[i].total);
}
}

// 比较函数用于排序
int compare(const void *a, const void *b) {
struct Student *studentA = (struct Student *)a;
struct Student *studentB = (struct Student *)b;
return studentB->total - studentA->total;
}

int main() {
struct Student students[100]; // 假设最多有100个学生
int count = 0;

readData(students, &count);

printf("Student Records:\n");
printData(students, count);

// 排序并输出排序后的结果
qsort(students, count, sizeof(struct Student), compare);

printf("\nStudent Records after sorting by total score:\n");
printData(students, count);

return 0;
}