嵌入式(c)项目:分文件编写员工信息管理系统

 main.c

#include "func.h"
extern Person* Employee[1024]; //保存员工的信息
extern int g_count;//保存员工的人数
#pragma warning(disable : 4996)
int main(void)
{

	//界面
	Welcome();
	Delay(40000);
	//菜单界面
	int choice;
	while (1)
	{
		system("cls");
		menu();
		printf("请输入选项:\n");
		scanf_s("%d",&choice);
		switch (choice)
		{
		case 1:
			AddEmployee();
			break;
		case 2:
			DeleteEmployee();
			break;
		case 3:
			ModifiEmployee();
			break;
		case 4: 
			SearchEmployee();
			break;
		case 5:
			ShowEmployee();
			break;
		case 6:
			exit(0);
			break;
		default:
			break;
		}
	}
	return 0;

}

func.h

#ifndef _FUNC_H
#define _FUNC_H
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#pragma warning(disable : 4996)
//extern Person* Employee[1024]; //保存员工的信息
//extern int g_count;//保存员工的人数
typedef struct Person
{
	int id;
	char name[32];
	char sex[6];
	int age;
	char tel[12];
	double salary;
	
}Person;


/*
日期:2023.6.16
作者:zc
功能:欢迎界面
参数:
返回值:
*/
void Welcome();
/*
日期:2023.6.16
作者:zc
功能:菜单界面
参数:
返回值:void
*/
void menu();
/*
日期:2023.6.16
作者:zc
功能:延迟
参数:
返回值:void
*/
void Delay(int n);
/*
日期:2023.6.16
作者:zc
功能:添加员工信息
参数:
返回值:void
*/

void AddEmployee();
/*
日期:2023.6.16
作者:zc
功能:删除员工信息
参数:Person* Employee[1024]
返回值:void
*/

void DeleteEmployee();
/*
日期:2023.6.16
作者:zc
功能:修改员工信息
参数:Person* Employee[1024]
返回值:void
*/
void ModifiEmployee();
/*

日期:2023.6.16
作者:zc
功能:查找员工信息
参数:Person* Employee[1024]
返回值:void
*/
void SearchEmployee();
/*
日期:2023.6.16
作者:zc
功能:展示员工信息
参数:Person* Employee[1024]
返回值:void
*/
void ShowEmployee();
/*
日期:2023.6.16
作者:zc
功能:封装的字符串函数,用于比较字符
参数:const char* dest, const char* src
返回值:int
*/
int Mystrcmp(const char* dest, const char* src);
#endif // !_FUNC_H

func.c

#include "func.h"
#pragma warning(disable : 4996)
Person* Employee[1024]; //保存员工的信息
int g_count;//保存员工的人数
void Welcome()
{
	printf("\t\t****************************\n\n");
	printf("\t\t  欢迎使用员工管理系统\n\n");
	printf("\t\t****************************\n\n");
}
void menu()
{
	printf("\t\t****************************\n\n");
	printf("\t\t  1.添加人员 2.删除员工\n\n");
	printf("\t\t  3.修改信息 4.查找员工\n\n");
	printf("\t\t  5.显示信息 6.退出系统\n\n");
	printf("\t\t****************************\n\n");
}

