帮我用C++写一个学生管理系统的代码
时间: 2024-05-15 12:12:56 浏览: 80
以下是一个简单的学生管理系统的代码,包括添加学生、删除学生、查找学生、修改学生信息和显示所有学生信息等功能:
```c++
#include <iostream>
#include <vector>
#include <string>
using namespace std;
// 学生类
class Student {
public:
Student(string name, int age, string gender, string id) {
m_name = name;
m_age = age;
m_gender = gender;
m_id = id;
}
string GetName() const { return m_name; }
int GetAge() const { return m_age; }
string GetGender() const { return m_gender; }
string GetId() const { return m_id; }
void SetName(string name) { m_name = name; }
void SetAge(int age) { m_age = age; }
void SetGender(string gender) { m_gender = gender; }
void SetId(string id) { m_id = id; }
private:
string m_name;
int m_age;
string m_gender;
string m_id;
};
// 学生管理系统类
class StudentManagementSystem {
public:
// 添加学生
void AddStudent() {
string name, gender, id;
int age;
cout << "请输入学生姓名:";
cin >> name;
cout << "请输入学生年龄:";
cin >> age;
cout << "请输入学生性别:";
cin >> gender;
cout << "请输入学生学号:";
cin >> id;
m_students.push_back(Student(name, age, gender, id));
cout << "已成功添加学生:" << name << endl;
}
// 删除学生
void RemoveStudent() {
string id;
cout << "请输入要删除的学生学号:";
cin >> id;
for (auto it = m_students.begin(); it != m_students.end(); it++) {
if (it->GetId() == id) {
m_students.erase(it);
cout << "已成功删除学生:" << it->GetName() << endl;
return;
}
}
cout << "未找到该学生,无法删除!" << endl;
}
// 查找学生
void FindStudent() {
string id;
cout << "请输入要查找的学生学号:";
cin >> id;
for (auto it = m_students.begin(); it != m_students.end(); it++) {
if (it->GetId() == id) {
cout << "学生信息:" << endl;
cout << "姓名:" << it->GetName() << endl;
cout << "年龄:" << it->GetAge() << endl;
cout << "性别:" << it->GetGender() << endl;
cout << "学号:" << it->GetId() << endl;
return;
}
}
cout << "未找到该学生!" << endl;
}
// 修改学生信息
void ModifyStudent() {
string id;
cout << "请输入要修改的学生学号:";
cin >> id;
for (auto it = m_students.begin(); it != m_students.end(); it++) {
if (it->GetId() == id) {
string name, gender, id;
int age;
cout << "请输入修改后的学生姓名:";
cin >> name;
cout << "请输入修改后的学生年龄:";
cin >> age;
cout << "请输入修改后的学生性别:";
cin >> gender;
cout << "请输入修改后的学生学号:";
cin >> id;
it->SetName(name);
it->SetAge(age);
it->SetGender(gender);
it->SetId(id);
cout << "已成功修改学生信息:" << name << endl;
return;
}
}
cout << "未找到该学生,无法修改!" << endl;
}
// 显示所有学生信息
void ShowAllStudents() {
cout << "所有学生信息如下:" << endl;
for (auto student : m_students) {
cout << "姓名:" << student.GetName() << endl;
cout << "年龄:" << student.GetAge() << endl;
cout << "性别:" << student.GetGender() << endl;
cout << "学号:" << student.GetId() << endl;
cout << "---------------------" << endl;
}
}
private:
vector<Student> m_students;
};
int main() {
StudentManagementSystem sms;
while (true) {
int choice;
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:
sms.AddStudent();
break;
case 2:
sms.RemoveStudent();
break;
case 3:
sms.FindStudent();
break;
case 4:
sms.ModifyStudent();
break;
case 5:
sms.ShowAllStudents();
break;
case 6:
return 0;
default:
cout << "输入有误,请重新输入!" << endl;
break;
}
}
return 0;
}
```
注意:此代码只是一个简单的示例,实际使用时需要根据具体需求进行修改和完善。
阅读全文