用C++写一个学生管理系统
时间: 2023-09-03 18:08:39 浏览: 50
好的,下面是一个简单的学生管理系统的 C++ 代码示例:
```c++
#include <iostream>
#include <string>
using namespace std;
const int MAX_STUDENTS = 100; // 最大学生数
// 学生结构体
struct Student {
string name;
int age;
char gender;
float score;
};
// 学生管理系统类
class StudentManagementSystem {
private:
Student students[MAX_STUDENTS]; // 学生数组
int count; // 当前学生数
public:
StudentManagementSystem() {
count = 0;
}
// 添加学生
void addStudent() {
if (count >= MAX_STUDENTS) {
cout << "已达到最大学生数!" << endl;
return;
}
Student student;
cout << "请输入学生姓名:";
cin >> student.name;
cout << "请输入学生年龄:";
cin >> student.age;
cout << "请输入学生性别(M/F):";
cin >> student.gender;
cout << "请输入学生分数:";
cin >> student.score;
students[count] = student;
count++;
cout << "添加成功!" << endl;
}
// 删除学生
void deleteStudent() {
if (count <= 0) {
cout << "当前没有学生!" << endl;
return;
}
int index;
cout << "请输入要删除的学生编号(从1开始):";
cin >> index;
if (index < 1 || index > count) {
cout << "请输入正确的学生编号!" << endl;
return;
}
for (int i = index - 1; i < count - 1; i++) {
students[i] = students[i + 1];
}
count--;
cout << "删除成功!" << endl;
}
// 修改学生
void modifyStudent() {
if (count <= 0) {
cout << "当前没有学生!" << endl;
return;
}
int index;
cout << "请输入要修改的学生编号(从1开始):";
cin >> index;
if (index < 1 || index > count) {
cout << "请输入正确的学生编号!" << endl;
return;
}
Student student = students[index - 1];
cout << "请输入学生姓名(原姓名:" << student.name << "):";
cin >> student.name;
cout << "请输入学生年龄(原年龄:" << student.age << "):";
cin >> student.age;
cout << "请输入学生性别(M/F,原性别:" << student.gender << "):";
cin >> student.gender;
cout << "请输入学生分数(原分数:" << student.score << "):";
cin >> student.score;
students[index - 1] = student;
cout << "修改成功!" << endl;
}
// 查询学生
void queryStudent() {
if (count <= 0) {
cout << "当前没有学生!" << endl;
return;
}
int index;
cout << "请输入要查询的学生编号(从1开始):";
cin >> index;
if (index < 1 || index > count) {
cout << "请输入正确的学生编号!" << endl;
return;
}
Student student = students[index - 1];
cout << "姓名:" << student.name << endl;
cout << "年龄:" << student.age << endl;
cout << "性别:" << student.gender << endl;
cout << "分数:" << student.score << endl;
}
// 显示所有学生
void showAllStudents() {
if (count <= 0) {
cout << "当前没有学生!" << endl;
return;
}
for (int i = 0; i < count; i++) {
cout << "学生编号:" << i + 1 << endl;
cout << "姓名:" << students[i].name << endl;
cout << "年龄:" << students[i].age << endl;
cout << "性别:" << students[i].gender << endl;
cout << "分数:" << students[i].score << endl;
}
}
// 显示菜单
void showMenu() {
cout << "欢迎使用学生管理系统!" << endl;
cout << "1. 添加学生" << endl;
cout << "2. 删除学生" << endl;
cout << "3. 修改学生" << endl;
cout << "4. 查询学生" << endl;
cout << "5. 显示所有学生" << endl;
cout << "0. 退出程序" << endl;
cout << "请输入操作编号:";
}
// 运行程序
void run() {
int choice;
do {
showMenu();
cin >> choice;
switch (choice) {
case 1:
addStudent();
break;
case 2:
deleteStudent();
break;
case 3:
modifyStudent();
break;
case 4:
queryStudent();
break;
case 5:
showAllStudents();
break;
case 0:
cout << "谢谢使用,再见!" << endl;
break;
default:
cout << "请输入正确的操作编号!" << endl;
break;
}
} while (choice != 0);
}
};
int main() {
StudentManagementSystem sms;
sms.run();
return 0;
}
```
这个学生管理系统包括添加、删除、修改、查询和显示所有学生等基本功能。可以根据需要进行修改和扩展。