#include <stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#define INITLISTSIZE 20
#define LISTINCREMENT 20
#define MAX 32767
#define N 500
int n=0;
typedef struct worker //员工信息存储结构
{
char sex;
char name[15];
int salary;
int bonus;
char position[15];
int age;
char num[15];
}workers;
typedef struct SqList //顺序表存储结构
{
workers *elem;
int listsize;
int length;
}SqList;
void Init(SqList &L)
{
L.elem=(worker *)malloc(INITLISTSIZE*sizeof(workers));
L.length=0;
}
int Menu()
{
int choice;
printf("\t职 工 信 息 管 理 系 统\n");
printf("==================员工信息管理==================\n ");
printf(" 1.录入职工信息\n ");
printf(" 2.修改职工信息\n ");
printf(" 3.浏览职工信息\n ");
printf(" 4.查询职工信息\n ");
printf(" 5.删除职工信息\n ");
printf(" 6.添加职工信息\n ");
printf(" 7.保存职工信息\n ");
printf(" 8.读取职工信息\n ");
printf(" 0.退出\n ");
printf("输入你选择的序号:");
scanf("%d",&choice);
return choice;
}
void Create(SqList &L)
{
while(1)
{
printf("请输入工号:");
printf("\n提示:输入0返回主菜单!\n");
scanf("%s",L.elem[n].num);
if (strcmp(L.elem[n].num, "0") == 0)
break;
printf("请输入姓名:");
scanf("%s",L.elem[n].name);
printf("请输入性别:");
scanf("%s",&L.elem[n].sex);
printf("请输入工资:");
scanf("%d",&L.elem[n].salary);
printf("请输入奖金:");
scanf("%d",&L.elem[n].bonus);
printf("请输入职务:");
scanf("%s",L.elem[n].position);
printf("请输入年龄:");
scanf("%d",&L.elem[n].age);
printf("提示:已经完成一条记录的添加!\n");
L.length++;
n++;
}
}
void display(SqList L)
{
int i;
printf("共有%d职工的信息:\n", n);
if (0!=n)
{
printf("工号 \t姓名 \t年龄\t性别\t职务\t工资 \t奖金\n");
printf("--------------------------------------------------------------------\n");
for (i=0;i<n;i++)
{
printf("%s\t%s\t%d \t%c \t%s\t%d \t%d\n",L.elem[i].num,L.elem[i].name, L.elem[i].age,
L.elem[i].sex,L.elem[i].position,L.elem[i].salary,L.elem[i].bonus);
}
}
system("pause");
}
void modify(SqList &L)
{
int i, item, n= -1;
char s1[N],s2[N];
printf("请输入要修改的职工的工号或姓名:\n");
scanf("%s",s1);
for (i=0;i<n;i++)
{
if((strcmp(L.elem[i].num,s1)==0)||(strcmp(L.elem[i].name,s1)==0))
{
n=i;
printf("------------------\n");
printf("1.修改工号(不可重复)\n");
printf("2.修改职务\n");
printf("3.修改工资\n");
printf("4.修改奖金\n");
printf("5.退出本菜单\n");
printf("------------------\n");
while (1)
{
printf("请选择子菜单编号:");
scanf("%d", &item);
switch (item)
{
case 1:
printf("请输入新的工号:\n");
scanf("%s",s2);
strcpy(L.elem[n].num,s2);
break;
case 2:
printf("请输入新的职务:\n");
scanf("%s", &L.elem[n].position);
break;
case 3:
printf("请输入新的工资:\n");
scanf("%d", &L.elem[n].salary);
break;
case 4:
printf("请输入新的奖金:\n");
scanf("%d", &L.elem[n].salary);
break;
case 5: return;
default:printf("请在1-5之间选择\n");
}
}
printf("修改完毕!请及时保存!\n");
}
else{
printf("没有该职工!!!");
}
}
}
void Del(SqList &L)
{
printf("输入要删除的职工工号:");
char number[11];
scanf("%s",number);
int i=0;
while(i<L.length&&strcmp(number,L.elem[i].num))
i++;
while(i<L.length-1)
{
L.elem[i]=L.elem[i+1];
i++;
}
L.length-=1;
}
void Save(SqList &L)
{
FILE *fp;
if((fp=fopen("workers.txt","w"))==NULL)
{
printf("文件打开失败\n");
getchar();
exit(1);
}
workers *p;
for(p=L.elem; p<L.elem+L.length; p++)
{
fprintf(fp,"%s %s %d %c %s %d %d \n",p->num,p->name,p->age,p->sex,p->position,p->salary,p->bonus);
}
fclose(fp);
printf("保存成功\n");
}
int BinarySearch(SqList &L)
{
int low,high,mid,i;
low=0,high=L.length;
workers x,e;
printf("按员工姓名称查找\n");
scanf("%s",e.name);
for(i=0;i<L.length;i++){
if(!strcmp(L.elem[i].name,e.name))
{
x.num[15]=L.elem[i].num[15];
}
}
while(low<=high)
{
mid=(low+high)/2;
if(L.elem[mid].num==x.num)
{
printf("工号:%d \n姓名:%s \n工资:%d \n奖金:%d \n年龄:%d \n职务:%s \n",L.elem[mid].num,L.elem[mid].name, L.elem[mid].salary,L.elem[mid].bonus,L.elem[mid].age,L.elem[mid].position);
return mid+1;
}
else if(L.elem[mid].num<x.num)
low=mid+1;
else if(L.elem[mid].num>x.num)
high=mid-1;
}
return 0;
}
int BinarySearch1(SqList L)
{
int low,high,mid;
low=0,high=L.length;
workers x;
printf("按工号查找\n");
scanf("%d",&x.num);
while(low<=high)
{
mid=(low+high)/2;
if(L.elem[mid].num==x.num)
{
printf("工号:%d \n姓名:%s \n工资:%d \n奖金:%d \n年龄:%d \n职务:%s \n",L.elem[mid].num,L.elem[mid].name, L.elem[mid].salary,L.elem[mid].bonus,L.elem[mid].age,L.elem[mid].position);
return mid+1;
}
else if(L.elem[mid].num<x.num)
low=mid+1;
else if(L.elem[mid].num>x.num)
high=mid-1;
}
return 0;
}
void Load(SqList &L)
{
int i=0;
FILE *fp;
if((fp=fopen("workers.txt","r"))==NULL)
{
printf("文件打开失败\n");
getchar();
exit(1);
}
workers *p;
p=L.elem;
while(fscanf(fp,"%s%s%d%c%s%d%d\n",p->num,p->name,p->age,p->sex,p->position,p->salary,p->bonus))
{
p++;
i++;
}
L.length=i;
fclose(fp);
}
int main()
{
SqList L;
Init(L);
while(1)
{
switch(Menu())
{int num;
case 1:Create(L);break;
case 2:modify(L);break;
case 3:display(L);break;
case 4:printf("==========请输入查询学生信息的类型==========\n");
printf("1.按位置编号查找\n");
printf("2.按学生名称查找\n");
scanf("%d",&num);
if(num==1){
BinarySearch1(L);
}else if(num==2)
BinarySearch(L);
break;
case 5:Del(L);break;
case 7:Save(L);break;
case 8:Load(L);break;
case 0:exit(0);
}
}
return 0;
}