编写一个简单的C++语言教务管理系统的代码
时间: 2023-07-29 22:09:07 浏览: 96
C++实现简单的教务管理系统
5星 · 资源好评率100%
以下是一个简单的C++语言教务管理系统的代码示例:
```c++
#include <iostream>
using namespace std;
// 定义学生结构体
struct Student {
string name;
int age;
string gender;
string major;
float score;
};
// 定义教务管理系统类
class EduSystem {
private:
Student students[100]; // 最多存储100个学生信息
int count = 0; // 学生数量
public:
void addStudent(Student student) {
if (count < 100) {
students[count++] = student;
cout << "添加学生成功!" << endl;
} else {
cout << "学生数量已达到上限,无法添加!" << endl;
}
}
void deleteStudent(string name) {
bool found = false;
for (int i = 0; i < count; i++) {
if (students[i].name == name) {
found = true;
for (int j = i; j < count - 1; j++) {
students[j] = students[j + 1];
}
count--;
cout << "删除学生成功!" << endl;
break;
}
}
if (!found) {
cout << "未找到该学生!" << endl;
}
}
void searchStudent(string name) {
bool found = false;
for (int i = 0; i < count; i++) {
if (students[i].name == name) {
found = true;
cout << "姓名:" << students[i].name << endl;
cout << "年龄:" << students[i].age << endl;
cout << "性别:" << students[i].gender << endl;
cout << "专业:" << students[i].major << endl;
cout << "成绩:" << students[i].score << endl;
break;
}
}
if (!found) {
cout << "未找到该学生!" << endl;
}
}
void displayAll() {
if (count == 0) {
cout << "没有学生信息!" << endl;
} else {
for (int i = 0; i < count; i++) {
cout << "姓名:" << students[i].name << endl;
cout << "年龄:" << students[i].age << endl;
cout << "性别:" << students[i].gender << endl;
cout << "专业:" << students[i].major << endl;
cout << "成绩:" << students[i].score << endl;
cout << endl;
}
}
}
};
int main() {
EduSystem eduSystem;
while (true) {
cout << "请选择操作:" << endl;
cout << "1. 添加学生" << endl;
cout << "2. 删除学生" << endl;
cout << "3. 查询学生" << endl;
cout << "4. 显示全部学生" << endl;
cout << "5. 退出" << endl;
int choice;
cin >> choice;
switch (choice) {
case 1: {
Student student;
cout << "请输入姓名、年龄、性别、专业和成绩(以空格分隔):" << endl;
cin >> student.name >> student.age >> student.gender >> student.major >> student.score;
eduSystem.addStudent(student);
break;
}
case 2: {
string name;
cout << "请输入要删除的学生姓名:" << endl;
cin >> name;
eduSystem.deleteStudent(name);
break;
}
case 3: {
string name;
cout << "请输入要查询的学生姓名:" << endl;
cin >> name;
eduSystem.searchStudent(name);
break;
}
case 4:
eduSystem.displayAll();
break;
case 5:
return 0;
default:
cout << "输入错误,请重新输入!" << endl;
}
}
}
```
该教务管理系统实现了添加学生、删除学生、查询学生和显示全部学生等功能。用户可以通过控制台输入指令来进行操作。其中,学生信息使用结构体存储,教务管理系统使用类来实现。
阅读全文