用C加加写学生信息管理系统
时间: 2023-07-18 11:03:51 浏览: 143
下面是一个简单的学生信息管理系统的示例代码,使用C++编写:
```
#include <iostream>
#include <string>
using namespace std;
const int MAX_STUDENT_NUM = 100; // 最大学生数
const int MAX_NAME_LEN = 20; // 学生姓名最大长度
struct Student {
string name;
int age;
float score;
};
int main() {
Student students[MAX_STUDENT_NUM]; // 学生数组
int studentNum = 0; // 学生数目
char choice = ' '; // 菜单选择
while (true) {
// 显示菜单
cout << "====================" << endl;
cout << "1. 添加学生信息" << endl;
cout << "2. 查询学生信息" << endl;
cout << "3. 修改学生信息" << endl;
cout << "4. 删除学生信息" << endl;
cout << "5. 显示所有学生信息" << endl;
cout << "6. 退出程序" << endl;
cout << "====================" << endl;
cout << "请选择: ";
cin >> choice;
switch (choice) {
case '1': // 添加学生信息
if (studentNum >= MAX_STUDENT_NUM) {
cout << "学生数目已达到最大值" << endl;
} else {
Student newStudent;
cout << "请输入学生姓名: ";
cin >> newStudent.name;
cout << "请输入学生年龄: ";
cin >> newStudent.age;
cout << "请输入学生成绩: ";
cin >> newStudent.score;
students[studentNum] = newStudent;
studentNum++;
cout << "添加成功" << endl;
}
break;
case '2': // 查询学生信息
if (studentNum == 0) {
cout << "学生信息为空" << endl;
} else {
string name;
cout << "请输入要查询的学生姓名: ";
cin >> name;
bool found = false;
for (int i = 0; i < studentNum; i++) {
if (students[i].name == name) {
cout << "姓名: " << students[i].name << endl;
cout << "年龄: " << students[i].age << endl;
cout << "成绩: " << students[i].score << endl;
found = true;
break;
}
}
if (!found) {
cout << "未找到该学生" << endl;
}
}
break;
case '3': // 修改学生信息
if (studentNum == 0) {
cout << "学生信息为空" << endl;
} else {
string name;
cout << "请输入要修改的学生姓名: ";
cin >> name;
bool found = false;
for (int i = 0; i < studentNum; i++) {
if (students[i].name == name) {
cout << "请输入修改后的信息" << endl;
cout << "年龄: ";
cin >> students[i].age;
cout << "成绩: ";
cin >> students[i].score;
found = true;
cout << "修改成功" << endl;
break;
}
}
if (!found) {
cout << "未找到该学生" << endl;
}
}
break;
case '4': // 删除学生信息
if (studentNum == 0) {
cout << "学生信息为空" << endl;
} else {
string name;
cout << "请输入要删除的学生姓名: ";
cin >> name;
bool found = false;
for (int i = 0; i < studentNum; i++) {
if (students[i].name == name) {
for (int j = i; j < studentNum - 1; j++) {
students[j] = students[j + 1];
}
studentNum--;
found = true;
cout << "删除成功" << endl;
break;
}
}
if (!found) {
cout << "未找到该学生" << endl;
}
}
break;
case '5': // 显示所有学生信息
if (studentNum == 0) {
cout << "学生信息为空" << endl;
} else {
for (int i = 0; i < studentNum; i++) {
cout << "姓名: " << students[i].name << endl;
cout << "年龄: " << students[i].age << endl;
cout << "成绩: " << students[i].score << endl;
cout << "------------------" << endl;
}
}
break;
case '6': // 退出程序
cout << "程序已退出" << endl;
return 0;
default:
cout << "无效的选择" << endl;
}
}
return 0;
}
```
上述代码实现了一个简单的学生信息管理系统,可以通过菜单进行添加、查询、修改、删除和显示学生信息。在这个示例中,使用结构体存储每个学生的信息,并使用数组来存储所有学生的信息。当然,这只是一个简单的示例,实际情况下可能需要更加复杂的实现方式。
阅读全文