#include <stdio.h>
#include <stdlib.h>
#include <wtypes.h>
#include <winbase.h>
#include <time.h>
#include <conio.h>
struct
{
	int choice;
	char name[50];
	char num[50];
	char position[50];
	char wanges[50];
	char department[50];
	char gread[50];
	char sex[20];
	char birth[50];
	char add[500];
	char pnum[50];
}
one;
FILE*fp;

void output();
void search();
void append();
void modify();
void delete();
void exit();
void nameway();
void numway();


void main()
{
	int m;
	while(1)
	{
		system("color 0a");
		printf("\n\n\n                   ~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*\n\n");
		printf("                             欢迎使用职工信息管理系统\n\n");
		printf("                   ~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*\n\n");
		printf("                  |     *     |  1.添加职工信息  |     *     |     \n");
		printf("                  |     *     |  2.查找职工信息  |     *     |     \n");
		printf("                  |     *     |  3.修改职工信息  |     *     |     \n");
		printf("                  |     *     |  4.删除职工信息  |     *     |     \n");
		printf("                  |     *     |  5.输出职工信息  |     *     |     \n");
		printf("                  |     *     |  6.退出信息系统  |     *     |   \n\n");
		printf("                   ~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*\n\n");
		printf("                               请输入对应的字符[ ]\b\b");
		scanf("%d",&m);
		if(m>=1&&m<=6)
		{
			switch(m)
			{
				case 1: append();
				break;
				case 2: search();
				break;
				case 3: modify();
				break;
				case 4: delete();
				break;
				case 5: output();
				break;
				case 6: exit(0);
			}
		}
		else
		{
			printf("\n\n无效指令,请重试");
		}
	}
}

void append()
{
	int choice;
	system("cls");
	if((fp=fopen("worker.xls","a"))==NULL)
	{
		printf("\n对不起,无法查询信息文件。\n\n");
		exit(0);
	}
	printf("                    *********************************\n");
	printf("                         请输入需要添加的职工信息      \n");
	printf("                    *********************************\n");
	printf("         姓名,职工号,性别,职位,工资,出生年月,学历,住址,电话\n");
	scanf("%s%s%s%s%s%s%s%s%s",one.name,one.num,one.sex,one.position,one.wanges,one.birth,one.gread,one.add,one.pnum);
	fprintf(fp,"%-20s%-20s%-20s%-20s%-20s%-20s%-20s%-20s%-20s\n",one.name,one.num,one.sex,one.position,one.wanges,one.birth,one.gread,one.add,one.pnum);
	fclose(fp);
	printf("\n\n操作完成,请按下回车继续");
	choice=getch();
	system("cls");
}

void output()
{	
	int choice;
	system("cls");
	if((fp=fopen("worker.xls","r"))==NULL)
	{
		system("cls");
		printf("\n对不起,无法查询信息文件。\n\n");
		exit(0);
	}
	printf("                          =========================\n");
	printf("                                  职工信息\n");
	printf("                          =========================\n");
	printf("          姓名,职工号,性别,职位,工资,出生年月,学历,住址,电话\n\n");
	while(!feof(fp))
	{
		fscanf(fp,"%s%s%s%s%s%s%s%s%s\n",one.name,one.num,one.sex,one.position,one.wanges,one.birth,one.gread,one.add,one.pnum);
		printf("%-20s%-20s%-20s%-20s%-20s%-20s%-20s%-20s%-20s\n",one.name,one.num,one.sex,one.position,one.wanges,one.birth,one.gread,one.add,one.pnum);
	}
	fclose(fp);
	printf("\n\n显示完毕,请按下回车继续");
	choice=getch();
	system("cls");
}

void search()
{	
	int l;
	system("cls");
	printf(" 按姓名查找请按: 1\n");
	printf(" 按工号查找请按: 2\n");
	scanf("%d",&l);
	if(l>0&&l<3)
	{
		switch(l)
		{
			case 1:nameway();
			break;
			case 2:numway();
			break;
		}
	}
	else 
	{
		printf("\n\n无效指令,请重试");
	}
}

void nameway()
{
	int choice;
	int k=0;
	char nam[50];
	printf("\n请输入您要查询的职工姓名:");
	scanf("%s",nam);
	if((fp=fopen("worker.xls","rb"))==NULL)
	{
		printf("\n对不起,无法查询信息文件。");
		exit(0);
	}
	while(!feof(fp))
	{
		fscanf(fp,"%s%s%s%s%s%s%s%s%s\n",one.name,one.num,one.sex,one.position,one.wanges,one.birth,one.gread,one.add,one.pnum);
		if(strcmp(nam,one.name)==0)
		{
			printf("\n\n已查到,记录为:");
			printf("%-20s%-20s%-20s%-20s%-20s%-20s%-20s%-20s%-20s\n",one.name,one.num,one.sex,one.position,one.wanges,one.birth,one.gread,one.add,one.pnum);
			k=1;
		}
	}
	if(k==0)
	{
		printf("\n\n对不起,查无此人。\n");
		fclose(fp);
	}
	printf("\n\n操作完成,请按下回车继续");
	choice=getch();
	system("cls");
}

