```cpp #include <iostream> #include <string> #include <vector> #include <algorithm> struct Volunteer { std::string id; std::string name; std::string phone; int hours; }; std::vector<Volunteer> roster; void addVolunteer(int n) { std::string id, name, phone; std::cout << "Please enter the information for " << n << " volunteers:" << std::endl; for (int i = 0; i < n; i++) { Volunteer vol; std::cout << "Volunteer #" << i+1 << " - ID: "; std::cin >> vol.id; std::cout << "Volunteer #" << i+1 << " - Name: "; std::cin >> vol.name; std::cout << "Volunteer #" << i+1 << " - Phone: "; std::cin >> vol.phone; vol.hours = 0; roster.push_back(vol); } } void insertVolunteer(int pos) { Volunteer vol; std::cout << "Enter the information for the volunteer to be inserted:" << std::endl; std::cout << "Volunteer ID: "; std::cin >> vol.id; std::cout << "Volunteer Name: "; std::cin >> vol.name; std::cout << "Volunteer Phone: "; std::cin >> vol.phone; vol.hours = 0; roster.insert(roster.begin() + pos - 1, vol); } void editVolunteer(std::string identifier) { for (int i = 0; i < roster.size(); i++) { if (roster[i].id == identifier || roster[i].name == identifier) { std::cout << "Enter new information for the volunteer:" << std::endl; std::cout << "New ID: "; std::cin >> roster[i].id; std::cout << "New Name: "; std::cin >> roster[i].name; std::cout << "New Phone: "; std::cin >> roster[i].phone; return; } } std::cout << "Volunteer not found." << std::endl; } void deleteVolunteer(std::string identifier) { for (auto it = roster.begin(); it != roster.end(); ) { if (it->id == identifier) { it = roster.erase(it); } else { ++it; } } } void printRoster() { std::cout << "Volunteer Roster:" << std::endl; for (const Volunteer& vol : roster) { std::cout << "ID: " << vol.id << " | Name: " << vol.name << " | Phone: " << vol.phone << " | Hours: " << vol.hours << std::endl; } std::cout << "Total volunteers: " << roster.size() << std::endl; } void sortRoster() { std::sort(roster.begin(), roster.end(), [](const Volunteer& a, const Volunteer& b) { return a.hours > b.hours; }); } int main() { addVolunteer(10); int choice; while (true) { std::cout << "\nMenu:" << std::endl; std::cout << "1. Add volunteers" << std::endl; std::cout << "2. Insert volunteer" << std::endl; std::cout << "3. Edit volunteer" << std::endl; std::cout << "4. Delete volunteer" << std::endl; std::cout << "5. Print roster" << std::endl; std::cout << "6. Sort roster by hours" << std::endl; std::cout << "7. Exit" << std::endl; std::cout << "Enter your choice: "; std::cin >> choice; switch (choice) { case 1: int n; std::cout << "Enter number of volunteers to add: "; std::cin >> n; addVolunteer(n); break; case 2: int pos; std::cout << "Enter position to insert volunteer: "; std::cin >> pos; insertVolunteer(pos); break; case 3: std::string identifier; std::cout << "Enter volunteer ID or Name to edit: "; std::cin >> identifier; editVolunteer(identifier); break; case 4: std::cout << "Enter volunteer ID to delete: "; std::cin >> identifier; deleteVolunteer(identifier); break; case 5: printRoster(); break; case 6: sortRoster(); std::cout << "Roster sorted by hours." << std::endl; printRoster(); break; case 7: return 0; default: std::cout << "Invalid choice. Please try again." << std::endl; break; } } return 0; } ``` [2024-12-16 11:11:39 | AI写代码神器 | 2934点数解答]