員工信息管理系統
需求
Employee.h
#pragma once#include<iostream>
#include<string>using namespace std;class Employee {
public:int id; // 編號string name; // 姓名string position; // 崗位int deptId; // 部門編號Employee();Employee(int id, string name, string position, int deptId);~Employee();virtual string responsibilities() const { return "員工職責!"; };void setName(string name);
};class PuTong : public Employee {
public:PuTong(int id, string name, string position, int deptId);virtual string responsibilities() const;
};
class JingLi : public Employee {
public:JingLi(int id, string name, string position, int deptId);virtual string responsibilities() const;
};
class LaoBan : public Employee {
public:LaoBan(int id, string name, string position, int deptId);virtual string responsibilities() const;
};
Employee.cpp
#include"Employee.h"void Employee::setName(string name) {this->name = name;
}Employee::Employee() :id(0), name(""), position(""), deptId(0) {
}Employee::Employee(int id, string name, string position, int deptId) {this->id = id;this->name = name;this->position = position;this->deptId = deptId;
}Employee::~Employee() {}PuTong::PuTong(int id, string name, string position, int deptId) {this->id = id;this->name = name;this->position = position;this->deptId = deptId;
}
string PuTong::responsibilities() const {return "完成經理交給的任務.";
}JingLi::JingLi(int id, string name, string position, int deptId) {this->id = id;this->name = name;this->position = position;this->deptId = deptId;
}
string JingLi::responsibilities() const {return "完成老板交給的任務,并下發任務給員工.";
}LaoBan::LaoBan(int id, string name, string position, int deptId) {this->id = id;this->name = name;this->position = position;this->deptId = deptId;
}
string LaoBan::responsibilities() const {return "管理公司所有事務.";
}
EmployeeManager.cpp
#pragma once#include<iostream>
#include<string>
#include<fstream>#include "outer.h"using namespace std;static const int MAX_EMP_COUNT = 10;
//Employee emp[MAX_EMP_COUNT]; // 數組 類似結構體數組
Employee* emp = new Employee[MAX_EMP_COUNT]; // 動態分配的數組
Employee** p_emp = new Employee* [MAX_EMP_COUNT]; // Employee指針數組 Employee為抽象類時可以實現多態
//auto emp = make_unique<Employee[]>(MAX_EMP_COUNT);
static int emp_size = 0; // 員工數 限制emp訪問區間
static const string DATA = "data.dat";static void add_emp(Employee& e) {if (emp_size == MAX_EMP_COUNT) { // 擴容cout << "已經存滿了!" << endl;}else {emp[emp_size] = e;p_emp[emp_size] = &e;emp_size++;}
}
static void print_emp(const Employee& e) {cout << "員工[" << e.id << "] ->"<< " 姓名: " << e.name<< " 崗位: " << e.position<< " 部門編號: " << e.deptId<< " 職責:" << e.responsibilities()<< endl;
}
static void print_emp(const Employee* const e) {cout << "員工[" << e->id << "] ->"<< " 姓名: " << e->name<< " 崗位: " << e->position<< " 部門編號: " << e->deptId<< " 職責:" << e->responsibilities()<< endl;
}
static void show_all_emp() {if (emp_size <= 0) {return;}cout << "所有員工信息如下:" << endl;for (int i = 0; i < emp_size; i++) {Employee e = emp[i];print_emp(&e);}
}
static Employee& search_emp(int id) {for (int i = 0; i < emp_size; i++) {if (emp[i].id == id) {return emp[i];}}cout << "沒有找到" << endl;exit(-1);
}
static Employee& search_emp(string name) {for (int i = 0; i < emp_size; i++) {if (emp[i].name == name) {return emp[i];}}cout << "沒有找到" << endl;exit(-1);
}
static bool del_emp(int id) {for (int i = 0; i < emp_size; i++) {if (emp[i].id == id) {while (i < emp_size - 1) {emp[i] = emp[i + 1];i++;}emp_size--;return true;}}return false;
}
static void modify_emp(int id, string name) {Employee& e = search_emp(id);e.name = name;
}static void add() {int id; // 編號string name; // 姓名string position; // 崗位int deptId; // 部門編號while (true) {cout << "輸入-1隨時退出!" << endl;cout << "請輸入編號:";cin >> id;if (id == -1) {break;}//Employee &x = search_emp(id);cout << "請輸入姓名:";cin >> name;if (name == "-1") {break;}pos: cout << "請輸入崗位[1:普通 2:經理 3:老板]:";cin >> position;if (position == "-1") {break;}Employee* e;if (position == "1") {e = new PuTong(id, name, position, 0);}else if (position == "2") {e = new JingLi(id, name, position, 0);}else if (position == "3") {e = new LaoBan(id, name, position, 0);}else {cout << "無效輸入!" << endl;goto pos;}cout << "請輸入部門編號:";cin >> deptId;if (deptId == -1) {break;}//Employee* e = new Employee(id, name, position, deptId);//Employee e(id, name, position, deptId);e->deptId = deptId; add_emp(*e);show_all_emp();cout << "選擇繼續添加還是退出:0 表示退出,1 表示繼續添加. ";int select;cin >> select;if (!select) {break;}}
}
static void del() {cout << "選擇要刪除的員工的編號:";int id;cin >> id;bool d = del_emp(id);if (d) {cout << "刪除成功!" << endl;}else {cout << "刪除失敗!" << endl;}show_all_emp();
}
static void modify() {cout << "選擇要修改的員工的編號:";int id;cin >> id;Employee &e = search_emp(id);cout << "修改姓名:";string name;cin >> name;modify_emp(id, name);show_all_emp();
}
static void search() {cout << "輸入1按編號查找,輸入2按姓名查找.";int select;cin >> select;if (select == 1) {int id;cout << "輸入編號:";cin >> id;Employee &e = search_emp(id);cout << "查找到的員工信息如下:" << endl;print_emp(&e);} else if(select == 2) {string name;cout << "輸入姓名:";cin >> name;Employee& e = search_emp(name);cout << "查找到的員工信息如下:" << endl;print_emp(&e);}
}
static int comp(const void *e1, const void *e2) {const Employee s1 = *static_cast<const Employee*>(e1);const Employee s2 = *static_cast<const Employee*>(e2);return s1.id - s2.id;//return ((Employee*)e1)->id - ((Employee*)e2)->id;
}
// 泛型比較方法,比較id
template<typename T>
static int comp_id(const void* e1, const void* e2) {const T t1 = *static_cast<const T*>(e1);const T t2 = *static_cast<const T*>(e2);return t1.id - t2.id;
}
static void sort() {qsort(emp, emp_size, sizeof(Employee), comp_id<Employee>);
}static void init() {fstream in(DATA, ios::in);Employee* e = new Employee;while (in.read((char*)e, sizeof(Employee))) {emp[emp_size++] = *e;}//p_emp = new Employee* [emp_size];for (int i = 0; i < emp_size; i++) {p_emp[i] = &emp[i];}//int i = 0;//while (getline(in)) {// int id, deptId;// string name, position;// in >> id >> name >> position >> deptId;// Employee e(id, name, position, deptId);// emp[i] = e;//}
}
static void store_file() {cout << "文件同步..." << endl;fstream out(DATA, ios::out);for (int i = 0; i < emp_size; i++) {Employee e = emp[i];out.write((const char*)&e, sizeof(Employee));//out << e.id << " "// << e.name << " "// << e.position << " "// << e.deptId// << endl;}
}
static void clear_file(const string name) {fstream out(DATA, ios::out|ios::trunc);out.close();
}
static void clear() {cout << "操作不可恢復!確定清空數據嗎?" << endl;cout << "輸入數字0取消!" << endl;int n;cin >> n;if (!n) {return;}emp_size = 0;delete[] emp; // 刪除動態分配的數組emp = new Employee[MAX_EMP_COUNT];//emp = NULL;//emp = make_unique<Employee[]>(MAX_EMP_COUNT);clear_file(DATA);
}static void menu() {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 start() {init();int flag = 1;int cls = 0;
label: while (flag) {show_all_emp();menu();cout << "輸入要執行的操作 : ";int select;cin >> select;switch(select) {case 0:flag = 0;goto label;case 1:add();store_file();break;case 2:show_all_emp();break;case 3:del();store_file();break;case 4:modify();store_file();break;case 5:search();break;case 6:sort();store_file();system("cls");goto label;case 7:clear();break;default:cout << "無效選項" << endl;}system("pause");system("cls");}cout << "退出系統,歡迎使用!" << endl;
}
outer.h
#pragma once#include "Employee.h"// 員工信息管理系統
void start();// 測試多態
void test_polymorphism();
測試多態
Worker.h
#pragma once#include<string>using namespace std;class Worker {
public:int id; // 編號string name; // 姓名string position; // 崗位//Worker();//Worker(int id, string name, string position);virtual string showInfo() const = 0;
};class Worker1 : public Worker {
public:Worker1(int id, string name, string position);virtual string showInfo() const;
};class Worker2 : public Worker {
public:Worker2(int id, string name, string position);virtual string showInfo() const;
};
Worker.cpp
#include<string>#include"Worker.h"using namespace std;Worker1::Worker1(int id, string name, string position) {this->id = id;this->name = name;this->position = position;
}
string Worker1::showInfo() const {return "完成經理交給的任務.";
}Worker2::Worker2(int id, string name, string position) {this->id = id;this->name = name;this->position = position;
}
string Worker2::showInfo() const {return "完成老板交給的任務,并下發任務給員工.";
}
WorkerManager.cpp
#include<iostream>
#include<string>#include"Worker.h"using namespace std;static int init_size = 1;
static Worker** worker_arry = new Worker*[init_size];
static int worker_size = 0;static void add(Worker* w) {if (worker_size == init_size) {Worker** w_arr = new Worker * [2 * init_size];for (int i = 0; i < worker_size; i++) {w_arr[i] = worker_arry[i];}delete[] worker_arry;worker_arry = w_arr;}worker_arry[worker_size++] = w;
}
static void add() {cout << "分別輸入id、name、position[1:普通、2:經理]." << endl;Worker* w = NULL;int id;cout << "輸入id: ";cin >> id;string name;cout << "輸入name: ";cin >> name;string position;
pos: cout << "輸入position: ";cin >> position;if (position == "1") {w = new Worker1(id, name, position);}else if (position == "2") {w = new Worker2(id, name, position);}else {cout << "position輸入有誤!" << endl;goto pos;}add(w);
}
static void print_worker(const Worker* const w) {cout << "id: " << w->id<< " name: " << w->name<< " position: " << w->position<< " 職責: " << w->showInfo()<< endl;
}
static void print_worker_array() {for (int i = 0; i < worker_size; i++) {Worker* w = worker_arry[i];print_worker(w);}
}void test_polymorphism() {bool flag = true;while (flag) {int s;cout << "輸入0退出. " << "輸入1添加." << endl;cin >> s;switch (s){case 0:system("pause");exit(1);//flag = false;//break;case 1:add();print_worker_array();default:break;}}
}
測試類
int main(int argc, char *argv[])
{// 面向對象設計start();test_polymorphism();return 1;
}
下一步:C++標準庫