酷代码 AI
菜单
全部AI  /  热门  /  AI写代码神器
服务商

用c++编写一个完整代码,要求:基于顺序存储结构的科技竞赛志愿者名册管理程序设计 利用数组设计一个志愿者名册管理程序,记录每个志愿者的编号、姓名、电话号码和志愿服务时长,并通过数字化菜单为用户提供插入、删除、查找和打印等功能。通过菜单的选择,用户可以对志愿者的信息进行管理。需要实现的功能包括: 添加志愿者信息。可一次添加n人,n为键盘录入的整数。录入的时候,志愿时长的初始值为0。 在数组的指定位置插入某个学生的数据。 修改志愿者信息。根据编号或姓名查找志愿者的信息。如果找到对应的志愿者,用户就可以对其所有信息进行修改。 删除特定编号的志愿者。 在屏幕上输出所有志愿者的信息。同一个人的信息应显示在同一行。最后显示总人数。 根据志愿服务时长进行排序,并输出排序后的结果。 以上功能对应的菜单,在执行时应当没有顺序和次数的限制。设计程序时需要对各种特殊情况(如数组为空)加以充分的考虑。编号、姓名和电话号码要求定义成字符串。为了节省数据录入的时间,可以通过初始化程序将部分数据写入顺序表。键盘录入数据的功能,演示实现的过程就可以。演示各项功能的时候,顺序表中至少要包含10组数据。

```cpp #include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; struct Volunteer { string id; string name; string phone; int duration; }; vector<Volunteer> roster; void addVolunteer() { int n; cout << "Enter the number of volunteers to add: "; cin >> n; for (int i = 0; i < n; i++) { Volunteer v; cout << "Enter volunteer's ID: "; cin >> v.id; cout << "Enter volunteer's name: "; cin >> v.name; cout << "Enter volunteer's phone number: "; cin >> v.phone; v.duration = 0; roster.push_back(v); } cout << "Volunteers added successfully." << endl; } void insertVolunteer() { int pos; Volunteer v; cout << "Enter the position to insert the volunteer: "; cin >> pos; cout << "Enter volunteer's ID: "; cin >> v.id; cout << "Enter volunteer's name: "; cin >> v.name; cout << "Enter volunteer's phone number: "; cin >> v.phone; v.duration = 0; roster.insert(roster.begin() + pos, v); cout << "Volunteer inserted successfully." << endl; } void modifyVolunteer() { string searchKey; cout << "Enter volunteer's ID or name to search: "; cin >> searchKey; for (Volunteer& v : roster) { if (v.id == searchKey || v.name == searchKey) { cout << "Enter new ID: "; cin >> v.id; cout << "Enter new name: "; cin >> v.name; cout << "Enter new phone number: "; cin >> v.phone; cout << "Volunteer information modified successfully." << endl; return; } } cout << "Volunteer not found!" << endl; } void deleteVolunteer() { string searchKey; cout << "Enter volunteer's ID to delete: "; cin >> searchKey; for (auto it = roster.begin(); it != roster.end(); ++it) { if (it->id == searchKey) { it = roster.erase(it); cout << "Volunteer deleted successfully." << endl; return; } } cout << "Volunteer not found!" << endl; } void printRoster() { for (const Volunteer& v : roster) { cout << "ID: " << v.id << " | Name: " << v.name << " | Phone: " << v.phone << " | Duration: " << v.duration << endl; } cout << "Total number of volunteers: " << roster.size() << endl; } void sortByDuration() { sort(roster.begin(), roster.end(), [](const Volunteer &a, const Volunteer &b) { return a.duration > b.duration; }); cout << "Volunteers sorted by duration:" << endl; printRoster(); } int main() { // Initialize some volunteers Volunteer v1 = {"001", "Alice", "123-456-7890", 10}; Volunteer v2 = {"002", "Bob", "321-654-0987", 5}; Volunteer v3 = {"003", "Charlie", "456-789-0123", 8}; roster = {v1, v2, v3}; int choice; do { cout << "\n1. Add Volunteer\n2. Insert Volunteer\n3. Modify Volunteer\n4. Delete Volunteer\n5. Print Roster\n6. Sort by Duration\n7. Exit\n"; cout << "Enter your choice: "; cin >> choice; switch (choice) { case 1: addVolunteer(); break; case 2: insertVolunteer(); break; case 3: modifyVolunteer(); break; case 4: deleteVolunteer(); break; case 5: printRoster(); break; case 6: sortByDuration(); break; case 7: cout << "Exiting program..." << endl; break; default: cout << "Invalid choice. Please try again." << endl; } } while (choice != 7); return 0; } ``` [2024-12-16 11:13:13 | AI写代码神器 | 2584点数解答]

相关提问