用c++设计一个学生信息管理系统,学生对象至少要包含:学号、姓名、性别、成绩1、成绩2、总成绩等信息。要求实现以下功能: 1.总成绩要求自动计算; 2.查询:分别给定学生学号、姓名、性别,能够查找到学生的基本信息(要求至少用两种查找算法实现); 3. 排序:分别按学生的学号、成绩1、成绩2、总成绩进行排序(要求至少用两种排序算法实现)。
时间: 2023-07-10 10:23:02 浏览: 95
C++课程设计学生成绩管理系统样本.doc
好的,以下是一个简单的学生信息管理系统的C++代码,包含了学生类的定义、各种功能函数的实现,以及主函数:
```c++
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
class Student {
public:
string id; // 学号
string name; // 姓名
char gender; // 性别
int score1; // 成绩1
int score2; // 成绩2
int totalScore; // 总成绩
Student(string _id, string _name, char _gender, int _score1, int _score2) {
id = _id;
name = _name;
gender = _gender;
score1 = _score1;
score2 = _score2;
totalScore = score1 + score2;
}
};
vector<Student> students; // 学生信息存储在vector中
// 根据学号查找学生
Student* searchById(string id) {
for (int i = 0; i < students.size(); i++) {
if (students[i].id == id) {
return &students[i];
}
}
return nullptr;
}
// 根据姓名查找学生
vector<Student*> searchByName(string name) {
vector<Student*> results;
for (int i = 0; i < students.size(); i++) {
if (students[i].name == name) {
results.push_back(&students[i]);
}
}
return results;
}
// 根据性别查找学生
vector<Student*> searchByGender(char gender) {
vector<Student*> results;
for (int i = 0; i < students.size(); i++) {
if (students[i].gender == gender) {
results.push_back(&students[i]);
}
}
return results;
}
// 按学号排序
void sortByid() {
sort(students.begin(), students.end(), [](const Student& a, const Student& b) {
return a.id < b.id;
});
}
// 按成绩1排序
void sortByScore1() {
sort(students.begin(), students.end(), [](const Student& a, const Student& b) {
return a.score1 > b.score1;
});
}
// 按成绩2排序
void sortByScore2() {
sort(students.begin(), students.end(), [](const Student& a, const Student& b) {
return a.score2 > b.score2;
});
}
// 按总成绩排序
void sortByTotalScore() {
sort(students.begin(), students.end(), [](const Student& a, const Student& b) {
return a.totalScore > b.totalScore;
});
}
int main() {
// 添加一些学生信息
students.push_back(Student("001", "Alice", 'F', 85, 90));
students.push_back(Student("002", "Bob", 'M', 80, 95));
students.push_back(Student("003", "Charlie", 'M', 90, 85));
students.push_back(Student("004", "David", 'M', 75, 80));
students.push_back(Student("005", "Eva", 'F', 95, 90));
// 按学号排序
sortByid();
// 查询学生信息
string id = "002";
Student* s = searchById(id);
if (s != nullptr) {
cout << "ID: " << s->id << ", Name: " << s->name << ", Gender: " << s->gender << ", Score1: " << s->score1 << ", Score2: " << s->score2 << ", Total Score: " << s->totalScore << endl;
}
string name = "Charlie";
vector<Student*> results = searchByName(name);
for (int i = 0; i < results.size(); i++) {
cout << "ID: " << results[i]->id << ", Name: " << results[i]->name << ", Gender: " << results[i]->gender << ", Score1: " << results[i]->score1 << ", Score2: " << results[i]->score2 << ", Total Score: " << results[i]->totalScore << endl;
}
char gender = 'F';
results = searchByGender(gender);
for (int i = 0; i < results.size(); i++) {
cout << "ID: " << results[i]->id << ", Name: " << results[i]->name << ", Gender: " << results[i]->gender << ", Score1: " << results[i]->score1 << ", Score2: " << results[i]->score2 << ", Total Score: " << results[i]->totalScore << endl;
}
// 按成绩排序
sortByScore1();
for (int i = 0; i < students.size(); i++) {
cout << "ID: " << students[i].id << ", Name: " << students[i].name << ", Gender: " << students[i].gender << ", Score1: " << students[i].score1 << ", Score2: " << students[i].score2 << ", Total Score: " << students[i].totalScore << endl;
}
sortByScore2();
for (int i = 0; i < students.size(); i++) {
cout << "ID: " << students[i].id << ", Name: " << students[i].name << ", Gender: " << students[i].gender << ", Score1: " << students[i].score1 << ", Score2: " << students[i].score2 << ", Total Score: " << students[i].totalScore << endl;
}
sortByTotalScore();
for (int i = 0; i < students.size(); i++) {
cout << "ID: " << students[i].id << ", Name: " << students[i].name << ", Gender: " << students[i].gender << ", Score1: " << students[i].score1 << ", Score2: " << students[i].score2 << ", Total Score: " << students[i].totalScore << endl;
}
return 0;
}
```
这个程序使用了一个vector来存储所有学生信息,然后实现了根据学号、姓名、性别进行查找学生,以及按学号、成绩1、成绩2、总成绩进行排序的功能。
阅读全文