数据结构程序设计实验三

  1 //[问题描述] 每个员工的信息包括:编号、姓名、性别、出生年月、学历、职务、电话、住址等。
  2 //系统能够完成员工信息的查询、更新、插入、删除、排序等功能。
  3 //[基本要求] (1) ok排序:按不同关键字,对所有员工的信息进行排序。(2) ok查询:按特定条件查找员工。 
  4 //(3) ok更新:按编号对某个员工的某项信息进行修改。 (4) ok插入:加入新员工的信息。 (5) ok删除:按编号删除已离职的员工的信息。
  5 
  6 
  7 
  8 #include<iostream>
  9 #include<cstdio>
 10 #include<string>
 11 #include<vector>
 12 #include<algorithm>
 13 using namespace std;
 14 
 15 class Management {
 16 public:
 17     int ID;//编号
 18     string name;//姓名
 19     string sex;//性别
 20     string birth;//出生年月
 21     string diploma;//学历
 22     string job;//职务
 23     string tele;//电话
 24     string address;//住址
 25 public:
 26     Management(){}
 27     Management(int id,string Name,string Sex,string Birth,string Diploma,
 28         string Job,string Tele,string Address):ID(id),name(Name),sex(Sex),
 29         birth(Birth),diploma(Diploma),job(Job),tele(Tele),address(Address){}
 30 
 31     friend ostream& operator<<(ostream& out, const Management& e);
 32     friend istream& operator>>(istream& in, Management& e);
 33 };
 34 
 35 istream& operator>>(istream& in, Management& e)
 36 {
 37     cout << "输入ID:"; cin >> e.ID;cout << "输入姓名:"; cin >> e.name;
 38     cout << "输入性别:"; cin >> e.sex; cout << "输入出生年月:"; cin >> e.birth;
 39     cout << "输入学历:"; cin >> e.diploma; cout << "输入职务:"; cin >> e.job; 
 40     cout << "输入电话:"; cin >> e.tele; cout << "输入住址:"; cin >> e.address;
 41     return in;//输入编号、姓名、性别、出生年月、学历、职务、电话、住址
 42 }
 43 
 44 ostream& operator<<(ostream& out, const Management& e)
 45 {
 46     cout << "ID:"; out<< e.ID<<endl; cout << "姓名:"; out << e.name << endl;
 47     cout << "性别:"; out << e.sex << endl; cout << "出生年月:"; out << e.birth << endl;
 48     cout << "学历:"; out << e.diploma << endl; cout << "职务:"; out << e.job << endl;
 49     cout << "电话:"; out << e.tele << endl; cout << "住址:"; out << e.address << endl;
 50     return out;//输出
 51 }
 52 
 53 class EmployeeManagement :public Management {
 54 private:
 55     vector<Management>employees;
 56 public:
 57     void addEmployee(const Management& e) {//添加员工
 58         employees.push_back(e);
 59     }
 60 
 61     void deleteEmployee(int id) {//删除员工
 62         auto it = remove_if(employees.begin(), employees.end(), [id](const Management& e) {
 63             return e.ID == id;
 64             });//将对应ID员工放在容器末尾
 65         employees.erase(it, employees.end());
 66     }
 67 
 68     void updateEmployee(int id) {//更新员工信息
 69         for (auto& e : employees){
 70             if (e.ID == id){
 71                 Management ne;
 72                 cout << "输入该员工新信息:\n";
 73                 cin >> ne;
 74                 e = ne;
 75             }
 76         }
 77         cout << "未找到员工信息!\n";
 78     }
 79 
 80     void findEmployee(int id) {//按id查找员工
 81         for (const auto& e : employees){
 82             if (e.ID == id){
 83                 cout << e;
 84                 return;
 85             }
 86         }
 87         cout << "未找到员工信息!\n";
 88     }
 89 
 90     void sortEmployees(int option) {//按不同方式排序
 91         if (option == 1) {
 92             // 按编号排序
 93             sort(employees.begin(), employees.end(), [](const Management& e1, const Management& e2) {
 94                 return e1.ID < e2.ID;
 95                 });
 96         }
 97         else if (option == 2) {
 98             // 按姓名排序
 99             sort(employees.begin(), employees.end(), [](const Management& e1, const Management& e2) {
100                 return e1.name < e2.name;
101                 });
102         }
103         else if (option == 3) {
104             // 按职位排序
105             sort(employees.begin(), employees.end(), [](const Management& e1, const Management& e2) {
106                 return e1.job < e2.job;
107                 });
108         }
109         cout << "排序完成!\n";
110     }
111 
112     void displayEmoployees()const{//显示所有员工信息
113         if (employees.empty()) {
114             cout << "没有员工信息!\n";
115             return;
116         }
117         int i = 1;
118         for (const auto& e : employees){
119             cout << string(20, '-') << "" << i << "位员工信息" << string(20, '-') << endl;
120             cout << e ;
121             i++;
122         }
123     }
124 };
125 
126 void menu()
127 {
128     cout << "\n员工管理系统\n";
129     cout << "1.添加员工\n";
130     cout << "2.删除员工\n";
131     cout << "3.更新员工信息\n";
132     cout << "4.按ID查找员工\n";
133     cout << "5.对员工排序\n";
134     cout << "6.显示所有员工\n";
135     cout << "0.退出系统\n";
136     cout << "请输入选择:";
137 }
138 
139 int main()
140 {
141     EmployeeManagement manager;
142     int choice;
143     while (true)
144     {
145         menu();
146         cin >> choice;
147         if (choice == 0)
148             break;
149         switch (choice) {
150         case 1: {
151             Management e;
152             cout << "输入要添加的员工信息:\n";
153             cin >> e;
154             manager.addEmployee(e);
155             break;
156             }
157         case 2: {
158             int id;
159             cout << "输入要删除的员工ID:\n";
160             cin >>id;
161             manager.deleteEmployee(id);
162             break;
163         }
164         case 3: {
165             int id;
166             cout << "输入要更新的员工ID:\n";
167             cin >> id;
168             manager.updateEmployee(id);
169             break;
170         }
171         case 4:{
172             int id;
173             cout << "输入要查找的员工ID:\n";
174             cin >> id;
175             manager.findEmployee(id);
176             break;
177         }
178         case 5: {
179             int option;
180             cout << "1.按编号排序\n";
181             cout << "2.按姓名排序\n";
182             cout << "3.按职位排序\n";
183             cout << "输入选择的排序方式:";
184             cin >> option;
185             manager.sortEmployees(option);
186             break;
187         }
188         case 6: {
189             cout << "所有员工信息如下:\n";
190             manager.displayEmoployees();
191             break;
192         }
193 
194         }
195     }
196     return 0;
197 }

 

posted @ 2024-12-15 10:44  SuouYuki  阅读(10)  评论(0编辑  收藏  举报