其他分享
首页 > 其他分享> > 职工信息管理系统代码

职工信息管理系统代码

作者:互联网

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//记录职工数量的变量
int size;
//职工结构体
struct Employ
{
	int num;
	char name[10];
	char sex[10];
	int age;
	char educ[10];
	int salary;
	char adress[20];
	int phone;
};
//结点
struct NodeEmploy
{
	struct Employ data;
	struct NodeEmploy* next;
};

//菜单函数
void Menu()
{
	printf("\n********************************\n");
	printf("*******欢迎使用职工信息管理系*****\n");
	printf("*********请选择功能**************\n");
	printf("*********1.录入职工信息**********\n");
	printf("*********2.浏览职工信息**********\n");
	printf("*********3.查询职工信息**********\n");
	printf("*********4.删除职工信息**********\n");
	printf("*********5.修改职工信息**********\n");
	printf("*********6.保存职工信息**********\n");
	printf("*********0.退出系统**************\n");
	printf("********************************\n");
	

}
//保存职工信息函数
void Save(struct NodeEmploy*headptr)
{

	//打开文件:Employ.dat
	FILE* fptr = fopen("Employ.dat", "wb");
	//FILE*指针为空则返回0
	//写入文件
	if (fptr == NULL)//若文件指针为空
	{
		printf("保存失败!文件打开失败!\n");
		return;
	}
	if (headptr->next == NULL)//若表为空
	{
		printf("保存失败!职工信息表为空!\n");
		return;
	}
	struct NodeEmploy* ptr = headptr;
	while (ptr->next != NULL)
	{
		fwrite(ptr->next,sizeof(struct NodeEmploy),1,fptr);
		ptr = ptr->next;
	}
	fclose(fptr);//关闭文件
	printf("保存成功!\n");
    fptr = NULL;//指针置为空
	
}
//职工录入函数
void InputEmploy(struct NodeEmploy* headptr)
{	
	int choice=0;
	struct NodeEmploy* p1=headptr;
	while (p1->next != NULL)//使p1指向最后一个结点
	{
		p1 = p1->next;
	}
	do
	{
		struct NodeEmploy* ptr = (struct NodeEmploy*)malloc(sizeof(struct NodeEmploy));//创建一个结点
		if (ptr == NULL)
		{
			free(ptr);
			printf("mallocError");
		}
		else
		{
			p1->next = ptr;
			size++;//size数量加1
			ptr->next = NULL;
			p1 = p1->next;
			//用户输入要录入职工的信息
			printf("请输入职工号:");
			scanf("%d", &ptr->data.num);
			printf("请输入职工姓名:");
			scanf("%s", ptr->data.name);
			printf("请输入职工性别(男/女):");
			scanf("%s", ptr->data.sex);
			printf("请输入职工年龄:");
			scanf("%d", &ptr->data.age);
			printf("请输入职工学历:");
			scanf("%s", ptr->data.educ);
			printf("请输入职工工资:");
			scanf("%d", &ptr->data.salary);
			printf("请输入职工住址:");
			scanf("%s", ptr->data.adress);
			printf("请输入职工电话:");
			scanf("%d", &ptr->data.phone);
			printf("添加成功!\n");
			//添加成功,若要继续添加请按1,推出功能按0
			printf("继续添加请按1,退出功能请按0:");
			scanf("%d", &choice);
		}
	}while (choice);
	printf("目前有%d个职工的信息。\n", size);
}
 //浏览职工函数
void Scan(struct NodeEmploy* headptr)
{
	//如果headptr->next==NULL则表示无职工信息
	if (headptr->next == NULL)
	{
		printf("职工信息表为空!\n");
		return;
	}
	//由头结点开始遍历整个链表
	//循环结束条件是结的next=NULL
	struct NodeEmploy* ptr = headptr;
	while (ptr->next != NULL)
	{
		printf("工号:\t%4d姓名:\t%-5s性别:\t%-1s年龄:\t%3d学历:\t%-4s工资:\t%6d住址:\t%-10s电话:\t%-10d\n",
			ptr->next->data.num,
			ptr->next->data.name,
			ptr->next->data.sex,
			ptr->next->data.age,
			ptr->next->data.educ,
			ptr->next->data.salary,
			ptr->next->data.adress,
			ptr->next->data.phone);
		ptr = ptr->next;
	}
}
//查询菜单函数
void SearchMenu()
{
	printf("\n********************************\n");
    printf("******请选择查询方式*************\n");
	printf("******1.按职工号查询 **************\n");
	printf("******2.按学历查询 **************\n");
	printf("******0.退出查询*****************\n");
	printf("********************************\n");
 }
