写一个C++的考生报名系统
时间: 2024-04-17 07:24:13 浏览: 18
当然可以!下面是一个简单的C++考生报名系统的示例代码:
```cpp
#include <iostream>
#include <vector>
#include <string>
class Student {
public:
Student(const std::string& name, int age, const std::string& id)
: name(name), age(age), id(id) {}
std::string getName() const {
return name;
}
int getAge() const {
return age;
}
std::string getId() const {
return id;
}
private:
std::string name;
int age;
std::string id;
};
class RegistrationSystem {
public:
void addStudent(const std::string& name, int age, const std::string& id) {
students.push_back(Student(name, age, id));
}
bool removeStudent(const std::string& id) {
for (auto it = students.begin(); it != students.end(); ++it) {
if (it->getId() == id) {
students.erase(it);
return true;
}
}
return false;
}
void displayStudents() const {
for (const auto& student : students) {
std::cout << "Name: " << student.getName() << ", Age: " << student.getAge() << ", ID: " << student.getId() << std::endl;
}
}
private:
std::vector<Student> students;
};
int main() {
RegistrationSystem system;
// 添加学生
system.addStudent("Alice", 20, "1001");
system.addStudent("Bob", 19, "1002");
system.addStudent("Charlie", 22, "1003");
// 显示所有学生
std::cout << "All Students:" << std::endl;
system.displayStudents();
std::cout << std::endl;
// 移除学生
std::cout << "Removing student with ID 1002..." << std::endl;
if (system.removeStudent("1002")) {
std::cout << "Student removed successfully." << std::endl;
} else {
std::cout << "Student not found." << std::endl;
}
std::cout << std::endl;
// 再次显示所有学生
std::cout << "All Students after removal:" << std::endl;
system.displayStudents();
return 0;
}
```
这个系统使用了两个类:`Student` 和 `RegistrationSystem`。`Student` 类表示一个学生,包含姓名、年龄和学号等信息。`RegistrationSystem` 类是报名系统,可以添加学生、移除学生和显示所有学生信息。
在 `main` 函数中,我们创建了一个 `RegistrationSystem` 对象,并使用 `addStudent` 函数添加了三个学生。然后使用 `displayStudents` 函数显示所有学生信息。接着,我们尝试通过 `removeStudent` 函数移除一个学生,并再次调用 `displayStudents` 函数验证学生是否被成功移除。
当你运行这段代码时,你将看到以下输出:
```
All Students:
Name: Alice, Age: 20, ID: 1001
Name: Bob, Age: 19, ID: 1002
Name: Charlie, Age: 22, ID: 1003
Removing student with ID 1002...
Student removed successfully.
All Students after removal:
Name: Alice, Age: 20, ID: 1001
Name: Charlie, Age: 22, ID: 1003
```
这个简单的示例只是一个考生报名系统的基本框架,你可以根据实际需求进行扩展和修改。希望能对你有所帮助!如果你有任何问题,请随时提问。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)