IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 数据结构与算法 -> StaffManagementSystem -> 正文阅读

[数据结构与算法]StaffManagementSystem

#include<stdio.h>
#include<stdlib.h>

typedef struct Link
{
	struct Link * prior;//指向直接前趋

	char name[20];//姓名
	char gender[10];//性别
	char dateOfBirth[20];//出生年月
	char workingyears[20];//工作年月
	char education[10];//学历
	char occupation[20];//职务
	char address[50];//住址
	char tel[20];//电话

	struct Link * next;//指向直接后继
}Link;//职工信息

//导入职工信息
Link* Read(Link* head)//从职工信息文件读取到链表里,此函数返回一个指向链表头的指针
{
	FILE *fp = fopen("D:\\staff.txt", "r");//打开文件
	if (fp == NULL)
	{
		printf("文件打开失败!\n");
		exit(0);
	}

	Link *newStaff, *p = NULL;
	while (!feof(fp))//读取文件信息
	{
		newStaff = (Link *)malloc(sizeof(Link));//开辟动态存储区,起始地址赋给newUser
		fscanf(fp, "%s %s %s %s %s %s %s %s", 
			newStaff->name, newStaff->gender, newStaff->dateOfBirth, newStaff->workingyears, newStaff->education, newStaff->occupation, newStaff->address, newStaff->tel);
		
		if (head == NULL)
		{
			head = newStaff;
			head->prior = NULL;
		}
		else
		{
			p->next = newStaff;
			newStaff->prior = p;
		}
		p = newStaff;
		p->next = NULL;
	}
	fclose(fp);//关闭文件

	return head;
}
//新增一名职工
Link* InsertElem(Link* head)
{
	printf("新增职工信息,请输入:\n");
	//创建插入结点c
	Link* c = (Link*)malloc(sizeof(Link));
	printf("姓名 性别 出生年月 工作年月 学历 职务 住址 电话\n");
	scanf("%s %s %s %s %s %s %s %s",
		c->name, c->gender, c->dateOfBirth, c->workingyears, c->education, c->occupation, c->address, c->tel);
	c->next = NULL;

	Link* temp = head;//创建临时结点temp
	//找链表最末端
	if (temp == NULL)//链表本就空的,考虑特殊情况
		head = c;
	else
	{
		while (temp->next != NULL)
			temp = temp->next;
		//插入链表
		temp->next = c;
		c->prior = temp;

	}
	printf("================新增成功================\n");
	return  head;
}
//删除一名职工
Link* DeleteElem(Link* head)
{
	Link * temp = head;

	char nameT[20];
	printf("输入你要删除职工的姓名:\n");
	scanf("%s", nameT);
	if (temp != NULL)
	{
		while (strcmp(temp->name, nameT) != 0 && temp != NULL)
			temp = temp->next;
		if (temp == NULL)
		{
			printf("未找到相关职工信息\n");
			return;
		}
		printf("姓名 性别 出生年月 工作年月 学历 职务 住址 电话\n");
		printf("%s %s %s %s %s %s %s %s\n",
			temp->name, temp->gender, temp->dateOfBirth, temp->workingyears, temp->education, temp->occupation, temp->address, temp->tel);

		if (temp == head)
		{
			head = temp->next;
			head->prior = NULL;
			free(temp);
		}
		else if (temp->next == NULL)//最后一个结点
		{
			temp->prior->next = NULL;
			free(temp);
		}
		else
		{
			temp->prior->next = temp->next;
			temp->next->prior = temp->prior;
			free(temp);//删除该结点
		}
		printf("================删除成功================\n");
	}
	else
		printf("职工信息为空!\n");

	return head;
}
//查询
void Find(Link* head)
{

	printf("请输入要查询的职工条件(如:姓名、性别、出生年月、工作年月、学历、职务、住址或电话):");
	char inputO[50];
	scanf("%s", inputO);

	Link* temp = head;//开始根据要求查找
	while (temp != NULL)//字符串比较
	{
		if (strcmp(temp->name, inputO) == 0 || strcmp(temp->dateOfBirth, inputO) == 0 || strcmp(temp->workingyears, inputO) == 0 || strcmp(temp->education, inputO) == 0 || strcmp(temp->occupation, inputO) == 0 || strcmp(temp->address, inputO) == 0 || strcmp(temp->tel, inputO) == 0)
		{
			printf("姓名 性别 出生年月 工作年月 学历 职务 住址 电话\n");
			printf("%s %s %s %s %s %s %s %s\n",
				temp->name, temp->gender, temp->dateOfBirth, temp->workingyears, temp->education, temp->occupation, temp->address, temp->tel);
		}
		temp = temp->next;
	}

}
//修改
void Change(Link* head)
{
	Link* temp = head;

	char nameT[10];
	printf("输入你要修改的职工姓名:\n");
	scanf("%s", nameT);
	if (temp != NULL)
	{
		while (strcmp(temp->name, nameT) != 0 && temp != NULL)
			temp = temp->next;
		if (temp == NULL)
		{
			printf("未找到相关职工信息\n");
			return;
		}
		printf("现在的信息为:姓名 性别 出生年月 工作年月 学历 职务 住址 电话\n");
		printf("\t%s %s %s %s %s %s %s %s\n",
			temp->name, temp->gender, temp->dateOfBirth, temp->workingyears, temp->education, temp->occupation, temp->address, temp->tel);
		printf("输入更改后的信息:");
		printf("姓名 性别 出生年月 工作年月 学历 职务 住址 电话\n");
		scanf("%s %s %s %s %s %s %s %s",
			temp->name, temp->gender, temp->dateOfBirth, temp->workingyears, temp->education, temp->occupation, temp->address, temp->tel);
		printf("================修改成功================\n");
	}
	else
		printf("职工信息为空!\n");

}
//交换
void SwapCh(char* T, char* L)
{
	char ch[50];
	strcpy(ch, T);
	strcpy(T, L);
	strcpy(L, ch);
}
//排序(根据职工名字)
Link* Sort(Link* head)
{
	Link* temp = head;
	int swap = 1;// 默认为真

	while (swap)
	{
		swap = 0;
		//大数沉底
		while (temp->next != NULL)//从左往右大数沉底
		{
			if (strcmp(temp->name, temp->next->name) > 0)//比较
			{
				swap = 1;
				SwapCh(temp->name, temp->next->name);
				SwapCh(temp->gender, temp->next->gender);
				SwapCh(temp->dateOfBirth, temp->next->dateOfBirth);
				SwapCh(temp->workingyears, temp->next->workingyears);
				SwapCh(temp->education, temp->next->education);
				SwapCh(temp->occupation, temp->next->occupation);
				SwapCh(temp->address, temp->next->address);
				SwapCh(temp->tel, temp->next->tel);
			}
			temp = temp->next;//指针后移
		}

		//小数起泡
		while (temp->prior != NULL)//从右往左小数冒泡
		{
			if (strcmp(temp->name, temp->prior->name) < 0)
			{
				swap = 1;
				SwapCh(temp->name, temp->prior->name);
				SwapCh(temp->gender, temp->prior->gender);
				SwapCh(temp->dateOfBirth, temp->prior->dateOfBirth);
				SwapCh(temp->workingyears, temp->prior->workingyears);
				SwapCh(temp->education, temp->prior->education);
				SwapCh(temp->occupation, temp->prior->occupation);
				SwapCh(temp->address, temp->prior->address);
				SwapCh(temp->tel, temp->prior->tel);
			}
			temp = temp->prior;
		}
	}
	printf("================排序成功================\n");
	return head;
}
//导出
void write(Link* head)
{
	FILE *fp;
	if ((fp = fopen("D:\\staff.txt", "wb")) == NULL)
	{
		printf("\nFailed to open file.");
		getchar();
		exit(1);
	}
	while (head)
	{
		fprintf(fp, "%s %s %s %s %s %s %s %s\n",
			head->name, head->gender, head->dateOfBirth, head->workingyears, head->education, head->occupation, head->address, head->tel);
		head = head->next;
	}
	fclose(fp);

}
//菜单
int menu()
{
	int m;
	do{
		printf("\t================职工管理系统================\n\n");
		printf("\t\t1. 新增一名职工\n");
		printf("\t\t2. 删除一名职工\n");
		printf("\t\t3. 查询\n");
		printf("\t\t4. 修改\n");
		printf("\t\t5. 排序\n");
		printf("\t\t0. 退出\n\n");
		printf("\t================请选择================\n\n");
		scanf("%d", &m);

	} while (m < 0 && m>5);//菜单选项外跳出

	return m;
}
int main()
{
	Link *head = NULL;
	head = Read(head);
	for (;;)//死循环
	{
		switch (menu())
		{
		case 1:head = InsertElem(head); write(head); break;
		case 2:head = DeleteElem(head); write(head); break;
		case 3:Find(head); write(head); break;
		case 4:Change(head); write(head); break;
		case 5:head = Sort(head); write(head); break;
		case 0:exit(0); break;
		default:printf("================输入错误================\n"); break;
		}

	}
	return 0;
}

staff.txt:
张三 男 1997.12.01 2年5月 本科 工人 花园小区 13525471586
王五 男 1998.12.01 3年5月 本科 工人 花园小区 18459632586
赵四 女 1999.12.01 4年5月 本科 工人 花园小区 14875695252
李二 男 2000.12.01 3年5月 硕士 经理 佳玉小区 18799845526

  数据结构与算法 最新文章
【力扣106】 从中序与后续遍历序列构造二叉
leetcode 322 零钱兑换
哈希的应用:海量数据处理
动态规划|最短Hamilton路径
华为机试_HJ41 称砝码【中等】【menset】【
【C与数据结构】——寒假提高每日练习Day1
基础算法——堆排序
2023王道数据结构线性表--单链表课后习题部
LeetCode 之 反转链表的一部分
【题解】lintcode必刷50题<有效的括号序列
上一篇文章      下一篇文章      查看所有文章
加:2021-09-26 10:26:26  更:2021-09-26 10:27:31 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年4日历 -2024/4/27 0:18:04-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码