构建一个班级信息链表类,学生信息包括:学号(ID)、姓名(Name)、出生年月(Birth)、性别(Sex)和某门功课成绩(Score)。类的成员函数只需要满足本题要求即可,学生人数由用户决定。为避免大量输入工作,建议学生信息从文件读入,文件格式可自行设定并描述清楚。实现:(1)按成绩降序排列,(2)汇总各分数段人数。分数段划分区间为[90,100],[80,90),[70,80), [60,70), [0,60),(3)列出最高分和最低分的同学信息。(注:不允许使用STL)用C++语言
时间: 2024-03-13 16:48:01 浏览: 25
以下是班级信息链表类的实现,包括文件读取、链表节点定义、链表类定义、链表排序和分数段统计、最高分和最低分同学信息列出等功能:
```c++
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
// 学生信息链表节点定义
struct StudentNode {
int ID;
string Name;
string Birth;
string Sex;
int Score;
StudentNode* next;
};
// 学生信息链表类定义
class StudentList {
public:
StudentList(); // 构造函数
~StudentList(); // 析构函数
void loadFromFile(string filename); // 从文件中读取学生信息
void sort(); // 按成绩降序排列
void countScore(); // 汇总各分数段人数
void printHighest(); // 列出最高分的同学信息
void printLowest(); // 列出最低分的同学信息
void printAll(); // 列出所有学生信息
private:
StudentNode* head;
};
// 构造函数
StudentList::StudentList() {
head = new StudentNode;
head->next = NULL;
}
// 析构函数
StudentList::~StudentList() {
StudentNode* p = head;
while (p) {
StudentNode* temp = p;
p = p->next;
delete temp;
}
}
// 从文件中读取学生信息
void StudentList::loadFromFile(string filename) {
ifstream infile(filename);
if (!infile) {
cout << "Failed to open file: " << filename << endl;
return;
}
StudentNode* p = head;
while (!infile.eof()) {
StudentNode* node = new StudentNode;
infile >> node->ID >> node->Name >> node->Birth >> node->Sex >> node->Score;
node->next = NULL;
p->next = node;
p = p->next;
}
infile.close();
}
// 按成绩降序排列
void StudentList::sort() {
if (!head->next) {
return;
}
bool swapped = true;
while (swapped) {
swapped = false;
StudentNode* p = head->next;
while (p->next) {
if (p->Score < p->next->Score) {
swapped = true;
int tempID = p->ID;
string tempName = p->Name;
string tempBirth = p->Birth;
string tempSex = p->Sex;
int tempScore = p->Score;
p->ID = p->next->ID;
p->Name = p->next->Name;
p->Birth = p->next->Birth;
p->Sex = p->next->Sex;
p->Score = p->next->Score;
p->next->ID = tempID;
p->next->Name = tempName;
p->next->Birth = tempBirth;
p->next->Sex = tempSex;
p->next->Score = tempScore;
}
p = p->next;
}
}
}
// 汇总各分数段人数
void StudentList::countScore() {
int scoreCount[6] = {0, 0, 0, 0, 0, 0};
StudentNode* p = head->next;
while (p) {
if (p->Score >= 90 && p->Score <= 100) {
scoreCount[0]++;
} else if (p->Score >= 80 && p->Score < 90) {
scoreCount[1]++;
} else if (p->Score >= 70 && p->Score < 80) {
scoreCount[2]++;
} else if (p->Score >= 60 && p->Score < 70) {
scoreCount[3]++;
} else if (p->Score >= 0 && p->Score < 60) {
scoreCount[4]++;
} else {
scoreCount[5]++;
}
p = p->next;
}
cout << "Score Summary:" << endl;
cout << "[90,100]: " << scoreCount[0] << endl;
cout << "[80,90): " << scoreCount[1] << endl;
cout << "[70,80): " << scoreCount[2] << endl;
cout << "[60,70): " << scoreCount[3] << endl;
cout << "[0,60): " << scoreCount[4] << endl;
cout << "Invalid: " << scoreCount[5] << endl;
}
// 列出最高分的同学信息
void StudentList::printHighest() {
StudentNode* p = head->next;
if (!p) {
return;
}
while (p->next) {
p = p->next;
}
cout << "Highest Score: " << p->Score << endl;
cout << "Student ID: " << p->ID << endl;
cout << "Student Name: " << p->Name << endl;
cout << "Student Birth: " << p->Birth << endl;
cout << "Student Sex: " << p->Sex << endl;
}
// 列出最低分的同学信息
void StudentList::printLowest() {
StudentNode* p = head->next;
if (!p) {
return;
}
StudentNode* lowest = p;
while (p->next) {
if (p->next->Score < lowest->Score) {
lowest = p->next;
}
p = p->next;
}
cout << "Lowest Score: " << lowest->Score << endl;
cout << "Student ID: " << lowest->ID << endl;
cout << "Student Name: " << lowest->Name << endl;
cout << "Student Birth: " << lowest->Birth << endl;
cout << "Student Sex: " << lowest->Sex << endl;
}
// 列出所有学生信息
void StudentList::printAll() {
cout << "ID\tName\tBirth\tSex\tScore" << endl;
StudentNode* p = head->next;
while (p) {
cout << p->ID << "\t" << p->Name << "\t" << p->Birth << "\t" << p->Sex << "\t" << p->Score << endl;
p = p->next;
}
}
int main() {
StudentList list;
list.loadFromFile("students.txt");
list.sort();
list.countScore();
list.printHighest();
list.printLowest();
list.printAll();
return 0;
}
```
以上代码实现了班级信息链表类,包括从文件读取学生信息、按成绩降序排列、汇总各分数段人数、列出最高分和最低分的同学信息、列出所有学生信息等功能。用户可以根据自己的需要调用这些函数来处理学生信息。