用C++写一个小型管理信息系统
时间: 2023-06-27 09:06:35 浏览: 137
好的,下面是一个简单的管理信息系统的代码示例:
```cpp
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct Student {
string name;
int age;
float score;
};
void printStudent(const Student& student) {
cout << "Name: " << student.name << ", Age: " << student.age << ", Score: " << student.score << endl;
}
void addStudent(vector<Student>& students) {
Student student;
cout << "Input name: ";
cin >> student.name;
cout << "Input age: ";
cin >> student.age;
cout << "Input score: ";
cin >> student.score;
students.push_back(student);
cout << "Add student successful!" << endl;
}
void deleteStudent(vector<Student>& students) {
string name;
cout << "Input name to delete: ";
cin >> name;
auto it = remove_if(students.begin(), students.end(), [&](const Student& student) {
return student.name == name;
});
if (it != students.end()) {
students.erase(it, students.end());
cout << "Delete student successful!" << endl;
} else {
cout << "Student not found!" << endl;
}
}
void printAllStudent(const vector<Student>& students) {
for (const auto& student : students) {
printStudent(student);
}
}
void printMenu() {
cout << "1. Add student" << endl;
cout << "2. Delete student" << endl;
cout << "3. Print all students" << endl;
cout << "4. Exit" << endl;
cout << "Please input your choice: ";
}
int main() {
vector<Student> students;
while (true) {
printMenu();
int choice;
cin >> choice;
switch (choice) {
case 1:
addStudent(students);
break;
case 2:
deleteStudent(students);
break;
case 3:
printAllStudent(students);
break;
case 4:
return 0;
default:
cout << "Invalid choice!" << endl;
break;
}
}
return 0;
}
```
这个小型管理信息系统可以添加、删除和打印所有学生的信息。其中,一个学生的信息包括姓名、年龄和分数。代码中使用了 STL 中的 vector 容器来存储学生信息,并使用了 lambda 表达式和算法函数来删除指定姓名的学生。你可以根据自己的需要进行修改和扩展。
阅读全文