C课设职工管理系统

建议使用vs编译器 

/*
项目名称:职工管理系统
制作人:2313040507
制作时间:2024.6.22
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct person {
	int ID;
	char name[28];
	int gender;
	int birth_day;
	int work_day;
	int phone_number;
	char education_background[28];
	char address[28];
	char job[28];
} person, * Staff;

void Print(Staff l, int n) {
	printf("\033[0;36m");
	printf("ID    Name   gender  birth_day\twork_day  phone_number\teducation_background\taddress\t  job\n");
	printf("\033[0m");
	for (int i = 0; i < n; i++) {
		printf("%d  %s\t%d    %d   %d   %8d\t%12s\t\t%s\t  %s\n",
			l[i].ID, l[i].name, l[i].gender, l[i].birth_day, l[i].work_day, l[i].phone_number,
			l[i].education_background, l[i].address, l[i].job);
	}
}

void Creat(person* L, int n)
{
	//printf("\033[0;33m");
	printf("请输入%d个人的数据:\n", n);
	//printf("\033[0m");
	for (int i = 0; i < n; i++)
	{
		printf("\033[0;33m");
		printf("第%d个人的数据:\n", i + 1);
		printf("\033[0m");
		int temp1;
		char temp2[20];

		printf("-ID:");
		scanf("%d", &temp1);
		L[i].ID = temp1;

		printf("-姓名:");
		scanf("%s", temp2);
		strcpy(L[i].name, temp2);

		printf("-性别:");
		scanf("%d", &temp1);
		L[i].gender = temp1;

		printf("-生日:");
		scanf("%d", &temp1);
		L[i].birth_day = temp1;

		printf("-工作日:");
		scanf("%d", &temp1);
		L[i].work_day = temp1;

		printf("-电话号码:");
		scanf("%d", &temp1);
		L[i].phone_number = temp1;

		printf("-教育经历:");
		scanf("%s", temp2);
		strcpy(L[i].education_background, temp2);

		printf("-地址:");
		scanf("%s", temp2);
		strcpy(L[i].address, temp2);

		printf("-职业:");
		scanf("%s", temp2);
		strcpy(L[i].job, temp2);

		printf("\n");
	}
}

int Delet(Staff* L, int* n, int d)
{
	for (int i = 0; i < *n; i++)
	{
		if ((*L)[i].ID == d)
		{
			for (int j = i; j < *n - 1; j++)
			{
				memcpy(&((*L)[j]), &((*L)[j + 1]), sizeof(person));
			}
			(*n)--;
			return 1;
		}
	}
	return 0;
}

void Sort_name(Staff L, int n) {
	person temp;
	for (int i = 0; i < n - 1; i++) {
		for (int j = 0; j < n - i - 1; j++) {
			if (strcmp(L[j].name, L[j + 1].name) > 0) {
				temp = L[j];
				L[j] = L[j + 1];
				L[j + 1] = temp;
			}
		}
	}
}

void Search(Staff l, int n, int id)
{
	for (int i = 0; i < n; i++)
	{
		if (l[i].ID == id)
		{
			printf("\033[0;36m");
			printf("ID    Name   gender  birth_day\twork_day  phone_number\teducation_background\taddress\t   job\n");
			printf("\033[0m");
			printf("%d  %s\t%d    %d   %d   %8d\t%12s\t\t%s     %s\n",
				l[i].ID, l[i].name, l[i].gender, l[i].birth_day, l[i].work_day, l[i].phone_number,
				l[i].education_background, l[i].address, l[i].job);
			return;
		}
	}
	printf("没有此ID!\n");
}

int compare1(const void* a, const void* b)
{
	return ((person*)a)->gender - ((person*)b)->gender;
}

void Sort_gender(person* L, int n)
{
	qsort(L, n, sizeof(person), compare1);
}

int compare(const void* a, const void* b)
{
	return ((person*)a)->ID - ((person*)b)->ID;
}

void Sort_id(person* L, int n)
{
	qsort(L, n, sizeof(person), compare);
}

int compare2(const void* a, const void* b)
{
	return ((person*)a)->birth_day - ((person*)b)->birth_day;
}

void Sort_age(person* L, int n)
{
	qsort(L, n, sizeof(person), compare2);
}

void Updata(person* L, int n, int id)
{
	printf("1.ID 2.名字 3.性别 4.生日 5.工作日 6.电话号码 7.教育经历 8.地址 9.职业 0.修改完成\n");
	for (int i = 0; i < n; i++)
	{
		if (L[i].ID == id)
		{
			int key = 0;
			int temp1;
			char temp2[20];
			while (1)
			{
				printf("*修改哪些数据:");
				scanf("%d", &key);
				switch (key)
				{
				case 1:
					printf("-输入新ID:");
					scanf("%d", &temp1);
					L[i].ID = temp1;
					break;
				case 2:
					printf("-输入新名字:");
					scanf("%s", temp2);
					strcpy(L[i].name, temp2);
					break;
				case 3:
					printf("-输入新性别:");
					scanf("%d", &temp1);
					L[i].gender = temp1;
					break;
				case 4:
					printf("-输入新生日:");
					scanf("%d", &temp1);
					L[i].birth_day = temp1;
					break;
				case 5:
					printf("-输入新工作日:");
					scanf("%d", &temp1);
					L[i].work_day = temp1;
					break;
				case 6:
					printf("-输入新电话号码:");
					scanf("%d", &temp1);
					L[i].phone_number = temp1;
					break;
				case 7:
					printf("-输入新教育经历:");
					scanf("%s", temp2);
					strcpy(L[i].education_background, temp2);
					break;
				case 8:
					printf("-输入新地址:");
					scanf("%s", temp2);
					strcpy(L[i].address, temp2);
					break;
				case 9:
					printf("-输入新职业:");
					scanf("%s", temp2);
					strcpy(L[i].job, temp2);
					break;
				case 0:
					return;
				default:
					printf("-重新输入\n");
					break;
				}
			}
		}
	}
	printf("查无此ID\n");
}

void Add(person* L, int* n)
{
	int temp1;
	char temp2[20];
	printf("-ID:");
	scanf("%d", &temp1);
	L[*n].ID = temp1;
	printf("-姓名:");
	scanf("%s", temp2);
	strcpy(L[*n].name, temp2);
	printf("-性别:");
	scanf("%d", &temp1);
	L[*n].gender = temp1;
	printf("-生日:");
	scanf("%d", &temp1);
	L[*n].birth_day = temp1;
	printf("-工作日:");
	scanf("%d", &temp1);
	L[*n].work_day = temp1;
	printf("-电话号码:");
	scanf("%d", &temp1);
	L[*n].phone_number = temp1;
	printf("-教育经历:");
	scanf("%s", temp2);
	strcpy(L[*n].education_background, temp2);
	printf("-地址:");
	scanf("%s", temp2);
	strcpy(L[*n].address, temp2);
	printf("-职业:");
	scanf("%s", temp2);
	strcpy(L[*n].job, temp2);
	(*n)++;
}

void Write_file(FILE* file, Staff l, int n) {
	fprintf(file, "ID\t\tName\tgender\tbirth_day\twork_day\tphone_number\teducation_background\taddress\tjob\n");
	for (int i = 0; i < n; i++) {
		fprintf(file, "%d\t%7s\t%d\t\t%d\t%d\t%d\t\t\t%20s\t\t%s\t%s\n",
			l[i].ID, l[i].name, l[i].gender, l[i].birth_day, l[i].work_day, l[i].phone_number,
			l[i].education_background, l[i].address, l[i].job);
	}
}

void Find_job(Staff l, int n)
{
	char temp[28];
	printf("要查找的职业:");
	scanf("%s", temp);
	printf("\033[0;36m");
	printf("ID    Name   gender  birth_day\twork_day  phone_number\teducation_background\taddress\t  job\n");
	printf("\033[0m");
	for (int i = 0; i < n; i++)
	{
		if (strcmp(l[i].job, temp) == 0)
		{
			printf("%d  %s\t%d    %d   %d   %8d\t%12s\t\t%s\t  %s\n",
				l[i].ID, l[i].name, l[i].gender, l[i].birth_day, l[i].work_day, l[i].phone_number,
				l[i].education_background, l[i].address, l[i].job);
		}
	}
}

int main()
{
	int n = 0;
	printf("职工数量:");
	scanf("%d", &n);
	int temp;
	Staff L = (Staff)malloc(n * sizeof(person));
	Creat(L, n);

	printf("\033[0;32m");
	printf("************************菜单**************************\n");
	printf("\033[0m");
	printf("1.输出所有数据\n2.查找ID\n3.查找职业\n4.按照名字排序\n5.按照ID排序\n6.年龄排序\n");
	printf("7.男女分开\n8.修改数据\n9.添加数据\n10.输出在文件中\n11.删除数据\n12.退出程序\n");
	printf("\033[0;32m");
	printf("******************************************************\n");
	printf("\033[0m");
	int key = 1;
	while (1)
	{
		printf("\033[0;33m");
		printf("\n@输入操作:");
		printf("\033[0m");
		scanf("%d", &key);
		switch (key)
		{
		case 1: {
			Print(L, n);
			break;
		}
		case 2: {
			printf("查找的人的ID:");
			scanf("%d", &temp);
			Search(L, n, temp);
			break;
		}
		case 3:
		{
			Find_job(L, n);
			break;
		}
		case 4:
		{
			Sort_name(L, n);
			printf("按照名字排序后:\n");
			Print(L, n);
			break;
		}
		case 5:
		{
			Sort_id(L, n);
			printf("按照ID排序后:\n");
			Print(L, n);
			break;
		}
		case 6:
		{
			printf("按照年龄排序后:\n");
			Sort_age(L, n);
			Print(L, n);
			break;
		}
		case 7:
		{
			Sort_gender(L, n);
			printf("按照性别分开后:\n");
			Print(L, n);
			break;
		}
		case 8:
		{
			printf("修改谁的数据:");
			scanf("%d", &temp);
			Updata(L, n, temp);
			break;
		}
		case 9:
		{
			Add(L, &n);
			break;
		}
		case 10:
		{
			FILE* file = fopen("output.txt", "w");
			if (file == NULL) {
				printf("Error opening file.\n");
				return 1;
			}

			Write_file(file, L, n);

			fclose(file);
			break;
		}
		case 11:
		{
			printf("删除的人的ID:");
			scanf("%d", &temp);
			if (Delet(&L, &n, temp))
				printf("成功删除!");
			else
				printf("删除失败");

			break;
		}
		case 12: {
			return 0;
		}
		default:
			printf("重新输入!");
		}
	}
	return 0;
}

/*
* 输入信息
4
1001
Lilac
0
20051115
20250708
123456
univercity
taiyvan
instructor
1002
Herzed
1
20050115
20230708
123457
univercity
taiyvan
doctor
1003
Kitty
0
19990115
20160708
125457
undergraduate
taiyvan
teacher
1004
Jeo
1
20040115
20230708
623457
undergraduate
taiyvan
doctor

*/

