#include"stdio.h"
#include"stdlib.h"
#include"string.h"
#include"conio.h"
#define PAGE 3
#define MAX 1000
#define N 5
int k=0;
/*结构体类型*/
typedef struct
{ int num;/*卡号*/
char name[20];/*姓名*/
char sex[5];/*性别*/
int age;/*年龄*/
char workerdentclass[20];/*组织*/
float money;/*工资*/
}PERSON;
int read_file(PERSON worker[])
{
FILE *fp;
int i=0;
if((fp=fopen("worker.txt","rt"))==NULL)
{printf("\n\n*****库存文件不存在!请创");
return 0;
}
while(feof(fp)!=1)
{
fread(&worker[i],sizeof(PERSON),1,fp);
if(worker[i].num==0)
break;
else
i++;
}
fclose(fp);
return i;
}
void save_file(PERSON worker[],int sum)
{FILE*fp;
int i;
if((fp=fopen("worker.txt","wb"))==NULL)
{printf("写文件错误!\n");
return;
}
for(i=0;i<sum;i++)
if(fwrite(&worker[i],sizeof(PERSON),1,fp)!=1)
printf("写文件错误!\n");
fclose(fp);
}
/*创建员工信息*/
int input(PERSON worker[])
{ int i,x;
for(i=0;i<1000;i++)
{
system("cls");
printf("\n\n 录入员工信息 (最多%d个)\n",MAX);
printf(" ----------------------------\n");
printf("\n 第%d个员工",k+1);
printf("\n 请输入员工的卡号:");
scanf("%d",&worker[k].num);
printf("\n 请输入员工的姓名:");
scanf("%s",worker[k].name);
printf("\n 请输入员工的性别:");
scanf("%s",worker[k].sex);
printf("\n 请输入员工的年龄:");
scanf("%d",&worker[k].age);
printf("\n 请输入员工的组织:");
scanf("%s",worker[k].workerdentclass);
printf("\n 请输入员工的工资:");
scanf("%f",&worker[k++].money);
printf("\n 请按1键返回菜单或按0键继续创建");
scanf("%d",&x);
if(x)
break;
}
return k;
}
/*删除员工信息*/
void deletel(PERSON worker[])
{ system("cls");
char Workername2[20];
int i,j;
printf("请输入员工姓名:");
scanf("%s",Workername2);
printf("\n");
for(i=0;i<k;i++)
if(strcmp(worker[i].name,Workername2)==0)
for(j=0;j<20;j++)
worker[i].name[j]=worker[i+1].name[j];
k--;
printf("删除成功\n");
printf("按任意键加回车返回主菜单!");
scanf("%d",&i);
getchar();
}
/*打印员工信息*/
void output(PERSON worker[])
{ system("cls");
int i;
for(i=0;i<k;i++)
printf("卡号:%d,姓名:%s,性别:%s,年龄:%d,组织:%s,工资: %f\n",worker[i].num,worker[i].name,
worker[i].sex,worker[i].age,worker[i].workerdentclass,worker[i].money);
printf("按任意键加回车返回主菜单!");
scanf("%d",&i);
getchar();
}
/*查询员工信息*/
void inquire(PERSON worker[])
{ int i;
int num;
system("cls");
printf(" \n\n请输入您要查找的员工的卡号");
scanf("%d",&num);
for(i=0;i<k;i++)
if(num==worker[i].num)
printf("\n\n\n卡号:%d,姓名:%s,性别:%s,年龄:%d,组织:%s,工资: %.2f,总工资:%.2f\n",worker[i].num,worker[i].name,
worker[i].sex,worker[i].age,worker[i].workerdentclass,worker[i].money,worker[i].money*12);
printf("按任意键加回车返回主菜单!");
scanf("%d",&i);
getchar();
}
/*修改员工信息*/
void change(PERSON worker[])
{ int num,i,choice;
system("cls");
printf("\n\n\n 请输入您要修改的员工的卡号");
scanf("%d",&num);
for(i=0;i<k;i++)
{ if(num==worker[i].num)
printf("\n卡号:%d,姓名:%s,性别:%s,年龄:%d,组织:%s,工资: %.2f\n",worker[i].num,worker[i].name,
worker[i].sex,worker[i].age,worker[i].workerdentclass,worker[i].money);
printf("\n\n\n ********请输入您想要修改的数据********\n\n");
printf(" 1. 卡号\n\n");
printf(" 2. 姓名\n\n");
printf(" 3. 性别\n\n");
printf(" 4. 年龄\n\n");
printf(" 5. 组织\n\n");
printf(" 6. 工资\n\n");
printf(" 请选择(1-6):");
scanf("%d",&choice);
switch(choice)
{case 1:{
printf("\n 请输入你改的新卡号");
scanf("%d",&worker[i].num);
break;
}
case 2:{
printf("\n 请输入你改的新姓名");
scanf("%s",worker[i].name);
break;
}
case 3:{
printf("\n 请输入你改的新性别");
scanf("%s",worker[i].sex);
break;
}
case 4:{
printf("\n 请输入你改的新年龄");
scanf("%d",&worker[i].age);
break;
}
case 5:{
printf("\n 请输入你改的新组织");
scanf("%s",worker[i].workerdentclass);
break;
}
case 6:{
printf("\n 请输入你改的新工资");
scanf("%f",&worker[i].money);
break;
}
}
printf("卡号:%d,姓名:%s,性别:%s,年龄:%d,组织:%s,工资: %f\n",worker[i].num,worker[i].name,
worker[i].sex,worker[i].age,worker[i].workerdentclass,worker[i].money);
printf("按任意键加回车返回主菜单!");
scanf("%d",&i);
break;
}
}
/*员工工资信息排名*/
void sort(PERSON worker[])
{ int i,j,n=1,x;
system("cls");
int t;
for(i=0;i<k-1;i++)
for(j=i+1;j<k;j++)
if(worker[i].money<worker[j].money)
{ t=worker[i].money;
worker[i].money=worker[j].money;
worker[j].money=t;
t=worker[i].num;
worker[i].num=worker[j].num;
worker[j].num=t;
}
for(i=0;i<k;i++)
printf("排名 卡号 工资 总工资\n %d %d %.2f %.2f\n",n++,worker[i].num,worker[i].money,worker[i].money*12);
printf("按任意键加回车返回主菜单!");
scanf("%d",&x);
getchar();
}
void pagedis()
{
printf(" \n\n\n **********************************\n");
printf(" * *\n");
printf(" * *\n");
printf(" * *\n");
printf(" * 欢迎进入员工信息管理系统 *\n");
printf(" * *\n");
printf(" * *\n");
printf(" * *\n");
printf(" **********************************\n");
}
void check()
{
char userName[5];/*用户名*/
char userPWD[5];/*密码*/
int i,sum;
system("color E8");
for(i = 1; i < 4; i++)
{
/*用户名yuangongguanli密码mimaqueren;*/
printf(" 用户名yuangongguanli密码mimaqueren\n\n");
printf("\n 请输入您的用户名:");
gets(userName);
printf("\n 请输入您的密码:");
gets(userPWD);
if ((strcmp(userName,"yuangongguanli")==0) && (strcmp(userPWD,"mimaqueren")==0))/*验证用户名和密码*/
{
printf("用户名和密码正确,显示主菜单");
return;
}
else
{
if (i < 3)
{
printf("用户名或密码错误,提示用户重新输入");
printf("用户名或密码错误,请重新输入!");
}
else
{
printf("连续3次输错用户名或密码,退出系统。");
printf("您已连续3次将用户名或密码输错,系统将退出!");
exit(1);
}
}
}
}
void menu()
{
PERSON worker[20];
int choice,k,sum;
sum=read_file(worker);
if(sum==0)
{ printf("首先录入基本库存信息!按回车后进入*****\n");
getch();
sum=input(worker);
}
do
{ system("cls");
printf("\n\n\n ********员工信息管理系统********\n\n");
printf(" 1. 创建员工信息\n\n");
printf(" 2. 打印员工信息\n\n");
printf(" 3. 查询员工信息\n\n");
printf(" 4. 修改员工信息\n\n");
printf(" 5. 删除员工信息\n\n");
printf(" 6. 员工工资信息排名\n\n");
printf(" 0. 退出系统\n\n");
printf(" 请选择(0-6):");
scanf("%d",&choice);
switch(choice)
{
case 1: input(worker); break;/*创建员工信息*/
case 2: output( worker) ; break;/*打印员工信息*/
case 3: inquire(worker); break;/*查询员工信息*/
case 4: change(worker); break;/*修改员工信息*/
case 5: deletel(worker); break;/*删除员工信息*/
case 6: sort(worker); break;/*员工工资信息排名*/
case 0: break;
}
}while(choice!=0);
save_file(worker,sum);
}
int main()
{
int i,sum;
pagedis();
check();
menu();
}