void Delay(int n)
{
	int i = 0;
	
	while (i++ < n)
	{
		int j = 0;
		while (j++ < n);
	}
}
//添加员工信息
void AddEmployee()
{
	int id;    //id号不能重复
	char name[32];
	char sex[6];
	int age;
	char tel[12]; //号码不能重复
	double salary;
	printf("请输入需要添加的员工id号:");
	scanf("%d",&id);
	for (int i = 0; i < g_count; i++)
	{
		if (Employee[i]->id == id)
		{
			printf("该id已存在,请重新输入id号:");
			scanf("%d", &id);
			i = -1; 
		}
	}
	printf("请输入需要添加的员工名字:");
	scanf(" %s", name);
	printf("请输入需要添加的员工性别:");
	scanf(" %s", sex);
	printf("请输入需要添加的员工年龄:");
	scanf("%d", &age);
	printf("请输入需要添加的员工电话号码:");
	scanf(" %s", tel); 
	for (int i = 0; i < g_count; i++)
	{
		if (strcmp(Employee[i]->tel, tel) == 0)
		{
			printf("该手机号已存在,请重新输入手机号:");
			scanf("%s", tel);
			i = -1;
		}
	}
	printf("请输入需要添加的员工薪资:");
	scanf("%lf", &salary);
	Person* NewEmployee = (Person*)malloc(sizeof(Person));
	NewEmployee->id = id;
	strcpy(NewEmployee->name, name);
	strcpy(NewEmployee->sex, sex);
	NewEmployee->age = age;
	strcpy(NewEmployee->tel, tel);
	NewEmployee->salary = salary;
	Employee[g_count++] = NewEmployee;
	printf("员工添加成功!!!");
	getchar();
	getchar();
	return ;
}
//删除员工信息
void DeleteEmployee()
{
	int id;
	printf("请输入要删除员工的id:\n");
	scanf("%d", &id);
	int index = -1;
	for (int i = 0; i < g_count; i++)
	{
		if (Employee[i]->id == id)
		{
			index = i;
			break;
		}
	}

	if (index == -1)
	{
		printf("未找到指定id的员工!\n");
	}
	else
	{
		free(Employee[index]);
		for (int i = index; i < g_count - 1; i++)
		{
			Employee[i] = Employee[i + 1];
		}
		g_count--;
		printf("成功删除员工信息!\n");
	}

}
//修改员工信息
void ModifiEmployee()
{
	int id;    //id号不能重复
	char name[32];
	char sex[6];
	int age;
	char tel[12]; //号码不能重复
	double salary;
	printf("请输入要修改信息的员工的id:\n");
	scanf("%d", &id);
	int index = -1;
	for (int i = 0; i < g_count; i++)
	{
		if (Employee[i]->id == id)
		{
			index = i;
			break;
		}
	}
	if (index == -1)
	{
		printf("未找到指定id的员工!\n");
	}
	else
	{
		printf("请输入需要修改的员工id号:");
		scanf("%d", &Employee[index]->id);
		for (int i = 0; i < g_count; i++)
		{
			if (Employee[i]->id == id)
			{
				printf("该id已存在,请重新输入id号:");
				scanf("%d", &id);
				i = -1;
			}
		}
		printf("请输入需要修改的员工名字:");
		scanf(" %s", Employee[index]->name);
		printf("请输入需要修改的员工性别:");
		scanf("%s", Employee[index]->sex);
		printf("请输入需要修改的员工年龄:");
		scanf("%d", &Employee[index]->age);
		printf("请输入需要修改的员工电话号码:");
		scanf(" %s", Employee[index]->tel);
		for (int i = 0; i < g_count; i++)
		{
			if (Mystrcmp(Employee[i]->tel, tel) == 0)
			{
				printf("该手机号已存在,请重新输入手机号:");
				scanf("%s", tel);
				i = -1;
			}
		}
		printf("请输入需要修改的员工薪资:");
		scanf("%lf", &Employee[index]->salary);
		printf("成功修改员工信息!\n");
	}
}
//查找员工信息
void SearchEmployee()
{
	int id;
	int choice;
		printf("按id查找请输入1\n");
		printf("按名字查找请输入2\n");
		scanf("%d", &choice);
		if (choice == 1)
		{
			int index = -1;
			printf("请输入要查找员工的id:\n");
			scanf("%d", &id);
			for (int i = 0; i < g_count; i++)
			{
				if (Employee[i]->id == id)
				{
					index = i;
					break;
				}
			}
			if (index == -1)	
			{		
				printf("未找到指定id的员工!\n");	
			}	
			else
			{	
				printf("该员工id号:%d\n", Employee[index]->id);
				printf("该员工名字:%s\n", Employee[index]->name);
				printf("该员工性别:%s\n", Employee[index]->sex);
				printf("该员工年龄:%d\n", Employee[index]->age);
				printf("该员工电话号码:%s\n",Employee[index]->tel);
				printf("该员工薪资:%lf\n",Employee[index]->salary);
			}
			printf("按任意键继续显示员工信息...\n");
			getchar(); // 清除缓冲区中的回车符
			getchar(); // 等待用户输入任意字符

		}
		else if (choice == 2)
		{
			char name[32];
			int index = -1;
			printf("请输入要查找员工的名字:\n");
			scanf("%s", &name);
			for (int i = 0; i < g_count; i++)
			{
				if (Mystrcmp(Employee[i]->name, name) == 0)
				{
					index = i;
					break;
				}
			}
			if (index == -1)
			{
				printf("未找到指定id的员工!\n");
			}
			else
			{
				printf("该员工id号:%d\n", Employee[index]->id);
				printf("该员工名字:%s\n", Employee[index]->name);
				printf("该员工性别:%s\n", Employee[index]->sex);
				printf("该员工年龄:%d\n", Employee[index]->age);
				printf("该员工电话号码:%s\n", Employee[index]->tel);
				printf("该员工薪资:%lf\n", Employee[index]->salary);
			}
			printf("按任意键继续显示员工信息...\n");
			getchar(); // 清除缓冲区中的回车符
			getchar(); // 等待用户输入任意字符


		}
	
}
//展示员工信息
void ShowEmployee()
{
	Person* p = (Person*)malloc(sizeof(Person)*1);
	for (int i = 0; i <g_count - 1; i++)
	{ 
		for (int j = 0; j < g_count-1- i; j++)
		{
			if ((Employee[j]->id) > (Employee[j + 1]->id))
			{
				p = Employee[j];
				Employee[j] = Employee[j + 1];
				Employee[j + 1] = p;			
			}
		
		} 
		
	}
	printf(" \n 职工id号\t姓名\t性别\t年龄\t电话\t\t薪资\n");
	for (int k = 0; k < g_count; k++)
	{
		printf("\n %d\t\t%s\t%s\t%d\t%s\t%lf\t\n",Employee[k]->id, Employee[k]->name, Employee[k]->sex, Employee[k]->age, Employee[k]->tel, Employee[k]->salary);
	}
	getchar();
	getchar();

}
//封装的字符串函数
int Mystrcmp(const char* dest, const char* src)
{
	int i = 0;
	int j = 0;
	if (dest == NULL || src == NULL)
	{
		return 0;
	}
	while (dest != '\0' || src != '\0')
	{
		if (dest[i] > src[j])
		{
			return 1;

		}
		else if (dest[i] < src[j])
		{
			return -1;
		}
		else
		{
			return 0;
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值