班级信息管理系统用数组实现多功能,包括删除信息,查询等
时间: 2023-06-11 19:08:23 浏览: 57
好的,下面是一个简单的班级信息管理系统的示例代码:
```c++
#include <iostream>
#include <string>
using namespace std;
struct Student {
string name; // 学生姓名
int age; // 学生年龄
int number; // 学生学号
};
const int MAX_NUM = 100; // 最大学生数
Student students[MAX_NUM]; // 学生数组
int numStudents = 0; // 学生数
// 添加学生信息
void addStudent() {
if (numStudents >= MAX_NUM) {
cout << "班级已满,无法添加学生!" << endl;
return;
}
Student s;
cout << "请输入学生姓名:";
cin >> s.name;
cout << "请输入学生年龄:";
cin >> s.age;
cout << "请输入学生学号:";
cin >> s.number;
students[numStudents++] = s;
cout << "学生信息添加成功!" << endl;
}
// 删除学生信息
void deleteStudent() {
if (numStudents <= 0) {
cout << "班级已无学生,无法删除学生!" << endl;
return;
}
int number;
cout << "请输入要删除的学生学号:";
cin >> number;
bool found = false;
for (int i = 0; i < numStudents; i++) {
if (students[i].number == number) {
found = true;
for (int j = i; j < numStudents - 1; j++) {
students[j] = students[j + 1];
}
numStudents--;
cout << "学生信息删除成功!" << endl;
break;
}
}
if (!found) {
cout << "未找到该学生!" << endl;
}
}
// 查询学生信息
void searchStudent() {
if (numStudents <= 0) {
cout << "班级已无学生,无法查询学生!" << endl;
return;
}
int number;
cout << "请输入要查询的学生学号:";
cin >> number;
bool found = false;
for (int i = 0; i < numStudents; i++) {
if (students[i].number == number) {
found = true;
cout << "学生姓名:" << students[i].name << endl;
cout << "学生年龄:" << students[i].age << endl;
cout << "学生学号:" << students[i].number << endl;
break;
}
}
if (!found) {
cout << "未找到该学生!" << endl;
}
}
// 显示所有学生信息
void displayStudents() {
if (numStudents <= 0) {
cout << "班级已无学生,无法显示学生信息!" << endl;
return;
}
cout << "班级共有 " << numStudents << " 名学生:" << endl;
for (int i = 0; i < numStudents; i++) {
cout << "学生姓名:" << students[i].name << endl;
cout << "学生年龄:" << students[i].age << endl;
cout << "学生学号:" << students[i].number << endl;
cout << "-------------" << endl;
}
}
int main() {
int choice;
while (true) {
cout << "请输入要进行的操作:" << endl;
cout << "1. 添加学生信息" << endl;
cout << "2. 删除学生信息" << endl;
cout << "3. 查询学生信息" << endl;
cout << "4. 显示所有学生信息" << endl;
cout << "0. 退出程序" << endl;
cin >> choice;
switch (choice) {
case 1:
addStudent();
break;
case 2:
deleteStudent();
break;
case 3:
searchStudent();
break;
case 4:
displayStudents();
break;
case 0:
cout << "程序已退出!" << endl;
return 0;
default:
cout << "输入的操作无效,请重新输入!" << endl;
}
}
}
```
这个班级信息管理系统使用了一个 `Student` 结构体来表示一个学生的信息,然后使用一个大小为 `MAX_NUM` 的数组 `students` 来存储所有学生的信息,并使用变量 `numStudents` 记录班级中当前的学生数。
程序提供了四个功能:添加学生信息、删除学生信息、查询学生信息和显示所有学生信息。用户可以通过输入对应的数字来选择要进行的操作。在每个功能的实现中,都根据需要从用户那里获取必要的输入,并对数组进行相应的操作。
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)