#include using namespace std;

// 定义学生的结构体 struct Student { string name; int englishScore; int mathScore; int cppScore; double averageScore; Student* next; };

// 建立学生链表 Student* createStudentList(int n) { Student* head = NULL; Student* tail = NULL; for (int i = 0; i < n; i++) { Student* node = new Student; cout << "请输入第" << i + 1 << "个学生的姓名:"; cin >> node->name; cout << "请输入第" << i + 1 << "个学生的英语成绩:"; cin >> node->englishScore; cout << "请输入第" << i + 1 << "个学生的数学成绩:"; cin >> node->mathScore; cout << "请输入第" << i + 1 << "个学生的C++成绩:"; cin >> node->cppScore; node->averageScore = (node->englishScore + node->mathScore + node->cppScore) / 3.0; node->next = NULL; if (head == NULL) { head = node; tail = node; } else { tail->next = node; tail = node; } } return head; }

// 寻找平均成绩最高的学生 string findMaxAverageScoreStudent(Student* head) { string maxName; double maxAverageScore = 0; Student* p = head; while (p != NULL) { if (p->averageScore > maxAverageScore) { maxAverageScore = p->averageScore; maxName = p->name; } p = p->next; } return maxName; }

int main() { int n; cout << "请输入学生人数:"; cin >> n; Student* head = createStudentList(n); string maxName = findMaxAverageScoreStudent(head); cout << "平均成绩最高的学生是:" << maxName << endl; return 0;

本关任务:编写C++程序建立一个学生的单向链表每个节点包括姓名、英语成绩、数学成绩和C++成绩通过链表操作求出平均成绩最高的学生并输出其姓名。输入约定:先输入学生人数然后依次输入每个学生的姓名、英语成绩、数学成绩和C++成绩。并且平均成绩最高的学生只有一位。

原文地址: https://www.cveoy.top/t/topic/hAq3 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录