1 //boss.h 2 #pragma once 3 #include <iostream> 4 using namespace std; 5 #include "worker.h" 6 class Boss :public Worker 7 { 8 public: 9 //构造函数 10 Boss(int id, string name, int dId); 11 //显示个人信息 12 void showInfo(); 13 //获取岗位名称 14 string getDeptName(); 15 };
1 //employee.h 2 #pragma once 3 #include <iostream> 4 using namespace std; 5 #include <string> 6 #include "worker.h" 7 class Employee :public Worker 8 { 9 public: 10 //构造函数 11 Employee(int id,string name,int dId); 12 //显示个人信息 13 void showInfo(); 14 //获取岗位名称 15 string getDeptName(); 16 };
1 //manager.h 2 #pragma once 3 #include <iostream> 4 using namespace std; 5 #include "worker.h" 6 class Manager :public Worker 7 { 8 public: 9 //构造函数 10 Manager(int id, string name, int dId); 11 //显示个人信息 12 void showInfo(); 13 //获取岗位名称 14 string getDeptName(); 15 };
1 //worker.h 2 //worker是boss,manager,employee的父类 3 #pragma once 4 #include <iostream> 5 using namespace std; 6 #include <string> 7 class Worker 8 { 9 public: 10 //显示个人信息 11 virtual void showInfo() = 0; 12 13 //获取岗位名称 14 virtual string getDeptName() = 0; 15 16 int m_ID; 17 string m_Name; 18 int m_DeptID; 19 };
1 //workerManager.h 2 #pragma once 3 #include <iostream> 4 #include "worker.h" 5 #include "employee.h" 6 #include "manager.h" 7 #include "boss.h" 8 #include <fstream> 9 #define FILENAME "empFile.txt" 10 using namespace std; 11 class WorkerManager 12 { 13 public: 14 WorkerManager(); 15 ~WorkerManager(); 16 void show_Menu();//展示菜单 17 void ExitSystem();//退出系统 18 int m_EmpNum; //记录职工人数 19 Worker** m_EmpArray;//职工数组指针 20 void Add_Emp();//添加职工 21 void save();//保存文件 22 bool m_FileIsEmpty;//标志文件是否为空 23 int get_Empnum();//统计文件中的人数 24 void init_Emp();//初始化职工 25 void Show_Emp();//显示职工 26 void Del_Emp();//删除职工 27 int IsExist(int id);//判断职工是否存在 28 void Mod_Emp();//修改职工 29 void Find_Emp(); 30 void Sort_Emp(); 31 void Clean_File(); 32 };
1 //boss.cpp 2 #include "boss.h" 3 Boss::Boss(int id, string name, int dId) 4 { 5 this->m_ID = id; 6 this->m_Name = name; 7 this->m_DeptID = dId; 8 } 9 void Boss::showInfo() 10 { 11 cout << "职工编号:" << this->m_ID 12 << "\t职工姓名:" << this->m_Name 13 << "\t岗位:" << this->getDeptName() 14 << "\t岗位:管理公司所有事务" << endl; 15 } 16 string Boss::getDeptName() 17 { 18 return string("老板"); 19 }
1 //employee.cpp 2 #include "employee.h" 3 Employee::Employee(int id, string name, int dId) 4 { 5 this->m_ID = id; 6 this->m_Name = name; 7 this->m_DeptID = dId; 8 } 9 void Employee::showInfo() 10 { 11 cout << "职工编号:" << this->m_ID 12 << "\t职工姓名:" << this->m_Name 13 <<"\t岗位:" << this->getDeptName() 14 << "\t岗位:完成经理交给的任务" << endl; 15 } 16 string Employee::getDeptName() 17 { 18 return string("员工"); 19 }
1 //manager.cpp 2 #include "manager.h" 3 Manager::Manager(int id, string name, int dId) 4 { 5 this->m_ID = id; 6 this->m_Name = name; 7 this->m_DeptID = dId; 8 } 9 void Manager::showInfo() 10 { 11 cout << "职工编号:" << this->m_ID 12 << "\t职工姓名:" << this->m_Name 13 <<"\t岗位:" << this->getDeptName() 14 << "\t岗位:完成老板交给的任务,下发任务给普通员工" << endl; 15 } 16 string Manager::getDeptName() 17 { 18 return string("经理"); 19 }
1 //workerManager.cpp 2 #include "workerManager.h" 3 #include"worker.h" 4 WorkerManager::WorkerManager() 5 { 6 ifstream ifs; 7 ifs.open(FILENAME, ios::in); 8 //文件不存在情况 9 if (!ifs.is_open()) 10 { 11 cout << "文件不存在" << endl; 12 this->m_EmpNum = 0; 13 this->m_FileIsEmpty = true; 14 this->m_EmpArray = NULL; 15 ifs.close(); 16 return; 17 } 18 //文件存在数据为空 19 char ch; 20 ifs >> ch; 21 if (ifs.eof()) 22 { 23 //文件为空 24 cout << "文件为空" << endl; 25 this->m_EmpNum = 0; 26 this->m_FileIsEmpty = true; 27 this->m_EmpArray = NULL; 28 ifs.close(); 29 return; 30 } 31 //文件存在,并且记录数据 32 int num = this->get_Empnum(); 33 cout << "职工人数为:" << num << endl; 34 this->m_EmpNum = num; 35 //根据职工数创建数组 36 this->m_EmpArray = new Worker * [this->m_EmpNum]; 37 //初始化职工 38 init_Emp(); 39 40 41 } 42 //WorkerManager::~WorkerManager() 43 //{ 44 // 45 //} 46 void WorkerManager::show_Menu() 47 { 48 cout << "*****************************" << endl; 49 cout << "*****欢迎来到职工管理系统****" << endl; 50 cout << "****** 0.退出管理程序 *******" << endl; 51 cout << "****** 1.增加职工信息 *******" << endl; 52 cout << "****** 2.显示职工信息 *******" << endl; 53 cout << "****** 3.删除离职职工 *******" << endl; 54 cout << "****** 4.修改职工信息 *******" << endl; 55 cout << "****** 5.查找职工信息 *******" << endl; 56 cout << "****** 6.按照编号排序 *******" << endl; 57 cout << "****** 7.清空所有文档 *******" << endl; 58 cout << "*****************************" << endl; 59 cout << endl; 60 } 61 void WorkerManager::ExitSystem() 62 { 63 cout << "欢迎下次使用" << endl; 64 system("pause"); 65 exit(0);//退出程序 66 } 67 void WorkerManager::Add_Emp() 68 { 69 cout << "请输入添加职工的数量:" << endl; 70 int addNum = 0; 71 cin >> addNum; 72 if (addNum > 0) 73 { 74 //新空间的人数 = 原来记录人数+新增人数 75 int newSize = this->m_EmpNum + addNum; 76 Worker** newSpace = new Worker * [newSize];//开辟新空间 77 78 //将原来空间下数据,拷贝到新空间下 79 if (this->m_EmpArray != NULL) 80 { 81 for (int i = 0; i < this->m_EmpNum; i++) 82 { 83 newSpace[i] = this->m_EmpArray[i]; 84 } 85 } 86 //批量添加新数据 87 for (int i = 0; i < addNum; i++) 88 { 89 int id; 90 string name; 91 int dSelect; 92 cout << "请输入第" << i + 1 << "个新职工编号" << endl; 93 cin >> id; 94 cout << "请输入第" << i + 1 << "个新职工姓名" << endl; 95 cin >> name; 96 cout << "请选择该职工岗位:" << endl; 97 cout << "1、普通职工" << endl; 98 cout << "2、经理" << endl; 99 cout << "3、老板" << endl; 100 cin >> dSelect; 101 102 Worker* worker = NULL; 103 switch (dSelect) 104 { 105 case 1: 106 worker = new Employee(id, name, 1); 107 break; 108 case 2: 109 worker = new Manager(id, name, 2); 110 break; 111 case 3: 112 worker = new Boss(id, name, 3); 113 break; 114 default: 115 break; 116 } 117 //将创建的职工职责,保存到数组中 118 newSpace[this->m_EmpNum + i] = worker; 119 } 120 //释放原有空间 121 delete[] this->m_EmpArray; 122 //更改新空间的指向 123 this->m_EmpArray = newSpace; 124 //更新新的职工人数 125 this->m_EmpNum = newSize; 126 //更新职工不为空 127 this->m_FileIsEmpty = false; 128 129 //显示添加成功 130 cout << "成功添加" << addNum << "名新职工" << endl; 131 132 //添加成功后保存到文件中 133 this->save(); 134 } 135 else 136 { 137 cout << "输入有误" << endl; 138 } 139 //按任意键清屏回到上一级目录 140 system("pause"); 141 system("cls"); 142 } 143 144 WorkerManager::~WorkerManager() 145 { 146 if (this->m_EmpArray != NULL) 147 { 148 for (int i = 0; i < this->m_EmpNum; i++) 149 { 150 if (this->m_EmpArray[i] = NULL) 151 { 152 delete this->m_EmpArray[i]; 153 } 154 } 155 delete[] this->m_EmpArray; 156 this->m_EmpArray = NULL; 157 } 158 } 159 void WorkerManager::save() 160 { 161 ofstream ofs; 162 ofs.open(FILENAME, ios::out); 163 for (int i = 0; i < this->m_EmpNum; i++) 164 { 165 ofs << this->m_EmpArray[i]->m_ID << " " 166 << this->m_EmpArray[i]->m_Name << " " 167 << this->m_EmpArray[i]->m_DeptID << endl; 168 } 169 ofs.close(); 170 } 171 int WorkerManager::get_Empnum() 172 { 173 ifstream ifs; 174 ifs.open(FILENAME, ios::in); 175 int id; 176 string name; 177 int dId; 178 int num = 0; 179 while (ifs >> id && ifs >> name && ifs >> dId) 180 { 181 num++; 182 } 183 ifs.close(); 184 return num; 185 } 186 void WorkerManager::init_Emp() 187 { 188 ifstream ifs; 189 ifs.open(FILENAME,ios::in); 190 int id; 191 string name; 192 int dId; 193 int index = 0; 194 while (ifs >> id && ifs >> name && ifs >> dId) 195 { 196 Worker* worker = NULL; 197 if (dId == 1) 198 { 199 worker = new Employee(id, name, dId); 200 } 201 else if (dId == 2) 202 { 203 worker = new Manager(id, name, dId); 204 } 205 else 206 { 207 worker = new Boss(id, name, dId); 208 } 209 //存放在数组中 210 this->m_EmpArray[index] = worker; 211 index++; 212 } 213 } 214 void WorkerManager::Show_Emp() 215 { 216 if (this->m_FileIsEmpty) 217 { 218 cout << "文件不存在或者记录为空" << endl; 219 } 220 else 221 { 222 for (int i = 0; i < m_EmpNum; i++) 223 { 224 this->m_EmpArray[i]->showInfo(); 225 } 226 } 227 system("pause"); 228 system("cls"); 229 } 230 void WorkerManager::Del_Emp() 231 { 232 if (this->m_FileIsEmpty) 233 { 234 cout << "文件不存在或记录为空" << endl; 235 } 236 else 237 { 238 cout << "请输入想要删除职工编号:" << endl; 239 int id = 0; 240 cin >> id; 241 int index = this->IsExist(id); 242 if (index != -1) 243 { 244 for (int i = index; i < this->m_EmpNum - 1; i++) 245 { 246 this->m_EmpArray[i] = this->m_EmpArray[i + 1]; 247 } 248 this->m_EmpNum--; 249 this->save(); 250 cout << "删除成功" << endl; 251 } 252 else 253 { 254 cout << "删除失败,未找到该职工" << endl; 255 } 256 } 257 system("pause"); 258 system("cls"); 259 } 260 261 int WorkerManager::IsExist(int id) 262 { 263 int index = -1; 264 for (int i = 0; i < this->m_EmpNum; i++) 265 { 266 if (this->m_EmpArray[i]->m_ID == id) 267 { 268 index = i; 269 break; 270 } 271 } 272 return index; 273 } 274 void WorkerManager::Mod_Emp() 275 { 276 if (this->m_FileIsEmpty) 277 { 278 cout << "文件不存在或记录为空" << endl; 279 } 280 else 281 { 282 cout << "请输入修改职工的编号:" << endl; 283 int id; 284 cin >> id; 285 int ret = this->IsExist(id); 286 if (ret != -1) 287 { 288 delete this->m_EmpArray[ret]; 289 int newID = 0; 290 string newName = ""; 291 int dSelect = 0; 292 cout << "查到:" << id << "号职工,请输入新职工号:" << endl; 293 cin >> newID; 294 cout << "请输入新姓名:" << endl; 295 cin >> newName; 296 cout << "请输入岗位:" << endl; 297 cout << "1、普通职工" << endl; 298 cout << "2、经理" << endl; 299 cout << "3、老板" << endl; 300 cin >> dSelect; 301 302 Worker* worker = NULL; 303 switch (dSelect) 304 { 305 case 1: 306 worker = new Employee(newID, newName, dSelect); 307 break; 308 case 2: 309 worker = new Manager(newID, newName, dSelect); 310 break; 311 case 3: 312 worker = new Boss(newID, newName, dSelect); 313 break; 314 default: 315 break; 316 } 317 this->m_EmpArray[ret] = worker; 318 cout << "修改成功!" << endl; 319 this->save(); 320 } 321 else 322 { 323 cout << "修改失败,查无此人" << endl; 324 } 325 } 326 system("pause"); 327 system("cls"); 328 } 329 void WorkerManager::Find_Emp() 330 { 331 if (this->m_FileIsEmpty) 332 { 333 cout << "文件不存在或记录为空" << endl; 334 } 335 else 336 { 337 cout << "请输入查找的方式" << endl; 338 cout << "1、按照职工编号查找" << endl; 339 cout << "2、按照姓名查找" << endl; 340 int select = 0; 341 cin >> select; 342 if (select == 1) 343 { 344 int id; 345 cout << "请输入查找的职工号:" << endl; 346 cin >> id; 347 int ret = IsExist(id); 348 if (ret != -1) 349 { 350 cout << "查找成功!职工信息如下:" << endl; 351 this->m_EmpArray[ret]->showInfo(); 352 } 353 else 354 { 355 cout << "查找失败,查无此人" << endl; 356 } 357 } 358 else if (select == 2) 359 { 360 string name; 361 cout << "请输入查找的姓名:" << endl; 362 cin >> name; 363 364 bool flag = false; 365 for (int i = 0; i < m_EmpNum; i++) 366 { 367 if (m_EmpArray[i]->m_Name == name) 368 { 369 cout << "查找成功,职工编号为:" 370 << m_EmpArray[i]->m_ID 371 << "号的信息如下" << endl; 372 flag = true; 373 this->m_EmpArray[i]->showInfo(); 374 } 375 } 376 if (flag == false) 377 { 378 cout << "查找失败,查无此人" << endl; 379 } 380 } 381 } 382 system("pause"); 383 system("cls"); 384 } 385 void WorkerManager::Sort_Emp() 386 { 387 if (this->m_FileIsEmpty) 388 { 389 cout << "文件不存在或记录为空" << endl; 390 system("pause"); 391 system("cls"); 392 } 393 else 394 { 395 cout << "请选择排序方式:" << endl; 396 cout << "1、按职工号升序" << endl; 397 cout << "2、按职工号降序" << endl; 398 int select = 0; 399 cin >> select; 400 for (int i = 0; i < m_EmpNum; i++) 401 { 402 int minOrmax = i; 403 for (int j = i + 1; j < m_EmpNum; j++) 404 { 405 if (select == 1) 406 { 407 if (m_EmpArray[minOrmax]->m_ID < m_EmpArray[j]->m_ID) 408 { 409 minOrmax = j; 410 } 411 } 412 else 413 { 414 if (m_EmpArray[minOrmax]->m_ID > m_EmpArray[j]->m_ID) 415 { 416 minOrmax = j; 417 } 418 } 419 } 420 if (i != minOrmax) 421 { 422 Worker* temp = m_EmpArray[i]; 423 m_EmpArray[i] = m_EmpArray[minOrmax]; 424 m_EmpArray[minOrmax] = temp; 425 } 426 } 427 cout << "排序成功,排序后结果为:" << endl; 428 this->save(); 429 this->Show_Emp(); 430 } 431 } 432 433 void WorkerManager::Clean_File() 434 { 435 cout << "确认清空?" << endl; 436 cout << "1、确认" << endl; 437 cout << "2、返回" << endl; 438 int select = 0; 439 cin >> select; 440 if (select == 1) 441 { 442 ofstream ofs(FILENAME, ios::trunc);//删除文件后重新创建 443 ofs.close(); 444 if (this->m_EmpArray != NULL) 445 { 446 for (int i = 0; i < this->m_EmpNum; i++) 447 { 448 if (this->m_EmpArray[i] != NULL) 449 { 450 delete this->m_EmpArray[i]; 451 } 452 } 453 this->m_EmpNum = 0; 454 delete[] this->m_EmpArray; 455 this->m_EmpArray = NULL; 456 this->m_FileIsEmpty = true; 457 } 458 cout << "清除成功" << endl; 459 } 460 system("pause"); 461 system("cls"); 462 }
1 //职工管理系统.cpp 2 #include<iostream> 3 #include "WorkerManager.h" 4 #include "worker.h" 5 #include "employee.h" 6 #include "manager.h" 7 #include "boss.h" 8 using namespace std; 9 10 int main() 11 { 12 ////测试代码 13 //Worker* worker = NULL; 14 //worker = new Employee(1, "张三", 1); 15 //worker->showInfo(); 16 //delete worker; 17 //Manager* manager = NULL; 18 //manager = new Manager(1, "张三", 1); 19 //manager->showInfo(); 20 //delete manager; 21 //Boss* boss = NULL; 22 //boss = new Boss(1, "张三", 1); 23 //boss->showInfo(); 24 //delete boss; 25 //实例化管理者对象 26 WorkerManager wm; 27 int choice = 0;//用来存储用户的选项 28 while (true) 29 { 30 wm.show_Menu(); 31 cout << "请输入您的选择:" << endl; 32 cin >> choice; 33 switch (choice) 34 { 35 case 0: //退出系统 36 wm.ExitSystem(); 37 break; 38 case 1: //增加职工 39 wm.Add_Emp(); 40 break; 41 case 2: //显示职工 42 wm.Show_Emp(); 43 break; 44 case 3: //删除职工 45 wm.Del_Emp(); 46 break; 47 case 4: //修改职工 48 wm.Mod_Emp(); 49 break; 50 case 5: //查找职工 51 wm.Find_Emp(); 52 break; 53 case 6: //排序职工 54 wm.Sort_Emp(); 55 break; 56 case 7: //清空文档 57 wm.Clean_File(); 58 break; 59 default: 60 system("cls"); 61 break; 62 } 63 } 64 system("pause"); 65 return 0; 66 }
标签:职工,cout,管理系统,int,EmpArray,worker,2023.5,include,id From: https://www.cnblogs.com/muzhaodi/p/17380123.html