构建一个班级信息链表类,学生信息包括:学号(ID)、姓名(Name)、出生年月(Birth)、性别(Sex)和某门功课成绩(Score)。类的成员函数只需要满足本题要求即可,学生人数由用户决定。为避免大量输入工作,建议学生信息从文件读入,文件格式可自行设定并描述清楚。实现:(1)按成绩降序排列,(2)汇总各分数段人数。分数段划分区间为[90,100],[80,90),[70,80), [60,70), [0,60),(3)列出最高分和最低分的同学信息。(注:不允许使用STL,否则不计成绩)
时间: 2024-03-14 11:48:16 浏览: 58
链表实现学生信息管理
以下是一个班级信息链表类的示例代码,满足题目要求:
```cpp
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
// 学生信息结构体
struct Student {
int ID;
string Name;
string Birth;
string Sex;
int Score;
Student* next;
};
class StudentList {
public:
StudentList(): head(NULL), num(0) {}
~StudentList() { clear(); }
// 从文件中读入学生信息
void readFromFile(const char* filename) {
ifstream fin(filename);
if (!fin) {
cerr << "Failed to open file " << filename << endl;
return;
}
clear();
while (!fin.eof()) {
Student* p = new Student;
fin >> p->ID >> p->Name >> p->Birth >> p->Sex >> p->Score;
addStudent(p);
}
}
// 按成绩降序排列
void sortByScore() {
if (num <= 1) {
return;
}
Student* p = head;
while (p != NULL) {
Student* q = p->next;
while (q != NULL) {
if (p->Score < q->Score) {
swap(p, q);
}
q = q->next;
}
p = p->next;
}
}
// 汇总各分数段人数
void countByScore() {
int count[6] = {0};
Student* p = head;
while (p != NULL) {
if (p->Score >= 90) {
count[0]++;
} else if (p->Score >= 80) {
count[1]++;
} else if (p->Score >= 70) {
count[2]++;
} else if (p->Score >= 60) {
count[3]++;
} else {
count[4]++;
}
p = p->next;
}
count[5] = num - count[0] - count[1] - count[2] - count[3] - count[4];
cout << "Score Summary:" << endl;
cout << "90-100: " << count[0] << endl;
cout << "80-90: " << count[1] << endl;
cout << "70-80: " << count[2] << endl;
cout << "60-70: " << count[3] << endl;
cout << "0-60: " << count[4] << endl;
cout << "Invalid: " << count[5] << endl;
}
// 列出最高分和最低分的同学信息
void printMaxMin() {
if (num == 0) {
cout << "No students found" << endl;
return;
}
Student* p = head;
Student* maxStu = p;
Student* minStu = p;
while (p != NULL) {
if (p->Score > maxStu->Score) {
maxStu = p;
}
if (p->Score < minStu->Score) {
minStu = p;
}
p = p->next;
}
cout << "Max Score:" << endl;
printStudent(maxStu);
cout << "Min Score:" << endl;
printStudent(minStu);
}
// 清空链表
void clear() {
Student* p = head;
while (p != NULL) {
Student* q = p->next;
delete p;
p = q;
}
head = NULL;
num = 0;
}
private:
Student* head;
int num;
// 添加一个学生到链表中
void addStudent(Student* p) {
p->next = head;
head = p;
num++;
}
// 输出一个学生的信息
void printStudent(Student* p) {
cout << "ID: " << p->ID << endl;
cout << "Name: " << p->Name << endl;
cout << "Birth: " << p->Birth << endl;
cout << "Sex: " << p->Sex << endl;
cout << "Score: " << p->Score << endl;
}
// 交换两个学生的位置
void swap(Student*& p, Student*& q) {
Student* temp = p;
p = q;
q = temp;
}
};
int main() {
StudentList list;
list.readFromFile("students.txt");
list.sortByScore();
cout << "Sorted by score:" << endl;
Student* p = list.head;
while (p != NULL) {
list.printStudent(p);
p = p->next;
}
list.countByScore();
list.printMaxMin();
list.clear();
return 0;
}
```
文件格式示例(每行代表一个学生信息):
```
1 Alice 2000-01-01 F 90
2 Bob 2001-02-03 M 85
3 Charlie 2002-03-05 M 80
4 David 2003-04-07 M 95
5 Emily 2004-05-09 F 75
```
阅读全文