当前位置: 首页 > news >正文

C++编程起步项目

员工信息管理系统

需求

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++标准库

http://www.xdnf.cn/news/515053.html

相关文章:

  • java中的Servlet1.x详解
  • 黑马k8s(十一)
  • LeetCode 155. 最小栈:Java 双栈解法详解
  • 【DeepSeek论文精读】11. 洞察 DeepSeek-V3:扩展挑战和对 AI 架构硬件的思考
  • STM32F103_LL库+寄存器学习笔记24 - TIM产生中心PWM波,中心对齐模式1 + PWM模式2(FOC算法专用)
  • AM32电调学习解读五:tenKhzRoutine
  • 第二十八天打卡
  • Linux常用命令44——bzip2压缩或解压缩.bz2文件
  • 【Spring】核心机制:IOC与DI深度解析
  • docker 安装 jenkins
  • 通俗解释Transformer在处理序列问题高效的原因(个人理解)
  • C++几何计算器
  • 【IP101】图像多尺度分析:金字塔结构的原理、构建与高级应用
  • 【SpringBoot】✈️整合飞书群机器人发送消息
  • JavaScript基础-获取元素
  • 【QGIS二次开发】地图编辑-09
  • python + pip 独家秘籍
  • printf函数参数与入栈顺序
  • 翻到了一段2005年写的关于需求的文字
  • java每日精进 5.18【文件存储】
  • Ubuntu 18.04设置静态IP的方法(图形化操作)
  • 美丽的独处时光
  • 菱形继承原理
  • java集合相关的api-总结
  • 2025年- H27-Lc135- 239.滑动窗口最大值(自定义双端队列)---java版
  • 量子计算在金融科技中的应用前景
  • [Codeforce刷题8]
  • 无废话离线大模型安装
  • 【随机过程】贝叶斯估计
  • 游戏引擎学习第292天:实现蛇