void numway()
{
	int i=0;
	int choice;
	char numb[50];
	printf("   输入你要查询的工号\n");
	scanf("%s",numb);
	if((fp=fopen("worker.xls","rb"))==NULL)
	{
		printf("\n对不起,无法查询信息文件。");
		exit(0);
	}
	while(!feof(fp))
	{
		fscanf(fp,"%s%s%s%s%s%s%s%s%s\n",one.name,one.num,one.sex,one.position,one.wanges,one.birth,one.gread,one.add,one.pnum);
		if(!strcmp(numb,one.num))
		{
			printf("\n\n已查到,记录为:");
			printf("%-20s%-20s%-20s%-20s%-20s%-20s%-20s%-20s%-20s\n",one.name,one.num,one.sex,one.position,one.wanges,one.birth,one.gread,one.add,one.pnum);
			i=1;
		}
	}
	if(!i)
	{
		printf("\n\n对不起,查无此人。");
		fclose(fp);
	}
	printf("\n\n操作完成,请按下回车继续");
	choice=getch();
	system("cls");
}

void modify()
{
	int choice;
	int k=0;	
	long a;
	char namekey[50];
	system("cls");
	printf("\n请输入您要修改的职工姓名:");
	scanf("%s",namekey);
	if((fp=fopen("worker.xls","r+"))==NULL)
	{
		printf("\n对不起,无法查询信息文件。");
		exit(0);
	}
	while(!feof(fp))
	{
		a=ftell(fp);
		fscanf(fp,"%s%s%s%s%s%s%s%s%s\n",one.name,one.num,one.sex,one.position,one.wanges,one.birth,one.gread,one.add,one.pnum);
		if(!strcmp(namekey,one.name))
		{
			k=1;
			break;
		}
	}
	if(k)
	{
		printf("\n已查到,记录为:");
		printf("\n%-20s%-20s%-20s%-20s%-20s%-20s%-20s%-20s%-20s\n",one.name,one.num,one.sex,one.position,one.wanges,one.birth,one.gread,one.add,one.pnum);
		printf("\n请输入需要修改的信息。\n");
		printf("(请按照:姓名,职工号,性别,职位,工资,出生年月,学历,住址,电话顺序输入)\n");
		scanf("%s%s%s%s%s%s%s%s%s",one.name,one.num,one.sex,one.position,one.wanges,one.birth,one.gread,one.add,one.pnum);
		fseek(fp,a,0);
		fprintf(fp,"%-20s%-20s%-20s%-20s%-20s%-20s%-20s%-20s%-20s\n",one.name,one.num,one.sex,one.position,one.wanges,one.birth,one.gread,one.add,one.pnum);
	}
	else
	{
		printf("\n对不起,查无此人,无法修改。");
		fclose(fp);
	}
	printf("\n\n操作完成,请按下回车继续");
	choice=getch();
	system("cls");
}

void delete()
{
	int m;
	int choice;
	int k=0;	
	long a;
	char namekey[50];
	char valid[50];
	system("cls");
	printf("\n请输入您要修改的职工姓名:");
	scanf("%s",namekey);
	if((fp=fopen("worker.xls","r+"))==NULL)
	{
		printf("\n对不起,无法查询信息文件。");
		exit(0);
	}
	while(!feof(fp))
	{
		a=ftell(fp);
		fscanf(fp,"%s%s%s%s%s\n",one.name,one.num,one.sex,one.position,one.wanges,one.birth,one.gread,one.add,one.pnum);
		if(strcmp(namekey,one.name)==0)
		{
			k=1;
			break;
		}
	}
	if(k==1)
	{
		printf("\n已查到,记录为:");
		printf("\n%-20s%-20s%-20s%-20s%-20s%-20s%-20s%-20s%-20s\n",one.name,one.num,one.sex,one.position,one.wanges,one.birth,one.gread,one.add,one.pnum);
		printf("\n确定删除按1,不删除按0:");
		scanf("%d",&m);
		if(m==1)
		{
			fseek(fp,a,0);
			fprintf(fp,"%-20s%-20s%-20s%-20s%-20s%-20s%-20s%-20s%-20s\n","","","","","");
		}
	}
	else
	{
		printf("\n对不起,查无此人。");
	}
	fclose(fp);
	printf("\n\n操作完成,请按下回车继续");
	choice=getch();
	system("cls");
}