//按学历查询函数
void Search_educ(struct NodeEmploy* headptr)
{
	printf("请输入你要查询的学历:");
	char ch[20];
	scanf("%s", ch);
	//如果headptr->next==NULL则表示无职工信息
	if (headptr->next == NULL)
	{
		printf("职工信息表为空!查找失败!\n");
		return;
	}
	//由头结点开始遍历整个链表
	//循环结束条件是结的next=NULL
	struct NodeEmploy* ptr = headptr;
	int count = 0;
	int flag = 0;//标记,用来标记是否查询到
	while (ptr->next != NULL)
	{
		if (!strcmp(ptr->next->data.educ, ch))
		{
			printf("工号:\t%4d姓名:\t%-5s性别:\t%-1s年龄:\t%3d学历:\t%-4s工资:\t%6d住址:\t%-10s电话:\t%-10d\n",
				ptr->next->data.num,
				ptr->next->data.name,
				ptr->next->data.sex,
				ptr->next->data.age,
				ptr->next->data.educ,
				ptr->next->data.salary,
				ptr->next->data.adress,
				ptr->next->data.phone);
			count++;
			flag = 1;
		}
		ptr = ptr->next;
	}
	if (flag)
		printf("查询成功!共找到%d个与\"%s\"相关的职工。\n", count, ch);
	else
		printf("查询失败!查不到学历为\"%s\"的职工信息\n", ch);
}
//按职工号查询函数
void Search_num(struct NodeEmploy* headptr)
{
	printf("请输入要查询的职工号:");
	int num = 0;
	int flag = 0;//标记用来表示是否查询成功
	scanf("%d", &num);
	//如果headptr->next==NULL则表示无职工信息
	if (headptr->next == NULL)
	{
		printf("查询失败!职工信息表为空!\n");
		return;
	}
	//由头结点开始遍历整个链表
	//循环结束条件是结的next=NULL
	struct NodeEmploy* ptr = headptr;
	while (ptr->next != NULL)
	{
		if (ptr->next->data.num == num)
		{
			printf("工号:\t%4d姓名:\t%-5s性别:\t%-1s年龄:\t%3d学历:\t%-4s工资:\t%6d住址:\t%-10s电话:\t%-10d\n",
				ptr->next->data.num,
				ptr->next->data.name,
				ptr->next->data.sex,
				ptr->next->data.age,
				ptr->next->data.educ,
				ptr->next->data.salary,
				ptr->next->data.adress,
				ptr->next->data.phone);
			flag = 1;
			break;
		}
			ptr = ptr->next;
	}
	if (flag)
		printf("查询成功!\n");
	else
		printf("查询失败!查无此人!");
}
 //查询主函数
void Search(struct NodeEmploy*headptr)
{
	int choice;
	do
	{
	    SearchMenu();
	    printf("请选择查询方式:");
	    scanf("%d", &choice);
	    switch (choice)
	    {
	    case 1://按职工号查询
			Search_num(headptr);
		    break;
	    case 2://按学历查询
			Search_educ(headptr);
		    break;
	    case 0://退出查询
	  	  break;
		default:
			printf("选择有误请重新选择!\n");
	    }
	
	} while (choice != 0);
}
//删除职工信息
void Delete(struct NodeEmploy* headptr)
{
	printf("请输入要删除的职工号:");
	int num = 0;
	int flag = 0;//标记用来表示是否查询成功
	scanf("%d", &num);
	//如果headptr->next==NULL则表示无职工信息
	if (headptr->next == NULL)
	{
		printf("删除失败!职工信息表为空!\n");
		return;
	}
	//由头结点开始遍历整个链表
	//循环结束条件是结的next=NULL
	struct NodeEmploy* ptr = headptr;
	while (ptr->next != NULL)
	{
		if (ptr->next->data.num == num)
		{
			struct NodeEmploy* p = ptr->next;
			ptr->next = ptr->next->next;
			free(p);
			p = NULL;
			size--;
			flag = 1;
			break;
		}
		ptr = ptr->next;
	}
	if (flag)
	{
		printf("删除成功!\n");
		printf("目前有%d个职工的信息。\n",size);
	}
	else
		printf("查无此人!删除失败!\n");
 }
