2、编写程序,实现一个具有添加、删除、修改、查询学生信息及退出系统功能的简易版学生管理系统
时间: 2024-06-16 13:08:36 浏览: 123
好的,这是一个常见的小项目。一般来说,学生信息包括学号、姓名、性别、年龄、班级等基本信息。下面是一个简单的学生管理系统的代码示例:
```
#include <iostream>
#include <string>
#include <vector>
using namespace std;
// 定义学生结构体
struct Student {
string id; // 学号
string name; // 姓名
string gender; // 性别
int age; // 年龄
string cls; // 班级
};
// 添加学生信息函数
void addStudent(vector<Student>& students) {
Student stu;
cout << "请输入学号:";
cin >> stu.id;
cout << "请输入姓名:";
cin >> stu.name;
cout << "请输入性别:";
cin >> stu.gender;
cout << "请输入年龄:";
cin >> stu.age;
cout << "请输入班级:";
cin >> stu.cls;
students.push_back(stu);
cout << "添加成功!" << endl;
}
// 删除学生信息函数
void deleteStudent(vector<Student>& students) {
string id;
cout << "请输入要删除学生的学号:";
cin >> id;
for (auto it = students.begin(); it != students.end(); it++) {
if (it->id == id) {
students.erase(it);
cout << "删除成功!" << endl;
return;
}
}
cout << "未找到该学生!" << endl;
}
// 修改学生信息函数
void modifyStudent(vector<Student>& students) {
string id;
cout << "请输入要修改学生的学号:";
cin >> id;
for (auto& stu : students) {
if (stu.id == id) {
cout << "请输入新的姓名(原姓名:" << stu.name << "):";
cin >> stu.name;
cout << "请输入新的性别(原性别:" << stu.gender << "):";
cin >> stu.gender;
cout << "请输入新的年龄(原年龄:" << stu.age << "):";
cin >> stu.age;
cout << "请输入新的班级(原班级:" << stu.cls << "):";
cin >> stu.cls;
cout << "修改成功!" << endl;
return;
}
}
cout << "未找到该学生!" << endl;
}
// 查询学生信息函数
void queryStudent(const vector<Student>& students) {
string id;
cout << "请输入要查询学生的学号:";
cin >> id;
for (const auto& stu : students) {
if (stu.id == id) {
cout << "学号:" << stu.id << endl;
cout << "姓名:" << stu.name << endl;
cout << "性别:" << stu.gender << endl;
cout << "年龄:" << stu.age << endl;
cout << "班级:" << stu.cls << endl;
return;
}
}
cout << "未找到该学生!" << endl;
}
int main() {
vector<Student> students; // 存储学生信息的vector
int choice; // 选择菜单项的变量
while (true) {
// 显示菜单项
cout << "请选择操作:\n"
<< "1. 添加学生\n"
<< "2. 删除学生\n"
<< "3. 修改学生信息\n"
<< "4. 查询学生信息\n"
<< "5. 退出系统\n"
<< "请输入选择(1-5):";
cin >> choice;
switch (choice) {
case 1:
addStudent(students);
break;
case 2:
deleteStudent(students);
break;
case 3:
modifyStudent(students);
break;
case 4:
queryStudent(students);
break;
case 5:
return 0;
default:
cout << "选择错误,请重新输入!" << endl;
}
}
return 0;
}
```
关于相关问题,以下是几个例子:
阅读全文
相关推荐


















