```cpp #include <iostream> #include <string> using namespace std; // 定义志愿者结构体 struct Volunteer { string id; string name; string phone; int hours; }; // 定义志愿者名册管理类 class Registry { private: Volunteer volunteers[100]; int numVolunteers; public: Registry() { numVolunteers = 0; } void addVolunteer() { int n; cout << "请输入要添加的志愿者人数:"; cin >> n; for (int i = 0; i < n; i++) { cout << "请输入第" << i + 1 << "个志愿者的编号:"; cin >> volunteers[numVolunteers].id; cout << "请输入第" << i + 1 << "个志愿者的姓名:"; cin >> volunteers[numVolunteers].name; cout << "请输入第" << i + 1 << "个志愿者的电话号码:"; cin >> volunteers[numVolunteers].phone; volunteers[numVolunteers].hours = 0; numVolunteers++; } } void insertVolunteer(int pos) { if (pos < 0 || pos > numVolunteers) { cout << "插入位置无效!" << endl; return; } cout << "请输入要插入的志愿者的编号:"; cin >> volunteers[numVolunteers].id; cout << "请输入要插入的志愿者的姓名:"; cin >> volunteers[numVolunteers].name; cout << "请输入要插入的志愿者的电话号码:"; cin >> volunteers[numVolunteers].phone; volunteers[numVolunteers].hours = 0; numVolunteers++; } void findVolunteer(string key) { for (int i = 0; i < numVolunteers; i++) { if (volunteers[i].id == key || volunteers[i].name == key) { cout << "编号:" << volunteers[i].id << " 姓名:" << volunteers[i].name << " 电话号码:" << volunteers[i].phone << " 志愿时长:" << volunteers[i].hours << endl; return; } } cout << "未找到匹配志愿者!" << endl; } void deleteVolunteer(string key) { for (int i = 0; i < numVolunteers; i++) { if (volunteers[i].id == key) { for (int j = i; j < numVolunteers - 1; j++) { volunteers[j] = volunteers[j + 1]; } numVolunteers--; cout << "删除成功!" << endl; return; } } cout << "未找到匹配志愿者!" << endl; } void printVolunteers() { for (int i = 0; i < numVolunteers; i++) { cout << "编号:" << volunteers[i].id << " 姓名:" << volunteers[i].name << " 电话号码:" << volunteers[i].phone << " 志愿时长:" << volunteers[i].hours << endl; } cout << "总人数:" << numVolunteers << endl; } void sortVolunteers() { for (int i = 0; i < numVolunteers - 1; i++) { for (int j = 0; j < numVolunteers - i - 1; j++) { if (volunteers[j].hours > volunteers[j + 1].hours) { Volunteer temp = volunteers[j]; volunteers[j] = volunteers[j + 1]; volunteers[j + 1] = temp; } } } } }; int main() { Registry registry; // 初始化部分数据 Volunteer v1 = {"001", "Alice", "*0", 10}; Volunteer v2 = {"002", "Bob", "9876543210", 5}; Volunteer v3 = {"003", "Cathy", "1357924680", 8}; registry.addVolunteer(v1); registry.addVolunteer(v2); registry.addVolunteer(v3); int choice; string key; int pos; do { cout << "==========志愿者名册管理系统==========" << endl; cout << "1. 添加志愿者信息" << endl; cout << "2. 在指定位置插入志愿者信息" << endl; cout << "3. 修改志愿者信息" << endl; cout << "4. 根据编号或姓名查找志愿者信息" << endl; cout << "5. 删除特定编号的志愿者" << endl; cout << "6. 输出所有志愿者信息" << endl; cout << "7. 根据志愿服务时长排序" << endl; cout << "8. 退出" << endl; cout << "请选择功能:"; cin >> choice; switch (choice) { case 1: registry.addVolunteer(); break; case 2: cout << "请输入插入位置:"; cin >> pos; registry.insertVolunteer(pos); break; case 3: cout << "请输入要查找的志愿者的编号或姓名:"; cin >> key; registry.findVolunteer(key); break; case 4: cout << "请输入要删除的志愿者的编号:"; cin >> key; registry.deleteVolunteer(key); break; case 5: registry.printVolunteers(); break; case 6: registry.sortVolunteers(); cout << "排序成功!" << endl; break; case 7: break; case 8: cout << "感谢使用!" << endl; break; default: cout << "请输入有效的菜单选项!" << endl; } } while (choice != 8); return 0; } ``` [2024-12-16 11:12:08 | AI写代码神器 | 3423点数解答]