//修改职工信息
 void Correct(struct NodeEmploy* headptr)
{
	 printf("请输入要修改的职工号:");
	 int num = 0;
	 int choice = 0;
	 int flag = 0;//标记用来表示是否查询成功
	 scanf("%d", &num);
	 //如果headptr->next==NULL则表示无职工信息
	 if (headptr->next == NULL)
	 {
		 printf("修改失败!职工信息表为空!\n");
		 return;
	 }
	 //由头结点开始遍历整个链表
	 //循环结束条件是结的next=NULL
	 struct NodeEmploy* ptr = headptr;
	 while (ptr->next != NULL)
	 {
		 if (ptr->next->data.num == num)
		 {
			 printf("工号:\t%4d姓名:\t%-5s性别:\t%-1s年龄:\t%3d学历:\t%-4s工资:\t%6d住址:\t%-10s电话:\t%-10d\n",
				 ptr->next->data.num,
				 ptr->next->data.name,
				 ptr->next->data.sex,
				 ptr->next->data.age,
				 ptr->next->data.educ,
				 ptr->next->data.salary,
				 ptr->next->data.adress,
				 ptr->next->data.phone);
			 do
			 {
				 int i;
				 printf("请输入要修改的信息(工号:1/姓名:2/性别:3/年龄:4/学历:5/工资:6/住址:7/电话:8):");
				 scanf("%d", &i);
				 switch (i)
				 {
				 case 1:
				 {
					 printf("请输入职工号:");
					 scanf("%d", &ptr->next->data.num);
					 break;
				 }
				 case 2:
				 {
					 printf("请输入职工姓名:");
					 scanf("%s", ptr->next->data.name);
					 break;
				 }
				 case 3:
				 {
					 printf("请输入职工性别(男/女):");
					 scanf("%s", ptr->next->data.sex);
					 break;
				 }
				 case 4:
				 {
					 printf("请输入职工年龄:");
					 scanf("%d", &ptr->next->data.age);
					 break;
				 }
				 case 5:
				 {
					 printf("请输入职工学历:");
					 scanf("%s", ptr->next->data.educ);
					 break;
				 }
				 case 6:
				 {
					 printf("请输入职工工资:");
					 scanf("%d", &ptr->next->data.salary);
					 break;
				 }
				 case 7:
				 {
					 printf("请输入职工住址:");
					 scanf("%s", ptr->next->data.adress);
					 break;
				 }
				 case 8:
				 {
					 printf("请输入职工电话:");
					 scanf("%d", &ptr->next->data.phone);
					 break;
				 }
				 default:
					 printf("输入有误,请重新输入:\n");
				 }
				 printf("若要继续修改请按1,结束修改请按0:");
				 scanf("%d", &choice);
			 } while (choice);
			 flag = 1;
			 break;
		 }
		 ptr = ptr->next;
	 }
	 if (flag)
		 printf("修改成功!\n");
	 else
		 printf("查无此人!修改失败!\n");
}
//导入文件信息
void Downlode(struct NodeEmploy* headptr)
{
	printf("导入文件中的职工信息中。。。。。。\n");
	struct NodeEmploy* p1 = headptr;	
	FILE* pf = fopen("Employ.dat", "rb");//打开文件
	if (pf == NULL)
	{
		printf("无可导入数据!");
		return;
	}
	if (feof(pf))
	{
		printf("无可导入数据!");
		return;
	}
	struct NodeEmploy* ptr = NULL;
	do
	{
	    ptr = (struct NodeEmploy*)malloc(sizeof(struct NodeEmploy));
		if (ptr == NULL)
		{
			free(ptr);
			printf("mallocError,导入失败!");
			return;

		}
		else
		{
			p1->next = ptr;
			size++;//size数量加1
			ptr->next = NULL;
			p1 = p1->next;

		}
	} while (fread(ptr, sizeof(struct NodeEmploy), 1, pf));
	fclose(pf);
    pf = NULL;
	struct NodeEmploy* p = headptr;
	while (p->next->next != NULL)
	{
		p = p->next;
	}
	free(p->next);
	p->next = NULL;
	size--;
    printf("导入职工信息成功!\n");
	printf("目前有%d个职工的信息!", size);
}
int main()
{
    int choice;
	//初始化链表
	struct NodeEmploy headnode;
	struct NodeEmploy* headptr=&headnode;
	headnode.next=NULL;
	size = 0;
	Downlode(headptr);
	//选择功能
	do
	{
		//进入菜单界面
	    Menu();
		printf("请选择功能:");
		scanf("%d", &choice);
		switch (choice)
		{
		case 1://职工录入
			InputEmploy(headptr);
			break;
		case 2://浏览职工
			Scan(headptr);
			break;
		case 3://查询职工
			Search(headptr);
			break;
		case 4://删除职工
			Delete(headptr);
			break;
		case 5://
			Correct(headptr);
			break;
		case 6://
			Save(headptr);
			break;
		case 0://
			break;
		default:	//选择错误则重新选择
			printf("选择错误,请重新选择:");
			break;
		}
	} while (choice != 0);
	return 0;
}

标签:职工,代码,next,headptr,printf,信息管理系统,NULL,data,ptr
来源: https://blog.csdn.net/weixin_50256876/article/details/118245121