本设计用到16个函数

(1)Print函数:传入指向结构体数组的指针L和结构体中数据个数n,先打印出表头,再根据for循环打印出表中数据;为使得数据更直观,打印表头时使用printf(“\33[0;36”);将输出变为蓝色,随后使用printf(“\033[0m”);变回白色。

(2)Creat函数:传入指向结构体数组的指针L和结构体中数据个数n,先提示输入的数据,再利用temp1和temp2依次赋值,输入数据。Add函数原理相同。

(3)Delet函数:传入指向结构体数组的指针L和结构体中数据个数n以及要删除的人员的ID,第一层for找到要删除的ID后,利用第二层for将后面的值覆盖到前一项,其中用到memcpy函数实现结构体的更改,免去逐个赋值的麻烦;

之后再利用引用调用直接更改全局变量n的值。(为了与delete函数区分而命名为Delet)

(4)Updata函数:传入指向结构体数组的指针L和结构体中数据个数n以及要更改的人员的ID,找到ID后再使用while套switch让用户选择要更改的项,输入0更改完毕。

(5)Search函数:传入指向结构体数组的指针L和结构体中数据个数n以及要查找的人员的ID,for中使用if找到后输出。

(6)Sort_...函数:传入指向结构体数组的指针L和结构体中数据个数n,按照结构体的某一项对结构体数组排序。

按照整型排序时使用qsort函数,只要更改其中compare函数的返回值即可实现由不同指标排序。按照字符型的字典序排序时利用冒泡排序算法,strcmp比较大小,strcpy交换数据。

(7)Write_file函数:传入指向结构体数组的指针L和结构体中数据个数n,使用for和fprint将随意数据输入文件中。

(8)main函数:输入开始时职工数量、显示操作菜单,使用while套switch进行各种操作。

  • 9
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值