相关视频——黑马程序员匠心之作|C++教程从0到1入门编程,学习编程不再难-(147-166)
基础部分(1-83)——如果你准备学习C++,并且有C语言的基础,我希望你能简单的过一遍知识点。
核心部分(84-146)——C++核心编程部分
![](/images/C++%E5%AE%9E%E7%8E%B0%E8%81%8C%E5%B7%A5%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F.assets/%E6%99%83%E5%8A%A8%E7%9A%84%E5%B0%8F%E8%80%97%E5%AD%90.gif)
职工管理系统
管理系统需求
职工管理系统可以用来管理公司内所有员工的信息
本教程主要利用C++来实现一个基于多态的职工管理系统
公司中职工分为三类:普通员工、经理、老板,显示信息时,需要显示职工编号、职工姓名、职工岗位、以及职责
普通员工职责:完成经理交给的任务
经理职责:完成老板交给的任务,并下发任务给员工
老板职责:管理公司所有事务
管理系统中需要实现的功能如下:
- 退出管理程序:退出当前管理系统
- 增加职工信息:实现批量添加职工功能,将功能信息录入到文件中,职工信息为:职工编号、姓名、部门编号
- 显示职工信息:显示公司内部所有职工的信息
- 删除离职职工:按照编号删除指定的职工
- 修改职工信息:按照编号修改职工个人信息
- 查找职工信息:按照职工的编号或者职工的姓名进行查找相关的人员信息
- 按照编号排序:按照职工的编号,进行排序,排序规则由用户指定
- 清空所有文档:清空文件中记录的所有职工信息(清空前需要确认,防止误删)
存储多个员工
![image-20210710113024707](/images/C++%E5%AE%9E%E7%8E%B0%E8%81%8C%E5%B7%A5%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F.assets/image-20210710113024707.png)
代码实现
worker.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| #pragma once #include<iostream> using namespace std; #include<string>
class Worker { public: virtual void ShowInfo() = 0; virtual string GetDeptName() = 0; int m_Id; string m_Name; int m_DeptId; };
|
employee.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| #pragma once #include<iostream> #include"worker.h" using namespace std;
class Employee:public Worker { public: Employee(int id, string name, int deptid); virtual void ShowInfo(); virtual string GetDeptName(); };
|
boss.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| #pragma once #include<iostream> #include"worker.h" #include<string> using namespace std;
class Boss :public Worker { public: Boss(int id, string name, int deptid); virtual void ShowInfo(); virtual string GetDeptName(); };
|
manager.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| #pragma once #include<iostream> #include"worker.h" #include<string> using namespace std;
class Manager :public Worker { public: Manager(int id, string name, int deptid); virtual void ShowInfo(); virtual string GetDeptName(); };
|
wokerManager.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| #pragma once #include<iostream> #include<string> #include"worker.h" #include"employee.h" #include"manager.h" #include"boss.h" #include<fstream> #define FILENAME "test.txt" using namespace std; class WorkerManager { public: WorkerManager(); void Show_Menu(); void ExitSystem(); int m_EmpNum; Worker** m_EmpArray; void AddEmp(); void SaveFile(); bool m_FileIsEmpty; int Get_EmpNum(); void Init_Emp(); void Show_Emp(); int IsExist(int id); void Del_Emp(); void Mod_Emp(); void Find_Emp(); void Sort_Emp(); void Clean_File(); ~WorkerManager(); };
|
boss.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| #include"boss.h"
Boss::Boss(int id, string name, int deptid) { this->m_Id = id; this->m_Name = name; this->m_DeptId = deptid; }
void Boss::ShowInfo() { cout << "职工编号:" << this->m_Id << "\t职工姓名:" << this->m_Name << "\t岗位:" << this->GetDeptName() << "\t岗位职责:管理公司所有的事物" << endl; }
string Boss::GetDeptName() { return string("老板"); }
|
Manager.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| #include"manager.h"
Manager::Manager(int id, string name, int deptid) { this->m_Id = id; this->m_Name = name; this->m_DeptId = deptid; }
void Manager::ShowInfo() { cout << "职工编号:" << this->m_Id << "\t职工姓名:" << this->m_Name << "\t岗位:" << this->GetDeptName() << "\t岗位职责:完成老板交个任务,并且下发任务给普通员工" << endl; }
string Manager::GetDeptName() { return string("经理"); }
|
workerManager.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
| #include"workerManager.h"
WorkerManager::WorkerManager() { ifstream ifs; ifs.open(FILENAME, ios::in); if (!ifs.is_open()) { this->m_EmpNum = 0; this->m_EmpArray = NULL; this->m_FileIsEmpty = true; ifs.close(); return; } char ch; ifs >> ch; if (ifs.eof()) { this->m_EmpNum = 0; this->m_EmpArray = NULL; this->m_FileIsEmpty = true; ifs.close(); } int num = this->Get_EmpNum(); this->m_EmpNum = num; this->m_EmpArray = new Worker * [this->m_EmpNum]; this->Init_Emp();
}
void WorkerManager::Show_Menu() { cout <<"*****************************" << endl; cout <<"*****欢迎使用职工管理系统*****" << endl; cout <<"*******0-退出管理程序*********" << endl; cout <<"*******1-增加职工信息*********" << endl; cout <<"*******2-显示职工信息*********" << endl; cout <<"*******3-删除离职职工*********" << endl; cout <<"*******4-修改职工信息*********" << endl; cout <<"*******5-查找职工信息*********" << endl; cout <<"*******6-按照编号排序*********" << endl; cout <<"*******7-清空所有文档*********" << endl; cout <<"*****************************" << endl; }
void WorkerManager::ExitSystem() { cout << "欢迎下次使用" << endl; system("pause"); exit(0); }
void WorkerManager::AddEmp() { cout << "请输入添加职工的数量" << endl; int addnum = 0; cin >> addnum; if (addnum > 0) { int NewSize = this->m_EmpNum + addnum; Worker** NewSpace = new Worker * [NewSize]; if (this->m_EmpArray != NULL) { for (int i = 0; i < this->m_EmpNum; i++) { NewSpace[i] = this->m_EmpArray[i]; } } for(int i = 0; i< addnum;i++) { int id = 0; string name; int dselect; cout << "请输入第" <<i+1<<"个新职工的编号"<< endl;
while (cin>>id) { int adjust = 0; for (int j = 0; j <this->m_EmpNum; j++) { if (id == this->m_EmpArray[j]->m_Id) { cout << "此编号已存在!请重新输入" << endl; adjust = 1; } } if (adjust == 0) { break; } } cout << "请输入第" << i+1<< "个新职工的姓名" << endl; cin >> name; cout << "请选择该职工的岗位"<< endl; cout << "1.普通职工" << endl; cout << "2.经理" << endl; cout << "3.老板" << endl; cin >> dselect; Worker* worker = NULL; switch (dselect) { case 1: worker = new Employee(id, name, 1); break; case 2: worker = new Manager(id, name, 2); break; case 3: worker = new Boss(id, name, 3); break; default: break; } NewSpace[this->m_EmpNum + i] = worker; } delete[] this->m_EmpArray; this->m_EmpArray = NewSpace; this->m_EmpNum = NewSize; this->m_FileIsEmpty = false; cout << "成功添加" << addnum << "个新职工" << endl; this->SaveFile(); } else { cout << "输入有误" << endl; } system("pause"); system("cls"); }
void WorkerManager::SaveFile() { ofstream ofs; ofs.open("test.txt", ios::out); for (int i = 0; i < this->m_EmpNum; i++) { ofs << this->m_EmpArray[i]->m_Id << " " << this->m_EmpArray[i]->m_Name << " " << this->m_EmpArray[i]->m_DeptId << endl; } }
int WorkerManager::Get_EmpNum() { ifstream ifs; ifs.open(FILENAME, ios::in); int id; string name; int did; int num = 0; while (ifs>>id && ifs>>name && ifs>>did) { num++; } return num; }
void WorkerManager::Init_Emp() { ifstream ifs; ifs.open(FILENAME, ios::in);
int id; string name; int did; int index = 0; while (ifs>>id && ifs >>name && ifs>>did) { Worker* worker = NULL; if (did == 1) { worker = new Employee(id,name,did); } else if (did == 2) { worker = new Manager(id, name, did); } else { worker = new Boss(id, name, did); } this->m_EmpArray[index] = worker; index++; } ifs.close(); }
void WorkerManager::Show_Emp() { if (this->m_FileIsEmpty) { cout << "文件为空或记录为空" << endl; } else { for (int i = 0; i < this->m_EmpNum; i++) { this->m_EmpArray[i]->ShowInfo(); } } system("pause"); system("cls"); }
int WorkerManager::IsExist(int id) { int index = -1; for (int i = 0; i < this->m_EmpNum; i++) { if (this->m_EmpArray[i]->m_Id == id) { index = i; break; } } return index; }
void WorkerManager::Del_Emp() { if (this->m_FileIsEmpty) { cout << "文件不存在或者记录为空" << endl; } else { cout << "请输入要删除职工的编号" << endl; int id = 0; cin >> id; int index = this->IsExist(id); if (index != -1) { for (int i = index; i < this->m_EmpNum -1; i++) { this->m_EmpArray[i] = this->m_EmpArray[i + 1]; } this->m_EmpNum--; this->SaveFile(); cout << "删除成功" << endl; } else { cout << "删除失败,未找到该员工" << endl; } system("pause"); system("cls"); } }
void WorkerManager::Mod_Emp() { if (this->m_FileIsEmpty) { cout << "文件不存在或记录为空" << endl; } else { cout << "请输入要修改的职工编号" << endl; int id = 0; cin >> id; int ret = this->IsExist(id); if (ret != -1) { delete this->m_EmpArray[ret]; int newid = 0;; string newname = " "; int newselect = 0; cout << "查找到了编号为" << id << "的这个职工," <<"请输入新的职工号"<< endl; cin >> newid; cout << "请输入新的姓名" << endl; cin >> newname; cout << "请输入新的岗位" << endl; cout << "1.普通职工" << endl; cout << "2.经理" << endl; cout << "3.老板" << endl; cin >> newselect; Worker* worker = NULL; switch (newselect) { case 1: worker = new Employee(newid, newname, newselect); break; case 2: worker = new Manager(newid, newname, newselect); break; case 3: worker = new Boss(newid, newname, newselect); break; default: break; } this->m_EmpArray[ret] = worker; cout << "修改成功!" << endl; this->SaveFile(); } else { cout << "修改失败,查无此人。" << endl; } } system("pause"); system("cls"); }
void WorkerManager::Find_Emp() { if (this->m_FileIsEmpty) { cout << "文件不存在或记录为空" << endl; } else { cout << "请输入查找的方式" << endl; cout << "1.按职工编号查找" << endl; cout << "2.按职工姓名查找" << endl; int select = 0; cin >> select; if (select == 1) { int id; cout << "请输入查找的职工编号" << endl; cin >> id; int ret = this->IsExist(id); if (ret != -1) { cout << "查找成功!该职工的信息如下:" << endl; this->m_EmpArray[ret]->ShowInfo(); } else { cout << "查找失败,查无此人!" << endl; } } else if (select == 2) { string name; cout << "请输入要查找的姓名" << endl; cin >> name; bool flag = false; for (int i = 0; i < m_EmpNum; i++) { if (this->m_EmpArray[i]->m_Name== name) { cout << "查找成功,职工编号为" << this->m_EmpArray[i]->m_Id << "的职工,他的信息如下:" << endl; this->m_EmpArray[i]->ShowInfo(); flag = true; } } if (flag == false) { cout << "查找失败,查无此人" << endl; } } else { cout << "输入选项有误" << endl; } system("pause"); system("cls"); } }
void WorkerManager::Sort_Emp() { if (this->m_FileIsEmpty) { cout << "文件不存在或记录为空" << endl; system("system"); system("clsf"); } else { cout << "请选择排序方式" << endl; cout << "1.按照职工号进行升序" << endl; cout << "2.按照职工号进行降序" << endl; int select = 0; cin >> select; for (int i = 0; i < this->m_EmpNum; i++) { int MinOrMax = i; for (int j = i + 1; j < this->m_EmpNum; j++) { if (select == 1) { if (this->m_EmpArray[MinOrMax]->m_Id > this->m_EmpArray[j]->m_Id) { MinOrMax = j; } } else { if (this->m_EmpArray[MinOrMax]->m_Id < this->m_EmpArray[j]->m_Id) { MinOrMax = j; } } } if (i != MinOrMax) { Worker* temp = this->m_EmpArray[i]; this->m_EmpArray[i] = this->m_EmpArray[MinOrMax]; this->m_EmpArray[MinOrMax] = temp; } } cout << "排序成功!排序后的结果为:" << endl; this->SaveFile(); this->Show_Emp(); } }
void WorkerManager::Clean_File() { cout << "确认清空吗?" << endl; cout << "1.确认" << endl; cout << "2.取消" << endl; int select = 0;; cin >> select; if (select == 1) { ofstream ofs(FILENAME,ios::trunc); ofs.close(); if (this->m_EmpArray != NULL) { for (int i = 0; i < this->m_EmpNum; i++) { delete this->m_EmpArray[i]; } delete this->m_EmpArray; this->m_EmpArray = NULL; this->m_EmpNum = 0; this->m_FileIsEmpty = true; } cout << "清空成功!" << endl;
} system("pause"); system("cls"); }
WorkerManager::~WorkerManager() { if (this->m_EmpArray != NULL) { for (int i = 0; i < this->m_EmpNum; i++) { delete this->m_EmpArray[i]; } delete this->m_EmpArray; this->m_EmpArray = NULL; } }
|
职工管理系统
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| #include<iostream> #include"woklerManager.h" #include"worker.h" #include"employee.h" #include"manager.h" #include"boss.h" using namespace std; int main(void) {
WorkerManager wm; int choice = 0; while (1) { wm.Show_Menu(); cout << "请输入你的选择" << endl; cin >> choice;
switch (choice) { case 0: wm.ExitSystem(); break; case 1: wm.AddEmp(); break; case 2: wm.Show_Emp(); break; case 3: wm.Del_Emp(); break; case 4: wm.Mod_Emp(); break; case 5: wm.Find_Emp(); break; case 6: wm.Sort_Emp(); break; case 7: wm.Clean_File(); break; default: system("cls"); break; }
} system("pause"); return 0; }
|