一个简单的学生管理系统代码C++
时间: 2023-07-31 11:11:34 浏览: 52
下面是一个简单的学生管理系统的 C++ 代码示例:
```cpp
#include <iostream>
#include <cstring>
using namespace std;
struct Student {
char name[20];
int age;
char gender;
int score;
};
void input(Student &s) {
cout << "请输入学生姓名:";
cin >> s.name;
cout << "请输入学生年龄:";
cin >> s.age;
cout << "请输入学生性别(男:M,女:F):";
cin >> s.gender;
cout << "请输入学生成绩:";
cin >> s.score;
}
void output(const Student &s) {
cout << "学生姓名:" << s.name << endl;
cout << "学生年龄:" << s.age << endl;
cout << "学生性别:" << s.gender << endl;
cout << "学生成绩:" << s.score << endl;
}
int main() {
Student stu[100];
int n = 0;
char command[10];
while (true) {
cout << "请输入命令(add/delete/find/show/quit):";
cin >> command;
if (strcmp(command, "add") == 0) {
input(stu[n]);
n++;
cout << "添加成功!" << endl;
} else if (strcmp(command, "delete") == 0) {
char name[20];
cout << "请输入要删除的学生姓名:";
cin >> name;
for (int i = 0; i < n; i++) {
if (strcmp(stu[i].name, name) == 0) {
for (int j = i; j < n - 1; j++) {
stu[j] = stu[j + 1];
}
n--;
cout << "删除成功!" << endl;
break;
}
}
} else if (strcmp(command, "find") == 0) {
char name[20];
cout << "请输入要查找的学生姓名:";
cin >> name;
for (int i = 0; i < n; i++) {
if (strcmp(stu[i].name, name) == 0) {
output(stu[i]);
break;
}
}
} else if (strcmp(command, "show") == 0) {
for (int i = 0; i < n; i++) {
output(stu[i]);
}
} else if (strcmp(command, "quit") == 0) {
break;
} else {
cout << "无效命令!" << endl;
}
}
return 0;
}
```
该程序定义了一个结构体 `Student`,其中包含了学生的姓名、年龄、性别和成绩。程序通过不断循环来接收用户的命令,用户可以输入 `add` 命令来添加学生信息,输入 `delete` 命令来删除学生信息,输入 `find` 命令来查找学生信息,输入 `show` 命令来显示所有学生信息,输入 `quit` 命令来退出程序。在程序的实现中,使用了数组来存储多个学生信息。