数据结构
简单的员工管理系统
问题描述
每个员工的信息包括:编号,姓名,性别,出生年月,学历,职务,电话,住址。
系统功能包括:
(1) 查询:按特定条件查找员工。
(2) 修改:按编号对某个员工的某项信息进行修改。
(3) 插入:加入新员工的信息。
(4) 删除:按编号删除已离职的员工的信息。
(5) 排序:按特定条件对所有员工的信息进行排序。
算法分析与设计
选用哈希表作为数据的存取结构,哈希表构造采用除余数法,冲突处理则采用线性探测再散列法,排序采用快速排序法。
基本员工结构描述如下:
typedef struct employ{ /*员工结构描述*/
int id; /*编号*/
char name[8]; /*姓名*/
char sex[2]; /*性别(fm,m)*/
int birth_y; /*出生年(19XX)*/
int birth_m; /*出生月*/
char level[6]; /*学历*/
char duty[8]; /*职务*/
char tel[11]; /*电话*/
char add[20]; /*住址*/
}employ;
员工的编号既id作为哈希法查找的关键字
int H(int key) /*哈希函数*/
{
return key%Max;
}
查询函数:
int find_emp(employ emp[],int flag); /*查询*/
employ emp[] /*待查询哈希表*/
int flag /*查询类型*/
flag=0 无条件查询
flag=1 按编号查询
flag=2 按姓名查询
flag=3 按性别查询
flag=4 按生日查询
flag=5 按学历查询
flag=6 按职务查询
flag=7 按电话查询
使用哈希法定位员工的存储位置
修改函数:
int edit_emp(employ emp[],int edit_id); /*修改*/
employ emp[] /*待查询哈希表*/
int edit_id /*待修改员工的编号*/
使用哈希法定位待编辑员工的存储位置
插入函数:
void ins_emp(employ emp[]); /*插入*/
employ emp[] /*待查询哈希表*/
首先录入员工基本资料,然后把员工编号带入哈希函数计算出存储位置,如果冲突,采用线性探测再散列法计算新的存储位置,直到没有冲突为止。
删除函数:
int del_emp(employ emp[],int del_id); /*删除*/
employ emp[] /*待操作哈希表*/
int del_id /*待删除员工的编号*/
使用哈希法定位待删除员工的存储位置
排序函数:
int sort_emp(employ emp[],int flag); /*排序*/
employ emp[] /*待排序哈希表*/
int flag /*排序类型*/
在此排序类型flag为0既为按员工编号排序
排序采用快速排序法,具体实现函数说明如下:
int QKPass(employ emp[],int left,int right) /*一趟快速排序算法*/
对记录数组emp中的emp[left]至emp[right]部分进行一趟排序,并得到基准的位置,使得排序后的结果满足其之后(前)的记录的关键字均不小于(大于)基准记录
void QKSort(employ emp[],int left,int right) /*快速排序*/
对记录数组emp[left…right]用快速排序法进行排序
其它具体描述参见程序源代码内的注释
程
序
源
代
码
程序源文件分为两部分:
fansy.h
头文件存放所有函数的实现代码
fansy.c
程序的实现代码,须包含前头文件
/*******************************************************/
/* F.S.Studio 1999--2004 */
/* Fansy */
/* http://fansy.nease.net */
/* */
/* 员工管理系统 */
/* DEMO */
/* fansy.h */
/**************************************2004.06.06*******/
#include "string.h"
#define Max 100
#define NULL 0
typedef struct employ{ /*员工结构描述*/
int id; /*编号*/
char name[8]; /*姓名*/
char sex[2]; /*性别(fm,m)*/
int birth_y; /*出生年(19XX)*/
int birth_m; /*出生月*/
char level[6]; /*学历*/
char duty[8]; /*职务*/
char tel[11]; /*电话*/
char add[20]; /*住址*/
}employ;
int H(int key) /*哈希函数*/
{
return key%Max;
}
/*************************************************************************/
void print_emp(employ p) /*职工资料输出*/
{
printf("/nThe id is:%d",p.id);
printf("/nThe name is:%s",p.name);
printf("/nThe sex is:%s",p.sex);
printf("/nThe birth year is:%d",p.birth_y);
printf("/nThe birth month is:%d",p.birth_m);
printf("/nThe educational level is:%s",p.level);
printf("/nThe duty is:%s",p.duty);
printf("/nThe telephone number is:%s",p.tel);
printf("/nThe address is:%s",p.add);
}
/*************************************************************************/
int find_emp(employ emp[],int flag) /*查询*/
{
int i;
i=0;
switch(flag)
{
case 0: { /*无条件查询*/