构建一个班级信息链表类,学生信息包括:学号(ID)、姓名(Name)、出生年月(Birth)、性别(Sex)和某门功课成绩(Score)。类的成员函数只需要满足本题要求即可,学生人数由用户决定。为避免大量输入工作,建议学生信息从文件读入,文件格式可自行设定并描述清楚。实现:(1)按成绩降序排列,(2)汇总各分数段人数。分数段划分区间为[90,100],[80,90),[70,80), [60,70), [0,60),(3)列出最高分和最低分的同学信息。(注:不允许使用STL,否则不计成绩)
时间: 2024-03-11 14:46:44 浏览: 15
好的,我可以帮你解决这个问题。以下是班级信息链表类的实现,包括文件读入、成绩降序排列、分数段汇总和最高分、最低分同学信息的列出:
```cpp
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct Student {
int ID;
string Name;
string Birth;
string Sex;
int Score;
Student* next;
};
class StudentList {
private:
Student* head;
int size;
public:
StudentList() {
head = nullptr;
size = 0;
}
void insert(int id, string name, string birth, string sex, int score) {
Student* newStudent = new Student;
newStudent->ID = id;
newStudent->Name = name;
newStudent->Birth = birth;
newStudent->Sex = sex;
newStudent->Score = score;
newStudent->next = nullptr;
if (head == nullptr) {
head = newStudent;
}
else {
Student* current = head;
while (current->next != nullptr) {
current = current->next;
}
current->next = newStudent;
}
size++;
}
void readFromFile(string filename) {
ifstream input(filename);
if (!input) {
cout << "Error opening file." << endl;
return;
}
int id, score;
string name, birth, sex;
while (input >> id >> name >> birth >> sex >> score) {
insert(id, name, birth, sex, score);
}
input.close();
}
void sortByScore() {
for (Student* i = head; i != nullptr; i = i->next) {
for (Student* j = i->next; j != nullptr; j = j->next) {
if (j->Score > i->Score) {
int tempID = i->ID;
string tempName = i->Name;
string tempBirth = i->Birth;
string tempSex = i->Sex;
int tempScore = i->Score;
i->ID = j->ID;
i->Name = j->Name;
i->Birth = j->Birth;
i->Sex = j->Sex;
i->Score = j->Score;
j->ID = tempID;
j->Name = tempName;
j->Birth = tempBirth;
j->Sex = tempSex;
j->Score = tempScore;
}
}
}
}
void countByScore() {
int count[6] = {0};
for (Student* i = head; i != nullptr; i = i->next) {
if (i->Score >= 90) {
count[0]++;
}
else if (i->Score >= 80) {
count[1]++;
}
else if (i->Score >= 70) {
count[2]++;
}
else if (i->Score >= 60) {
count[3]++;
}
else {
count[4]++;
}
}
count[5] = size - count[0] - count[1] - count[2] - count[3] - count[4];
cout << "90-100: " << count[0] << endl;
cout << "80-89: " << count[1] << endl;
cout << "70-79: " << count[2] << endl;
cout << "60-69: " << count[3] << endl;
cout << "0-59: " << count[4] << endl;
cout << "Total: " << count[5] << endl;
}
void printHighestAndLowest() {
Student* highest = head;
Student* lowest = head;
for (Student* i = head->next; i != nullptr; i = i->next) {
if (i->Score > highest->Score) {
highest = i;
}
if (i->Score < lowest->Score) {
lowest = i;
}
}
cout << "Highest: " << highest->ID << " " << highest->Name << " " << highest->Birth << " " << highest->Sex << " " << highest->Score << endl;
cout << "Lowest: " << lowest->ID << " " << lowest->Name << " " << lowest->Birth << " " << lowest->Sex << " " << lowest->Score << endl;
}
};
```
班级信息链表类包含一个 `Student` 结构体和一个 `StudentList` 类。`Student` 结构体表示学生信息,`next` 指向下一个学生结点。`StudentList` 类包含头结点 `head` 和链表大小 `size`,同时定义了一些成员函数来实现题目要求:
- `insert` 函数:将一个学生信息插入链表中。
- `readFromFile` 函数:从文件中读入学生信息并插入到链表中。
- `sortByScore` 函数:按成绩降序排列链表。
- `countByScore` 函数:汇总各分数段人数。
- `printHighestAndLowest` 函数:列出最高分和最低分的同学信息。
以上各函数的具体实现细节见代码注释。以下是一个使用示例:
```cpp
int main() {
StudentList list;
list.readFromFile("students.txt");
list.sortByScore();
list.countByScore();
list.printHighestAndLowest();
return 0;
}
```
这个示例用 `readFromFile` 函数从文件 `students.txt` 中读入学生信息,然后用 `sortByScore` 函数按成绩降序排列,用 `countByScore` 函数汇总各分数段人数,用 `printHighestAndLowest` 函数列出最高分和最低分的同学信息。