用c++语言编写一个学生成绩管理系统(全英文),实现学生基本信息(包括学号、姓名、数学成绩、英语成绩、计算机课成绩、体育成绩)的录入并用类进行封装(其中基本信息都作为类的私有数据成员)、计算学生的总成绩和平均成绩、学生基本信息的删除、学生基本信息的查询、学生基本信息的修改、学生基本信息的统计(通过链表来将所有学生的所有信息按平均成绩从大到小来排序并以一个表格的形式输出在电脑屏幕上),并且实现对学生基本信息的存盘(保存为文本文件,并在该文本文件当中以表格的形式按所有学生的平均成绩从大到小来排序在该文本文件中打印出来所有学生的所有信息)和读盘
时间: 2024-03-10 13:47:54 浏览: 123
Sure! 这是一个典型的学生成绩管理系统,应该用到了很多基本的C++语言特性,包括类的封装、文件读写操作、链表的使用等等。
下面是一个简单的实现,代码中有详细的注释说明。请注意,本代码未做太多错误处理和异常处理,仅供参考。
```c++
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
// 学生基本信息的类
class Student {
private:
string id; // 学号
string name; // 姓名
int math_score; // 数学成绩
int english_score; // 英语成绩
int cs_score; // 计算机课成绩
int pe_score; // 体育成绩
public:
// 构造函数
Student(string id, string name, int math_score, int english_score, int cs_score, int pe_score) {
this->id = id;
this->name = name;
this->math_score = math_score;
this->english_score = english_score;
this->cs_score = cs_score;
this->pe_score = pe_score;
}
// 计算总成绩
int get_total_score() {
return math_score + english_score + cs_score + pe_score;
}
// 计算平均成绩
float get_average_score() {
return get_total_score() / 4.0;
}
// 获取学号
string get_id() {
return id;
}
// 获取姓名
string get_name() {
return name;
}
// 获取数学成绩
int get_math_score() {
return math_score;
}
// 获取英语成绩
int get_english_score() {
return english_score;
}
// 获取计算机课成绩
int get_cs_score() {
return cs_score;
}
// 获取体育成绩
int get_pe_score() {
return pe_score;
}
// 修改数学成绩
void set_math_score(int score) {
math_score = score;
}
// 修改英语成绩
void set_english_score(int score) {
english_score = score;
}
// 修改计算机课成绩
void set_cs_score(int score) {
cs_score = score;
}
// 修改体育成绩
void set_pe_score(int score) {
pe_score = score;
}
// 打印学生信息
void print_info() {
cout << left << setw(10) << id << setw(10) << name << setw(10) << math_score << setw(10) << english_score << setw(10) << cs_score << setw(10) << pe_score << setw(10) << get_total_score() << setw(10) << get_average_score() << endl;
}
};
// 学生信息链表的节点
class Node {
public:
Student* data; // 学生信息指针
Node* next; // 指向下一个节点的指针
// 构造函数
Node(Student* data) {
this->data = data;
next = NULL;
}
// 析构函数
~Node() {
delete data;
}
};
// 学生信息链表
class StudentList {
private:
Node* head; // 链表头指针
int length; // 链表长度
public:
// 构造函数
StudentList() {
head = NULL;
length = 0;
}
// 析构函数
~StudentList() {
Node* p = head;
while (p != NULL) {
Node* q = p->next;
delete p;
p = q;
}
}
// 获取链表长度
int get_length() {
return length;
}
// 添加学生信息
void add_student(Student* student) {
Node* node = new Node(student);
if (head == NULL) {
head = node;
}
else {
Node* p = head;
while (p->next != NULL) {
p = p->next;
}
p->next = node;
}
length++;
}
// 删除学生信息
void remove_student(string id) {
if (head == NULL) {
return;
}
if (head->data->get_id() == id) {
Node* p = head;
head = head->next;
delete p;
length--;
return;
}
Node* p = head;
while (p->next != NULL) {
if (p->next->data->get_id() == id) {
Node* q = p->next;
p->next = q->next;
delete q;
length--;
return;
}
p = p->next;
}
}
// 查找学生信息
Student* find_student(string id) {
Node* p = head;
while (p != NULL) {
if (p->data->get_id() == id) {
return p->data;
}
p = p->next;
}
return NULL;
}
// 修改学生信息
void update_student(string id, int math_score, int english_score, int cs_score, int pe_score) {
Student* student = find_student(id);
if (student != NULL) {
student->set_math_score(math_score);
student->set_english_score(english_score);
student->set_cs_score(cs_score);
student->set_pe_score(pe_score);
}
}
// 统计学生信息
void sort_students() {
Node* p = head;
Node* q = p->next;
while (q != NULL) {
if (q->data->get_average_score() > p->data->get_average_score()) {
p->next = q->next;
q->next = head;
head = q;
q = p->next;
}
else {
Node* r = head;
while (r != p && r->next->data->get_average_score() > q->data->get_average_score()) {
r = r->next;
}
if (r != p) {
p->next = q->next;
q->next = r->next;
r->next = q;
q = p->next;
}
else {
p = p->next;
q = p->next;
}
}
}
}
// 打印学生信息
void print_students() {
cout << left << setw(10) << "ID" << setw(10) << "Name" << setw(10) << "Math" << setw(10) << "English" << setw(10) << "CS" << setw(10) << "PE" << setw(10) << "Total" << setw(10) << "Average" << endl;
Node* p = head;
while (p != NULL) {
p->data->print_info();
p = p->next;
}
}
// 从文件中读取学生信息
void read_students(string filename) {
ifstream fin(filename);
if (!fin) {
return;
}
string id, name;
int math_score, english_score, cs_score, pe_score;
while (fin >> id >> name >> math_score >> english_score >> cs_score >> pe_score) {
Student* student = new Student(id, name, math_score, english_score, cs_score, pe_score);
add_student(student);
}
fin.close();
}
// 将学生信息写入文件
void write_students(string filename) {
ofstream fout(filename);
if (!fout) {
return;
}
sort_students();
Node* p = head;
while (p != NULL) {
fout << p->data->get_id() << " " << p->data->get_name() << " " << p->data->get_math_score() << " " << p->data->get_english_score() << " " << p->data->get_cs_score() << " " << p->data->get_pe_score() << endl;
p = p->next;
}
fout.close();
}
};
int main() {
StudentList list;
list.read_students("students.txt");
while (true) {
cout << "Please choose an operation:" << endl;
cout << "1. Add student" << endl;
cout << "2. Remove student" << endl;
cout << "3. Find student" << endl;
cout << "4. Update student" << endl;
cout << "5. Print students" << endl;
cout << "6. Save students" << endl;
cout << "0. Exit" << endl;
int option;
cin >> option;
if (option == 1) {
string id, name;
int math_score, english_score, cs_score, pe_score;
cout << "Please input student ID, name, math score, english score, CS score and PE score:" << endl;
cin >> id >> name >> math_score >> english_score >> cs_score >> pe_score;
Student* student = new Student(id, name, math_score, english_score, cs_score, pe_score);
list.add_student(student);
cout << "Student added successfully!" << endl;
}
else if (option == 2) {
string id;
cout << "Please input student ID:" << endl;
cin >> id;
list.remove_student(id);
cout << "Student removed successfully!" << endl;
}
else if (option == 3) {
string id;
cout << "Please input student ID:" << endl;
cin >> id;
Student* student = list.find_student(id);
if (student != NULL) {
student->print_info();
}
else {
cout << "Student not found!" << endl;
}
}
else if (option == 4) {
string id;
int math_score, english_score, cs_score, pe_score;
cout << "Please input student ID, math score, english score, CS score and PE score:" << endl;
cin >> id >> math_score >> english_score >> cs_score >> pe_score;
list.update_student(id, math_score, english_score, cs_score, pe_score);
cout << "Student updated successfully!" << endl;
}
else if (option == 5) {
list.sort_students();
list.print_students();
}
else if (option == 6) {
list.write_students("students.txt");
cout << "Students saved successfully!" << endl;
}
else if (option == 0) {
break;
}
}
return 0;
}
```
阅读全文