c语言 8.2 成绩处理 输入一个正整数n,再输入n个学生的学号和数学、英语、C语言成绩,要求用链表实现,包含以下功能: (1) 计算每一个学生的平均成绩。 (2) 输出平均成绩最高的学生的记录。 (3) 按平均成绩从高到低输出学生的成绩单(学号、数学、英语、C语言成绩和平均成绩)
时间: 2023-11-04 09:31:57 浏览: 178
以下是一个示例程序,用链表实现成绩处理,包括计算平均成绩、输出平均成绩最高的学生记录、以及按平均成绩从高到低输出学生的成绩单:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_ID_LEN 20
struct Student {
char id[MAX_ID_LEN + 1];
int math;
int english;
int c;
double average;
struct Student *next;
};
struct Student* addStudent(struct Student *head, char *id, int math, int english, int c);
void calculateAverage(struct Student *head);
struct Student* findMaxAverage(struct Student *head);
void sortStudents(struct Student *head);
void printStudents(struct Student *head);
int main() {
int n, math, english, c;
char id[MAX_ID_LEN + 1];
struct Student *head = NULL;
printf("请输入学生人数:");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
printf("请输入第%d个学生的学号、数学成绩、英语成绩、C语言成绩:", i + 1);
scanf("%s %d %d %d", id, &math, &english, &c);
head = addStudent(head, id, math, english, c);
}
calculateAverage(head);
printf("平均成绩最高的学生记录:\n");
struct Student *maxAverageStudent = findMaxAverage(head);
printf("%s %d %d %d %.2lf\n", maxAverageStudent->id, maxAverageStudent->math,
maxAverageStudent->english, maxAverageStudent->c, maxAverageStudent->average);
sortStudents(head);
printf("按平均成绩从高到低输出学生的成绩单:\n");
printStudents(head);
return 0;
}
struct Student* addStudent(struct Student *head, char *id, int math, int english, int c) {
struct Student *p = (struct Student *)malloc(sizeof(struct Student));
strcpy(p->id, id);
p->math = math;
p->english = english;
p->c = c;
p->average = 0;
p->next = head;
return p;
}
void calculateAverage(struct Student *head) {
struct Student *p = head;
while (p != NULL) {
p->average = (p->math + p->english + p->c) / 3.0;
p = p->next;
}
}
struct Student* findMaxAverage(struct Student *head) {
struct Student *p = head;
struct Student *maxAverageStudent = head;
while (p != NULL) {
if (p->average > maxAverageStudent->average) {
maxAverageStudent = p;
}
p = p->next;
}
return maxAverageStudent;
}
void sortStudents(struct Student *head) {
struct Student *p = head;
while (p != NULL) {
struct Student *q = p->next;
while (q != NULL) {
if (q->average > p->average) {
char id[MAX_ID_LEN + 1];
strcpy(id, p->id);
strcpy(p->id, q->id);
strcpy(q->id, id);
int math = p->math;
p->math = q->math;
q->math = math;
int english = p->english;
p->english = q->english;
q->english = english;
int c = p->c;
p->c = q->c;
q->c = c;
double average = p->average;
p->average = q->average;
q->average = average;
}
q = q->next;
}
p = p->next;
}
}
void printStudents(struct Student *head) {
struct Student *p = head;
while (p != NULL) {
printf("%s %d %d %d %.2lf\n", p->id, p->math, p->english, p->c, p->average);
p = p->next;
}
}
```
程序中定义了一个结构体 Student,里面包含了学号、数学、英语、C语言成绩和平均成绩五个成员,以及一个指向下一个结构体的指针。在主函数中,首先通过 scanf 函数输入学生人数和每个学生的信息,然后通过 addStudent 函数将学生信息添加到链表中。
接着,调用 calculateAverage 函数计算每个学生的平均成绩,调用 findMaxAverage 函数找到平均成绩最高的学生记录,以及调用 sortStudents 函数按平均成绩从高到低排序链表中的学生信息。
最后,调用 printStudents 函数输出排序后的学生信息。值得一提的是,为了方便排序,程序中使用了冒泡排序算法,将平均成绩最高的学生记录放到了链表的第一个位置。
阅读全文