使用c++学生成绩管理系统
时间: 2023-12-31 08:22:10 浏览: 26
以下是使用C++实现学生成绩管理系统的示例代码:
```cpp
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
// 定义学生类
class Student {
public:
string id; // 学号
string name; // 姓名
int score1; // 大学英语一成绩
int score2; // 大学英语二成绩
int score3; // 大学英语三成绩
// 构造函数
Student(string id, string name, int score1, int score2, int score3) {
this->id = id;
this->name = name;
this->score1 = score1;
this->score2 = score2;
this->score3 = score3;
}
// 显示学生信息
void display() {
cout << "学号:" << id << ",姓名:" << name << ",大学英语一成绩:" << score1 << ",大学英语二成绩:" << score2 << ",大学英语三成绩:" << score3 << endl;
}
};
// 定义学生管理系统类
class StudentSystem {
private:
vector<Student> students; // 学生列表
public:
// 添加学生
void addStudent() {
string id, name;
int score1, score2, score3;
cout << "请输入学号:";
cin >> id;
cout << "请输入姓名:";
cin >> name;
cout << "请输入大学英语一成绩:";
cin >> score1;
cout << "请输入大学英语二成绩:";
cin >> score2;
cout << "请输入大学英语三成绩:";
cin >> score3;
Student student(id, name, score1, score2, score3);
students.push_back(student);
cout << "添加成功!" << endl;
}
// 显示所有学生信息
void displayAllStudents() {
for (int i = 0; i < students.size(); i++) {
students[i].display();
}
}
// 根据学号查找学生
Student* findStudentById(string id) {
for (int i = 0; i < students.size(); i++) {
if (students[i].id == id) {
return &students[i];
}
}
return NULL;
}
// 修改学生信息
void modifyStudent() {
string id;
cout << "请输入要修改的学生的学号:";
cin >> id;
Student* student = findStudentById(id);
if (student == NULL) {
cout << "未找到该学生!" << endl;
return;
}
cout << "请输入新的姓名:";
cin >> student->name;
cout << "请输入新的大学英语一成绩:";
cin >> student->score1;
cout << "请输入新的大学英语二成绩:";
cin >> student->score2;
cout << "请输入新的大学英语三成绩:";
cin >> student->score3;
cout << "修改成功!" << endl;
}
// 删除学生
void deleteStudent() {
string id;
cout << "请输入要删除的学生的学号:";
cin >> id;
for (vector<Student>::iterator it = students.begin(); it != students.end(); it++) {
if (it->id == id) {
students.erase(it);
cout << "删除成功!" << endl;
return;
}
}
cout << "未找到该学生!" << endl;
}
// 按照总成绩排序并显示
void sortByTotalScore() {
vector<Student> temp = students;
sort(temp.begin(), temp.end(), [](Student a, Student b) {
return a.score1 + a.score2 + a.score3 > b.score1 + b.score2 + b.score3;
});
for (int i = 0; i < temp.size(); i++) {
temp[i].display();
}
}
};
int main() {
StudentSystem system;
int choice;
while (true) {
cout << "请选择操作:" << endl;
cout << "1.添加学生" << endl;
cout << "2.显示所有学生信息" << endl;
cout << "3.修改学生信息" << endl;
cout << "4.删除学生" << endl;
cout << "5.按照总成绩排序并显示" << endl;
cout << "6.退出" << endl;
cin >> choice;
switch (choice) {
case 1:
system.addStudent();
break;
case 2:
system.displayAllStudents();
break;
case 3:
system.modifyStudent();
break;
case 4:
system.deleteStudent();
break;
case 5:
system.sortByTotalScore();
break;
case 6:
return 0;
default:
cout << "无效的操作!" << endl;
break;
}
}
return 0;
}
```