C++实现员工信息管理系统


using namespace std;

#include
#include
#include

/**
* 员工信息结构体
*/
struct career {
char id[10];
char name[10];
char sex[2];
int age;
char position[20];
};

/**
* 员工链表元素
*/
class link {
public:
career data;
link *next;
};

// 链头
link *p;

/**
* 操作提示
*/
void showOperations();

/**
* 新建信息
*/
void enter();

/**
* 删除信息
*/
void deleteCareer(link *q, char a[]);

/**
* 根据姓名修改信息
*/
void modify(link *q, char a[]);

/**
* 根据职位查询信息
*/
void search(link *q, char sc[12]);

/**
* 显示所有信息
*/
void showAll();

/**
* 保存信息到磁盘
*/
void save();

/**
* 读取已有信息到链表
*/
link *read();

/**
* 处理各种操作
*/
void dealWithEvents();

/**
* 主函数
*/
int main(){
showOperations();
p = new link();
p = read();
dealWithEvents();
return 0;
}

void showOperations(){
cout
<< "********************************************************************************"
<< "\n";
cout << "1 " << "新建员工" << "\t";
cout << "2 " << "根据姓名删除员工" << "\t";
cout << "3 " << "根据姓名修改员工" << "\t";
cout << "4 " << "根据职位查询员工" << "\n";
cout << "5 " << "查询全体员工信息" << "\t";
cout << "6 " << "存档" << "\t";
cout << "7 " << "退出程序" << "\t";
cout << "8 " << "帮助" << "\n";
cout
<< "********************************************************************************"
<< "\n";
}

void enter()
{
link *s;
s = new link;
cout << "请输入员工信息:\n 员工号 " << " 姓名 " << " 性别 " << " 年龄 " << " 职位 ";
cin >> s->data.id;
cin >> s->data.name;
cin >> s->data.sex;
cin >> s->data.age;
cin >> s->data.position;
s->next = p->next;
p->next = s;
}
void deleteCareer(link *q, char a[])
{
link *r, *t;//link *r,*ss;
t = q;
r = q->next;

if (r == NULL) {
cout << "员工信息为空\n";
return;
}
while ((r != NULL) && (strcmp(r->data.name, a) != 0)) {
t = r;
r = r->next;
}
if (r == NULL)
cout << "员工姓名不存在" << "\n";
else {
t->next = r->next;
delete (r);
cout << "此员工 \"" << a << "\"被删除" << "\n";
}
}
void modify(link *q, char a[])
{
link *r;
r = q->next;
while ((r != NULL) && (strcmp(r->data.name, a) != 0))
r = r->next;
if (r == NULL)
cout << "员工名不存在" << "\n";
else {
cout << "请输入新的员工信息: \n 员工号 " << " 姓名 " << " 性别 " << " 年龄 " << " 职位 \n";
cin >> r->data.id;
cin >> r->data.name;
cin >> r->data.sex;
cin >> r->data.age;
cin >> r->data.position;
}

}
void search(link *q, char sc[12]) {
link *r;
r = q->next;
if (r == NULL) {
cout << "职位不存在";
return;
}
cout << "员工号" << "\t";
cout << "姓名" << "\t";
cout << "性别" << "\t";
cout << "年龄" << "\t";
cout << "职位" << "\n";
while (r != NULL) {
if (strcmp(r->data.position, sc) == 0) {
cout << r->data.id << "\t";
cout << r->data.name << "\t";
cout << r->data.sex << "\t";
cout << r->data.age << "\t";
cout << r->data.position << "\n";
}
r = r->next;
}
}
void showAll() {
link *q;
q = p->next;
if (q == NULL) {
cout << "无任何员工信息" << "\n";
return;
}
cout << "员工号" << "\t";
cout << "姓名" << "\t";
cout << "性别" << "\t";
cout << "年龄" << "\t";
cout << "职位" << "\n";
while (q != NULL) {
cout << q->data.id << "\t";
cout << q->data.name << "\t";
cout << q->data.sex << "\t";
cout << q->data.age << "\t";
cout << q->data.position << "\n";
q = q->next;
}
}
link *read()
{
FILE *fp;
link *s, *q;
q = new link;
q->next = NULL;
career e;
if ((fp = fopen("career_list.txt", "rb")) == NULL) {
cout << "信息读取失败\n";
return q;
}
while (fread(&e, sizeof(struct career), 1, fp) == 1)

{
s = new link;
s->data = e;
s->next = q->next;
q->next = s;
}

fclose(fp);
return q;
}
void dealWithEvents() {
char a[10];
int s;

for (;;) {
cout << "请选择操作(1-7):\n ";
cin >> s;
switch (s) {
case 1:
enter();
break;
case 2:
cout << "请输入要删除的员工姓名:" << "\n";
cin >> a;
deleteCareer(p, a);
break;
case 3:
cout << "请输入要修改的员工姓名:" << "\n";
cin >> a;
modify(p, a);
break;

case 4:
cout << "请输入要查询的职位:" << "\n";
cin >> a;
search(p, a);
break;
case 5:
showAll();
break;
case 6:
save();
break;
case 7:
return;
case 8:
showOperations();
break;
}
}
}

void save() {
career qq;
link *r;
FILE *fp;
r = p->next;
if ((fp = fopen("career_list.txt", "wb")) == NULL) {
cout << "信息读取失败";
return;
}
while (r != NULL) {
qq = r->data;
fwrite(&qq, sizeof(struct career), 1, fp);
r = r->next;
}
cout << "存档完成" << "\n";
fclose(fp);
}

你可能感兴趣的:(C/C++,c/c++,数据结构与算法)