利用c++写一个学生信息管理系统
时间: 2023-03-22 13:00:55 浏览: 73
好的,关于利用 C++ 写一个学生信息管理系统,我可以回答这个问题。
一个简单的学生信息管理系统可以包括以下功能:
1. 添加学生信息:包括学生的姓名、学号、性别、年龄、成绩等信息。
2. 删除学生信息:根据学生的学号或姓名进行删除。
3. 修改学生信息:可以修改学生的各项信息。
4. 查询学生信息:可以根据学号、姓名、性别等进行查询。
5. 显示所有学生信息:可以将所有学生的信息全部显示出来。
以下是一个简单的 C++ 程序实现学生信息管理系统的示例代码:
```c++
#include <iostream>
#include <string>
#include <vector>
using namespace std;
// 学生信息类
class Student {
public:
Student(string name, string id, string gender, int age, float score)
: name_(name), id_(id), gender_(gender), age_(age), score_(score) {}
string GetName() const { return name_; }
string GetId() const { return id_; }
string GetGender() const { return gender_; }
int GetAge() const { return age_; }
float GetScore() const { return score_; }
private:
string name_; // 姓名
string id_; // 学号
string gender_; // 性别
int age_; // 年龄
float score_; // 成绩
};
// 学生信息管理类
class StudentManager {
public:
// 添加学生信息
void AddStudent(const Student& student) { students_.push_back(student); }
// 删除学生信息
void DeleteStudent(const string& id) {
for (auto it = students_.begin(); it != students_.end(); ++it) {
if (it->GetId() == id) {
students_.erase(it);
break;
}
}
}
// 修改学生信息
void ModifyStudent(const string& id, const Student& student) {
for (auto& s : students_) {
if (s.GetId() == id) {
s = student;
break;
}
}
}
// 查询学生信息
vector<Student> QueryStudent(const string& keyword) const {
vector<Student> result;
for (const auto& s : students_) {
if (s.GetName() == keyword || s.GetId() == keyword || s.GetGender() == keyword) {
result.push_back(s);
}
}
return result;
}
// 显示所有学生信息
void ShowAllStudents() const {
for (const auto& s : students_) {
cout << "姓名:" << s.GetName() << "\t学号:" << s.GetId() << "\t性别:" << s.GetGender()
<< "\t年龄:" << s.GetAge() << "\t成绩:" << s.GetScore() << endl;
}
}
private:
vector<Student> students_; // 学生信息容器
};
// 主函数
int main() {
StudentManager manager;
// 添加学生信息
manager.AddStudent(Student("张三", "1001", "男", 18, 90));
manager.AddStudent(Student("李四", "1002", "女", 19, 85));