员工管理系统链表+txt版
主要实现下面这几个功能,用字符串操作是为了练习使用字符串比较函数。
在编写过程中遇到的一些问题,选择绕过了,比如scanf多输入且有字符串的时候,报错。
所以还是用了cout。
1、添加员工信息 add
2、显示所有信息 print
3、删除员工信息 delete
4、查找员工信息 find
5、修改员工信息 modify
6、清空员工信息 clean
7、退出系统 exit
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<iostream>
#include<fstream>
using namespace std;
#define NAME_LEN 32
#define ID_LEN 5
#define CMD_LEN 128
ofstream ofs;
ifstream ifs;
int head = 0;
struct employee
{
char em_Name[NAME_LEN];
char em_ID[ID_LEN];
int em_Sex;
int em_Age;
employee* pNextEm;
};
employee* headoftable = NULL;
//保存信息
void save_info()
{
ofs.open("table.txt", ios::out | ios::binary);
if (NULL == headoftable)
{
head = 0;
ofs.write((char*)&head, sizeof(head));
return;
}
head = 1;
ofs.write((char*)&head, sizeof(head));
employee* ptEm = headoftable;
while (true)
{
ofs.write((char*)ptEm, sizeof(employee));
if (ptEm->pNextEm == NULL)
return;
ptEm = ptEm->pNextEm;
}
ofs.close();
}
//初始化,主要是把信息读进来
void init()
{
ifs.open("table.txt", ios::out | ios::binary);
if (!ifs.is_open())
return;
ifs.read((char*)&head, sizeof(head));
if (head == 0)
{
headoftable = NULL;
return;
}
employee* ptEM = (employee*)malloc(sizeof(employee));
ifs.read((char*)ptEM, sizeof(employee));
headoftable = ptEM;
employee* ptEm_last = ptEM;
while (1)
{
if (ptEm_last->pNextEm == NULL)
return;
employee* ptEM = (employee*)malloc(sizeof(employee));
ifs.read((char*)ptEM, sizeof(employee));
ptEm_last->pNextEm = ptEM;
ptEm_last = ptEM;
}
ifs.close();
}
void show_menu()
{
printf("**********************************************\n");
printf("****** 1、添加员工信息 add ******\n");
printf("****** 2、显示所有信息 print ******\n");
printf("****** 3、删除员工信息 delete ******\n");
printf("****** 4、查找员工信息 find ******\n");
printf("****** 5、修改员工信息 modify ******\n");
printf("****** 6、清空员工信息 clean ******\n");
printf("****** 7、退出系统 exit ******\n");
printf("**********************************************\n");
}
//根据字符操作返回值
int cmd()
{
char cmd[CMD_LEN] = { 0 };
scanf_s("%s", cmd,CMD_LEN);
if (0 == strncmp(cmd, "add", 3))
return 1;
else if (0 == strncmp(cmd, "print", 5))
return 2;
else if (0 == strncmp(cmd, "del", 3))
return 3;
else if (0 == strncmp(cmd, "find", 4))
return 4;
else if (0 == strncmp(cmd, "modify", 6))
return 5;
else if (0 == strncmp(cmd, "clean", 5))
return 6;
else if (0 == strncmp(cmd, "exit", 4))
return 7;
else //printf("%s", cmd);
return 0;
}
//找到尾部
employee* search_tail(employee* head)
{
if (NULL == head)
return head;
else
{
employee* ptEm = head;
while (1)
{
if (ptEm->pNextEm == NULL)
return ptEm;
else
ptEm = ptEm->pNextEm;
}
}
}
//检查是否充复
int check_rep(char str[])
{
if (headoftable == NULL)
return 0;
employee* ptEm = headoftable;
while (true)
{
if (0 == strcmp(ptEm->em_ID, str))
return 1;
if (ptEm->pNextEm == NULL)
return 0;
ptEm = ptEm->pNextEm;
}
}
//检查工号输入是否正确
int check_ID(char str[])
{
if (strlen(str) != 5)
{
printf("错误!\n");
return 0;
}
for (int i = 0; str[i] == '0'; i++)
{
if ('0' > str[i] || '9' < str[i])
{
printf("错误!!\n");
return 0;
}
}
if (check_rep(str))
{
printf("错误!员工ID重复!!\n");
return 0;
}
return 1;
}
//添加一个员工
void add_em(employee* head)
{
employee* ptEm = search_tail(head);
employee* current = (employee*)malloc(sizeof(employee));
//char Name[NAME_LEN] = {0};
//char ID[ID_LEN] = { 0 };
//int Sex = 0;
//int Age =0;
//printf("请输入员工信息,空格隔开\n");
//scanf_s("%s %s %d %d", Name, ID, Sex, Age);
//printf("%s %s %d %d", Name, ID, Sex, Age);
cout << "请输入员工的姓名" << endl;
cin >> current->em_Name;
while (1)
{
cout << "请输入员工ID号(5位数)" << endl;
cin >> current->em_ID;
if (check_ID(current->em_ID))
break;
}
while (1)
{
cout << "请输入员工性别(1男 2女)" << endl;
cin >> current->em_Sex;
if (current->em_Sex == 1 || current->em_Sex == 2)
{
break;
}
printf("错误,请重新输入!\n");
}
cout << "请输入员工的年龄" << endl;
cin >> current->em_Age;
current->pNextEm = NULL;
if (head == NULL)
headoftable = current;
//head = current; //地址传不出来
else
ptEm->pNextEm = current;
}
void print_all(employee* head)
{
if (NULL == head)
printf("列表为空\n");
else
{
employee* ptEm = head;
while (1)
{
cout << "姓名:" << ptEm->em_Name << "\t";
cout << "员工ID:" << ptEm->em_ID << "\t";
cout << "年龄:" << ptEm->em_Age<< "\t";
cout << "性别:" << (ptEm->em_Sex == 1 ? "男" : "女") << endl;
if (ptEm->pNextEm == NULL)
break;
ptEm = ptEm->pNextEm;
}
}
}
//查找员工
int find_em()
{
char str[32] = { 0 };
printf("请输入您要查找的工号或姓名\n");
cin >> str;
if (NULL==headoftable)
printf("当前列表为空\n");
else
{
employee* ptEm = headoftable;
while (true)
{
if (0 == strcmp(ptEm->em_Name, str) || 0 == strcmp(ptEm->em_ID, str))
{
cout << "姓名:" << ptEm->em_Name << "\t";
cout << "员工ID:" << ptEm->em_ID << "\t";
cout << "年龄:" << ptEm->em_Age << "\t";
cout << "性别:" << (ptEm->em_Sex == 1 ? "男" : "女") << endl;
return 0;
}
if (ptEm->pNextEm == NULL)
{
printf("查无此人!\n");
return 0;
}
ptEm = ptEm->pNextEm;
}
}
}
//删除或者修改摸个员工
int delormod_em(int a) //删除1,修改0
{
char str[32] = { 0 };
printf("请输入工号或姓名\n");
cin >> str;
if (NULL == headoftable)
printf("当前列表为空\n");
else
{
employee* ptEm = headoftable;
employee* ptEm_last = ptEm;
while (true)
{
if (0 == strcmp(ptEm->em_Name, str) || 0 == strcmp(ptEm->em_ID, str))
{
if (a) //删除
{
if (ptEm == headoftable)
{
headoftable = ptEm->pNextEm;
free(ptEm);
printf("删除成功!\n");
return 0;
}
ptEm_last->pNextEm = ptEm->pNextEm;
free(ptEm);
printf("删除成功!\n");
return 0;
}
else //修改
{
cout << "请输入员工的姓名" << endl;
cin >> ptEm->em_Name;
cout << "请输入员工ID号(5位数)" << endl;
cin >> ptEm->em_ID;
cout << "请输入员工性别(1男 2女)" << endl;
cin >> ptEm->em_Sex;
cout << "请输入员工的年龄" << endl;
cin >> ptEm->em_Age;
printf("修改成功!\n");
return 0;
}
}
if (ptEm->pNextEm == NULL)
{
printf("查无此人!\n");
return 0;
}
ptEm_last = ptEm;
ptEm = ptEm->pNextEm;
}
}
}
//清楚信息
int clean()
{
if (NULL == headoftable)
{
printf("已清空\n");
return 0;
}
employee* ptEm = headoftable;
employee* ptEm_next = ptEm;
while (ptEm->pNextEm != NULL)
{
ptEm_next = ptEm->pNextEm;
free(ptEm);
ptEm = ptEm_next;
}
free(ptEm);
headoftable = NULL;
printf("已清空\n");
return 0;
}
int main()
{
init();
while (1)
{
show_menu();
int _cmd = cmd();
switch (_cmd)
{
case 1:
add_em(headoftable);
break;
case 2:
print_all(headoftable);
break;
case 3:
delormod_em(1);
break;
case 4:
find_em();
break;
case 5:
delormod_em(0);
break;
case 6:
clean();
break;
case 7:
save_info();
printf("谢谢使用\n");
system("pause");
return 0;
}
system("pause");
system("cls");
}
system("pause");
return 0;
}