《数据结构与算法》课程设计
24、员工管理系统
问题描述:
每个员工的信息包括:编号、姓名、性别、出生年月、学历、职务、电话、住址等。系统能够完成员工信息的查询、更新、插入、删除、排序等功能。
实现要求:
(1) 排序:按不同关键字,对所有员工的信息进行排序。
(2) 查询:按特定条件查找员工。
(3) 更新:按编号对某个员工的某项信息进行修改。
(4) 插入:加入新员工的信息。
(5) 删除:按编号删除已离职的员工的信息。
#include <cstdio>
#include <iostream>
#include <map>
#include <iomanip>
#include <algorithm>using namespace std;
struct employee {
//存储结构string num; //编号string name; //姓名string gender; //性别string birth; //出生日期string education; //学历string position; //职务string phone; //电话string address; //住址
};employee em[1024];
int len = 0; //员工数量
int flag = 0; //控制系统是否退出//初始化员工信息
void init(employee t[]) {
len++, t[1].num = "1", t[1].name = "张三", t[1].gender = "男", t[1].birth = "2001-01-01", t[1].education = "本科", t[1].position = "经理", t[1].phone = "13970959821", t[1].address = "北京";len++, t[2].num = "2", t[2].name = "李四", t[2].gender = "女", t[2].birth = "2002-02-02", t[2].education = "大专", t[2].position = "策划", t[2].phone = "16846541231", t[2].address = "上海";len++, t[3].num = "3", t[3].name = "王五", t[3].gender = "男", t[3].birth = "2003-03-03", t[3].education = "中专", t[3].position = "职工", t[3].phone = "18174085740", t[3].address = "广州";len++, t[4].num = "4", t[4].name = "赵六", t[4].gender = "女", t[4].birth = "2004-04-04", t[4].education = "本科", t[4].position = "秘书", t[4].phone = "18670569826", t[4].address = "深圳";
}//打印表头
void emOutTitle() {
cout << setiosflags(ios::left) << setw(8) << "编号" << setw(8) << "姓名" << setw(8) << "性别" << setw(17) << "出生年月"<< setw(9) << "学历" << setw(10) << "职务" << setw(18) << "电话" << setw(8) << "住址" << endl;
}//打印员工信息
void emInfo(employee t[], int i) {
cout << setiosflags(ios::left) << setw(5) << t[i].num << setw(9) << t[i].name << setw(6) << t[i].gender<< setw(12) << t[i].birth << setw(9) << t[i].education << setw(10) << t[i].position << setw(15)<< t[i].phone << setw(8) << t[i].address << endl;
}//显示所有员工的信息
void emOut() {
emOutTitle();for (int i = 1; i <= len; i++)emInfo(em, i);
}//按编号降序排序
bool cmpNum(employee o, employee t) {
return o.num < t.num; }//按出生日期降序排序
bool cmpBirth(employee o, employee t) {
return o.birth < t.birth; }//按学历从高到低降序排序
bool cmpEdu(employee o, employee t) {
map<string, int> m_edu;m_edu["本科"] = 1;m_edu["大专"] = 2;m_edu["中专"] = 3;return m_edu[o.education] < m_edu[t.education];
}//按职务从高到低排序
bool cmpPosition(employee o, employee t) {
map<string, int> m_po;m_po["经理"] = 1;m_po["策划"] = 2;m_po["秘书"] = 3;m_po["司机"] = 4;m_po["职工"] = 5;return m_po[o.position] < m_po[t.position];
}//按照编号排序
void emSortNum() {
sort(em + 1, em + 1 + len, cmpNum); }//按照出生日期排序
void emSortBirth() {
sort(em + 1, em + 1 + len, cmpBirth); }//按照学历排序
void emSortEdu() {
sort(em + 1, em + 1 + len, cmpEdu); }//按照职务排序
void emSortPosition() {
sort(em + 1, em + 1 + len, cmpPosition); }//排序操作
void emSort() {
string key;printf("请输入关键词(编号/出生日期/学历/职务):");cin >> key;if (key == "编号") {
emSortNum(); //按编号排序emOut();} else if (key == "出生日期") {
emSortBirth(); //按出生日期排序emOut();} else if (key == "学历") {
emSortEdu(); //按学历排序emOut();} else if (key == "职务") {
emSortPosition(); //按职务排序emOut();} else {
printf("关键词不正确!\n");}
}//为新建员工的信息赋值
void emGet(int i, string num, string name, string gender, string birth, string education, string position, string phone,string address) {
em[i].num = num;em[i].name = name;em[i].gender = gender;em[i].birth = birth;em[i].education = education;em[i].position = position;em[i].phone = phone;em[i].address = address;
}//加入新员工的信息
void emInsert() {
string num;string name;string gender;string birth;string education;string position;string phone;string address;printf("请输入编号:");cin >> num;printf("请输入姓名:");cin >> name;printf("请输入性别:");cin >> gender;printf("请输入出生日期:");cin >> birth;printf("请输入学历:");cin >> education;printf("请输入职务:");cin >> position;printf("请输入电话:");cin >> phone;printf("请输入住址:");cin >> address;len++;emGet(len, num, name, gender, birth, education, position, phone, address); //插入新建的员工信息printf("插入成功!\n");
}//按编号删除已离职的员工信息
void emDelete() {
string num;printf("请输入要删除员工信息的编号:");cin >> num;int flag = 0;for (int i = 1; i <= len; i++) {
if (em[i].num == num) {
//查询到对应编号的员工for (int j = i; j <= len; j++) {
em[j] = em[j + 1]; //将后续元素前移一位}flag = 1;break;}}if (flag == 1) {
printf("删除成功!\n");len--; //员工数量减 1} else printf("删除失败!未查找到该员工!\n");
}//按编号查询员工信息
void emQueryNum(string num) {
int flag = 0;for (int i = 1; i <= len; i++) {
if (em[i].num == num) {
flag++;if (flag == 1) emOutTitle();emInfo(em, i);}}if (flag == 0) {
printf("不存在符合条件的员工!\n");} else {
printf("查找成功\n");}
}//按姓名查询员工信息
void emQueryName(string name) {
int flag = 0;for (int i = 1; i <= len; i++) {
if (em[i].name == name) {
flag++;if (flag == 1) emOutTitle();emInfo(em, i);}}if (flag == 0) {
printf("不存在符合条件的员工!\n");} else {
printf("查找成功\n");}
}//按职务查询员工信息
void emQueryPosition(string position) {
int flag = 0;for (int i = 1; i <= len; i++) {
if (em[i].position == position) {
flag++;if (flag == 1) emOutTitle();emInfo(em, i);}}if (flag == 0) {
printf("不存在符合条件的员工!\n");} else {
printf("查找成功\n");}
}//按学历查询员工信息
void emQueryEdu(string education) {
int flag = 0;for (int i = 1; i <= len; i++) {
if (em[i].education == education) {
flag++;if (flag == 1) emOutTitle();emInfo(em, i);}}if (flag == 0) {
printf("不存在符合条件的员工!\n");} else {
printf("查找成功\n");}
}//查询操作
void emQuery() {
string key;string s;printf("请输入查询信息:");cin >> key;printf("请输入查询内容:");cin >> s;if (key == "学历") {
emQueryEdu(s);} else if (key == "姓名") {
emQueryName(s);} else if (key == "编号") {
emQueryNum(s);} else if (key == "职务") {
emQueryPosition(s);} else {
printf("查询条件输入错误!\n");}
}//按编号修改员工姓名
void emUpdateName(string num, string name) {
int flag = 0;for (int i = 1; i <= len; i++) {
if (em[i].num == num) {
flag = 1;em[i].name = name;if (flag == 1) emOutTitle();emInfo(em, i);break;}}if (flag == 0) {
printf("更新失败!\n");} else {
printf("更新成功!\n"); }
}//按编号修改员工性别
void emUpdateGender(string num, string gender) {
int flag = 0;for (int i = 1; i <= len; i++) {
if (em[i].num == num) {
flag = 1;em[i].gender = gender;if (flag == 1) emOutTitle();emInfo(em, i);break;}}if (flag == 0) {
printf("更新失败!\n");} else {
printf("更新成功!\n"); }
}//按编号修改员工出生日期
void emUpdateBirth(string num, string birth) {
int flag = 0;for (int i = 1; i <= len; i++) {
if (em[i].num == num) {
flag = 1;em[i].birth = birth;if (flag == 1) emOutTitle();emInfo(em, i);break;}}if (flag == 0) {
printf("更新失败!\n");} else {
printf("更新成功!\n"); }
}//按编号修改员工学历
void emUpdateEducation(string num, string education) {
int flag = 0;for (int i = 1; i <= len; i++) {
if (em[i].num == num) {
flag = 1;em[i].education = education;if (flag == 1) emOutTitle();emInfo(em, i);break;}}if (flag == 0) {
printf("更新失败!\n");} else {
printf("更新成功!\n"); }
}//按编号修改员工职务
void emUpdatePosition(string num, string position) {
int flag = 0;for (int i = 1; i <= len; i++) {
if (em[i].num == num) {
flag = 1;em[i].position = position;if (flag == 1) emOutTitle();emInfo(em, i);break;}}if (flag == 0) {
printf("更新失败!\n");} else {
printf("更新成功!\n"); }
}//按编号修改员工电话
void emUpdatePhone(string num, string phone) {
int flag = 0;for (int i = 1; i <= len; i++) {
if (em[i].num == num) {
flag = 1;em[i].phone = phone;if (flag == 1) emOutTitle();emInfo(em, i);break;}}if (flag == 0) {
printf("更新失败!\n");} else {
printf("更新成功!\n"); }
}//按编号修改员工住址
void emUpdateAddress(string num, string address) {
int flag = 0;for (int i = 1; i <= len; i++) {
if (em[i].num == num) {
flag = 1;em[i].address = address;if (flag == 1) emOutTitle();emInfo(em, i);break;}}if (flag == 0) {
printf("更新失败!\n");} else {
printf("更新成功!\n"); }
}//更新操作
void emUpdate() {
string num;string key;string s;printf("请输入要更新员工的编号:");cin >> num;printf("请输入要更新员工的信息:");cin >> key;printf("请输入要更新的内容:");cin >> s;if (key == "姓名") {
emUpdateName(num, s);} else if (key == "性别") {
emUpdateGender(num, s);} else if (key == "出生年月") {
emUpdateBirth(num, s);} else if (key == "学历") {
emUpdateEducation(num, s);} else if (key == "职务") {
emUpdatePosition(num, s);} else if (key == "电话") {
emUpdatePhone(num, s);} else if (key == "住址") {
emUpdateAddress(num, s);} else {
printf("更新内容有误!\n");}
}void interface(int order) {
if (order == 1) {
emSort();} else if (order == 2) {
emQuery();} else if (order == 3) {
emUpdate();} else if (order == 4) {
emInsert();} else if (order == 5) {
emDelete();} else if (order == 6) {
emOut();} else if (order == 0) {
flag = 1;} else {
printf("指令不正确!请重新输入指令!\n");}
}int main() {
init(em);while (!flag) {
printf("===================================================================================================\n");printf(" ********** 员 工 管 理 系 统 ********** \n");printf(" 1.排序 2.查询 3.更新 4.插入 5.删除 6.显示当前员工 0.退出 \n");printf("===================================================================================================\n");printf("请输入指令:");int order;cin >> order;interface(order);printf("\n");}return 0;
}