【升级版学生信息管理系统&员工工资信息管理系统】+文件操作+更多细节

目录

1.功能介绍(主菜单-有什么功能)

 2.结构体的定义(定义一个结构体类型)

3.主函数(如何调用分函数)

4.初始化顺序表(数组加上数组的附加信息)

5.退出程序同时保存数据到文档

6.增加员工信息

7.删除员工信息

 8.按照名字查找(查询工资信息)

9. 修改员工信息

10. 按照工资排序

11.按照工号排序

12. 计算工资信息

 13.打印工资信息

14. 统计员工总数

15.default语句

 16.源代码


1.功能介绍(主菜单-有什么功能)

讲一下:这个0123的标号的伏笔

//菜单函数
void Meau(void)
{
	printf("--------------------------------\n");
	printf("---  欢迎来到员工工资管理系统 ---\n");
	printf("--------------------------------\n");
	printf("----     0.退出程序         ----\n");
	printf("----     1.增加工资信息     ----\n");
	printf("----     2.删除工资信息     ----\n");
	printf("----     3.按名字查工资     ----\n");
	printf("----     4.修改工资信息     ----\n");
	printf("----     5.按照工资排序     ----\n");
	printf("----     6.按照工号排序     ----\n");
	printf("----     7.计算员工工资     ----\n");
	printf("----     8.打印工资信息     ----\n");
	printf("----     9.统计员工人数     ----\n");
	printf("-------------------------------\n");
}

 2.结构体的定义(定义一个结构体类型)

1.画一个盒子的嵌套图

2.各种结构体类型成员的命名和函数命名规范

3.typedef类型重命名

4.size和capacity 的引入

5.计算员工工资放的位置

typedef struct Employee
{
	char name[20];
	char id[20];
	double InitWages;
	double bonus;
	double deduct;
	double NextWages;
	double taxes;
	double FinaWages;
}Employee;

typedef struct SeqList
{
	Employee* emp;
	int size;
	int capacity;
}SeqList;

3.主函数(如何调用分函数)

1.do while语句(前后呼应)

2.switch case  break;语句和case内部的每一步在干干嘛

3.传值调用和传址调用

int main()
{
	int input = 0;

	SeqList ST;
	InitSeqList(&ST);

	do//先执行一次
	{
		Meau();
		printf("请输入您的选择(提示:请在0-9之间选择):>");
		scanf("%d", &input);
		switch (input)
		{
		case 0:
			printf("欢迎您的使用,即将退出程序.\n");
			SaveSeqList(&ST);
			break;
		case 1:
			printf("********增加工资信息.********\n");
			int nums = 0;
			printf("请输入您要增加的员工信息的员工数量:>");
			scanf("%d", &nums);
			Employee temp = { 0 };
			for (int i = 1; i <= nums; i++)
			{
				InitEmployee(&temp,i);
				AddSeqList(&ST,temp);
			}
			CountSeqList(&ST);
			break;
		case 2:
			printf("********删除工资信息.********\n");
			DelSeqList(&ST);
			break;
		case 3:
			printf("********查询工资信息.********\n");
			SearByName(&ST);
			break;
		case 4:
			printf("********修改工资信息.********\n");
			ModifySeqList(&ST);
			CountSeqList(&ST);
			break;
		case 5:
			printf("********按照工资排序.(从高到低)********\n");
			QsortByFinaWages(&ST,0,ST.size);
			PrintSeqList(&ST);
			break;
		case 6:
			printf("********按照工号排序.(从小到大)********\n");
			ShellSortById(&ST);
			PrintSeqList(&ST);
			break;
		case 7:
			printf("********计算员工工资********\n");
			CountSeqList(&ST);
			printf("计算成功.\n");
			break; 
		case 8:
			printf("********打印工资信息********\n");
			PrintSeqList(&ST);
			break;
		case 9:
			printf("********统计员工人数********\n");
			printf("当前系统人数:%d\n", ST.size);
		default:
			printf("********输入错误,请重新输入********\n");
			break;
		}
	} while (input);
}

以下是功能的具体实现

4.初始化顺序表(数组加上数组的附加信息)

1.size和capacity的关系-->扩容(检查是否满了)

2.初始化

3.把上一次的数据加载到文件中-->持久化

主:

SeqList ST;
	InitSeqList(&ST);

分:

void CheckCapacity(SeqList* ps)
{
	if (ps->size == ps->capacity)
	{
		int newcapacity = 2 * ps->capacity;
		Employee* temp = (Employee*)realloc(ps->emp,sizeof(Employee) * newcapacity);
		if (temp==NULL)
		{
			perror("CheckCapacity::realloc");
			return;
		}
		ps->emp = temp;
		ps->capacity = newcapacity;
		printf("扩容成功\n");
	}
}

void LoadSeqList(SeqList* ps)
{
	FILE* pf = fopen("D:\\桌面\\test.txt", "rb");
	if (pf == NULL)
	{
		perror("LoadSeqList::fopen");
		return;
	}
	Employee temp = { 0 };
	while (fread(&temp, sizeof(Employee), 1, pf))
	{
		CheckCapacity(ps);
		ps->emp[ps->size] = temp;
		ps->size++;
	}

	fclose(pf);
	pf = NULL;
}

void InitSeqList(SeqList* ps)
{
	ps->emp = (Employee*)malloc(sizeof(Employee) * 4);
	if (ps->emp == NULL)
	{
		perror("InitSeqList::malloc");
		return;
	}
	ps->size = 0;
	ps->capacity = 4;

	LoadSeqList(ps);
}

5.退出程序同时保存数据到文档

1.文件的分类,为什么要有文件操作

2.关于fopen函数,fread函数,fwrite函数

主函数调用

		case 0:
			printf("欢迎您的使用,即将退出程序.\n");
			SaveSeqList(&ST);
			break;

分函数实现

void SaveSeqList(SeqList* ps)
{
	FILE* pf = fopen("D:\\桌面\\test.txt", "wb");
	if (pf == NULL)
	{
		perror("LoadSeqList::fopen");
		return;
	}

	for (int i = 0; i < ps->size; i++)
	{
		fwrite(ps->emp + i, sizeof(Employee), 1, pf);
	}

	fclose(pf);
	pf = NULL;
}

6.增加员工信息

1.打包操作

2.顺序表的插入

主:

		case 1:
			printf("********增加工资信息.********\n");
			int nums = 0;
			printf("请输入您要增加的员工信息的员工数量:>");
			scanf("%d", &nums);
			Employee temp = { 0 };
			for (int i = 1; i <= nums; i++)
			{
				InitEmployee(&temp,i);
				AddSeqList(&ST,temp);
			}
			CountSeqList(&ST);
			break;

分:

void InitEmployee(Employee* ps, int i)
{
	printf("请输入第%d位待插入的员工工资信息:\n",i);
	printf("请输入员工姓名:>");
	scanf("%s", ps->name);
	printf("请输入员工工号:>");
	scanf("%s", ps->id);
	printf("请输入员工的基本工资:>");
	scanf("%lf", &(ps->InitWages));
	printf("请输入员工的奖金:>");
	scanf("%lf", &(ps->bonus));
	printf("请输入员工的扣款:>");
	scanf("%lf", &(ps->deduct));
	printf("请输入员工的税款:>");
	scanf("%lf", &(ps->taxes));
}

void AddSeqList(SeqList* ps, Employee temp)
{
	CheckCapacity(ps);
	ps->emp[ps->size] = temp;
	ps->size++;
	printf("增加成功.\n");
}

7.删除员工信息

0.局部变量和全局变量

1.输入姓名,定位下标(封装成函数),strcmp函数,不存在返回-1 的原因

2.存在则执行顺序表的删除操作,覆盖问题

主:

		case 2:
			printf("********删除工资信息.********\n");
			DelSeqList(&ST);
			break;

分:


int FindByName(SeqList* ps,char* name)
{
	for (int i = 0; i < ps->size; i++)
	{
		if (strcmp(name, ps->emp[i].name) == 0)
		{
			return i;
		}
	}
	return -1;
}

void DelSeqList(SeqList* ps)
{
	if (ps->size == 0)
	{
		printf("当前员工总数为0,无法删除.\n");
	}
	char name[20] = { 0 };
	printf("请输入您要删除的员工信息的员工姓名:>");
	scanf("%s", name);
	int pos = FindByName(ps,name);
	if (pos == -1)
	{
		printf("当前系统中不存在名字为%s的员工.\n", name);
		return;
	}
	else
	{
		int begin = pos + 1;
		while (begin < ps->size)
		{
			ps->emp[begin -1] = ps->emp[begin];
			begin++;
		}
		ps->size--;
	}
	printf("删除成功.\n");
}

 8.按照名字查找(查询工资信息)

1.同上的定位函数

2.打印下标为i的员工信息

主:

		case 3:
			printf("********查询工资信息.********\n");
			SearByName(&ST);
			break;

分:


void SearByName(SeqList* ps)
{
	char name[20] = { 0 };
	printf("请输入您要查询的员工信息的员工姓名:>");
	scanf("%s", name);
	int pos = FindByName(ps, name);
	if (pos == -1)
	{
		printf("当前系统中不存在名字为%s的员工.\n", name);
		return;
	}
	else
	{
		printf("%-20s %-20s %-10s %-10s %-10s %-10s %-10s %-10s\n", "员工姓名", "员工工号", "基本工资", "奖金", "扣款", "应发工资", "税款", "实发工资");
		printf("%-20s %-20s %-10.2lf %-10.2lf %-10.2lf %-10.2lf %-10.2lf %-10.2lf\n", \
			ps->emp[pos].name, ps->emp[pos].id, ps->emp[pos].InitWages, ps->emp[pos].bonus, ps->emp[pos].deduct, ps->emp[pos].NextWages, ps->emp[pos].taxes, ps->emp[pos].FinaWages);
	}
}

9. 修改员工信息

1.同上

2.修改if else if和go to 语句

主:

		case 4:
			printf("********修改工资信息.********\n");
			ModifySeqList(&ST);
			CountSeqList(&ST);
			break;

分: 

void ModifySeqList(SeqList* ps)
{

	char name[20] = { 0 };
	printf("请输入您要修改的员工信息的员工姓名:>");
	scanf("%s", name);
	int pos = FindByName(ps, name);
	if (pos == -1)
	{
		printf("当前系统中不存在名字为%s的员工.\n", name);
		return;
	}
	char choice[20] = { 0 };
rechoice:
	printf("请输入你要修改的信息项目名称");
	printf("(提示:您可选择输入员工工号,基本工资,奖金,扣款,税款):>");
	scanf("%s", choice);
	printf("请输入修改后的该信息项目值:>");
	if (strcmp(choice, "员工工号") == 0)
	{
		scanf("%s", ps->emp[pos].id);
	}
	else if (strcmp(choice, "基本工资") == 0)
	{
		scanf("%lf", &(ps->emp[pos].InitWages));
	}
	else if (strcmp(choice, "奖金") == 0)
	{
		scanf("%lf", &(ps->emp[pos].bonus));
	}
	else if (strcmp(choice, "扣款") == 0)
	{
		scanf("%lf", &(ps->emp[pos].deduct));
	}
	else if (strcmp(choice, "税款") == 0)
	{
		scanf("%lf", &(ps->emp[pos].taxes));
	}
	else
	{
		printf("请在提示中给出的选项中做出选择!\n");
		goto rechoice;
	}
	printf("修改成功.\n");
}

10. 按照工资排序

1.快速排序的原理

主:

		case 5:
			printf("********按照工资排序.(从高到低)********\n");
			QsortByFinaWages(&ST,0,ST.size);
			PrintSeqList(&ST);
			break;

分:

void QsortByFinaWages(SeqList* ps, int begin, int end)
{
	if (begin >= end)
	{
		return;
	}
	int keyi = begin;
	int left = begin, right = end;
	while (left < right)
	{
		while (left < right && ps->emp[right].FinaWages <= ps->emp[keyi].FinaWages)
		{
			right--;
		}
		while (left < right && ps->emp[left].FinaWages >= ps->emp[keyi].FinaWages)
		{
			left++;
		}
		Swap(&(ps->emp[left]), &(ps->emp[right]));
	}
	int meeti = left;
	Swap(&(ps->emp[keyi]), &(ps->emp[meeti]));

	QsortByFinaWages(ps, begin, meeti - 1);
	QsortByFinaWages(ps, meeti+1, end);
}

11.按照工号排序

1.直接插入排序

2.希尔排序的原理

主:

		case 6:
			printf("********按照工号排序.(从小到大)********\n");
			ShellSortById(&ST);
			PrintSeqList(&ST);
			break;

分:

void ShellSortById(SeqList* ps)
{
	int gap = ps->size;
	while (gap > 1)
	{
		gap = (gap / 3 + 1);
		for (int i = 0; i < ps->size - gap; i++)
		{
			int end = i;
			Employee temp = ps->emp[i + gap];
			while (end >= 0)
			{
				if (strcmp(ps->emp[end].id , temp.id)>0)
				{
					ps->emp[end + gap] = ps->emp[end];
					end -= gap;
				}
				else
				{
					break;
				}
			}
			ps->emp[end + gap] = temp;
			
		}
	}
}

12. 计算工资信息

数学计算

主:

		case 7:
			printf("********计算员工工资********\n");
			CountSeqList(&ST);
			printf("计算成功.\n");

分:

void CountSeqList(SeqList* ps)
{
	for (int i = 0; i < ps->size; i++)
	{
		ps->emp[i].NextWages = ps->emp[i].InitWages + ps->emp[i].bonus - ps->emp[i].deduct;
		ps->emp[i].FinaWages = ps->emp[i].NextWages - ps->emp[i].taxes;
	}

}

 13.打印工资信息

格式控制

主:

		case 8:
			printf("********打印工资信息********\n");
			PrintSeqList(&ST);
			break;

分:

void PrintSeqList(SeqList* ps)
{
	printf("%-20s %-20s %-10s %-10s %-10s %-10s %-10s %-10s\n", "员工姓名", "员工工号", "基本工资", "奖金", "扣款", "应发工资","税款","实发工资");
	for (int i = 0; i < ps->size; i++)
	{
		printf("%-20s %-20s %-10.2lf %-10.2lf %-10.2lf %-10.2lf %-10.2lf %-10.2lf\n",\
			ps->emp[i].name,ps->emp[i].id, ps->emp[i].InitWages, ps->emp[i].bonus, ps->emp[i].deduct,ps->emp[i].NextWages,ps->emp[i].taxes,ps->emp[i].FinaWages);
	}
}

14. 统计员工总数

		case 9:
			printf("********统计员工人数********\n");
			printf("当前系统人数:%d\n", ST.size);

15.default语句

		default:
			printf("********输入错误,请重新输入********\n");
			break;

 16.源代码

//Employee.c


#define _CRT_SECURE_NO_WARNINGS 1
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<windows.h>

typedef struct Employee
{
	char name[20];
	char id[20];
	double InitWages;
	double bonus;
	double deduct;
	double NextWages;
	double taxes;
	double FinaWages;
}Employee;

typedef struct SeqList
{
	Employee* emp;
	int size;
	int capacity;
}SeqList;

void CheckCapacity(SeqList* ps)
{
	if (ps->size == ps->capacity)
	{
		int newcapacity = 2 * ps->capacity;
		Employee* temp = (Employee*)realloc(ps->emp,sizeof(Employee) * newcapacity);
		if (temp==NULL)
		{
			perror("CheckCapacity::realloc");
			return;
		}
		ps->emp = temp;
		ps->capacity = newcapacity;
		printf("扩容成功\n");
	}
}

void LoadSeqList(SeqList* ps)
{
	FILE* pf = fopen("D:\\桌面\\test.txt", "rb");
	if (pf == NULL)
	{
		perror("LoadSeqList::fopen");
		return;
	}
	Employee temp = { 0 };
	while (fread(&temp, sizeof(Employee), 1, pf))
	{
		CheckCapacity(ps);
		ps->emp[ps->size] = temp;
		ps->size++;
	}

	fclose(pf);
	pf = NULL;
}

void InitSeqList(SeqList* ps)
{
	ps->emp = (Employee*)malloc(sizeof(Employee) * 4);
	if (ps->emp == NULL)
	{
		perror("InitSeqList::malloc");
		return;
	}
	ps->size = 0;
	ps->capacity = 4;

	LoadSeqList(ps);
}

//菜单函数
void Meau(void)
{
	printf("--------------------------------\n");
	printf("---  欢迎来到员工工资管理系统 ---\n");
	printf("--------------------------------\n");
	printf("----     0.退出程序         ----\n");
	printf("----     1.增加工资信息     ----\n");
	printf("----     2.删除工资信息     ----\n");
	printf("----     3.按名字查工资     ----\n");
	printf("----     4.修改工资信息     ----\n");
	printf("----     5.按照工资排序     ----\n");
	printf("----     6.按照工号排序     ----\n");
	printf("----     7.计算员工工资     ----\n");
	printf("----     8.打印工资信息     ----\n");
	printf("----     9.统计员工人数     ----\n");
	printf("-------------------------------\n");
}

void SaveSeqList(SeqList* ps)
{
	FILE* pf = fopen("D:\\桌面\\test.txt", "wb");
	if (pf == NULL)
	{
		perror("LoadSeqList::fopen");
		return;
	}

	for (int i = 0; i < ps->size; i++)
	{
		fwrite(ps->emp + i, sizeof(Employee), 1, pf);
	}

	fclose(pf);
	pf = NULL;
}



void InitEmployee(Employee* ps, int i)
{
	printf("请输入第%d位待插入的员工工资信息:\n",i);
	printf("请输入员工姓名:>");
	scanf("%s", ps->name);
	printf("请输入员工工号:>");
	scanf("%s", ps->id);
	printf("请输入员工的基本工资:>");
	scanf("%lf", &(ps->InitWages));
	printf("请输入员工的奖金:>");
	scanf("%lf", &(ps->bonus));
	printf("请输入员工的扣款:>");
	scanf("%lf", &(ps->deduct));
	printf("请输入员工的税款:>");
	scanf("%lf", &(ps->taxes));
}

void AddSeqList(SeqList* ps, Employee temp)
{
	CheckCapacity(ps);
	ps->emp[ps->size] = temp;
	ps->size++;
	printf("增加成功.\n");
}


int FindByName(SeqList* ps,char* name)
{
	for (int i = 0; i < ps->size; i++)
	{
		if (strcmp(name, ps->emp[i].name) == 0)
		{
			return i;
		}
	}
	return -1;
}

void DelSeqList(SeqList* ps)
{
	if (ps->size == 0)
	{
		printf("当前员工总数为0,无法删除.\n");
	}
	char name[20] = { 0 };
	printf("请输入您要删除的员工信息的员工姓名:>");
	scanf("%s", name);
	int pos = FindByName(ps,name);
	if (pos == -1)
	{
		printf("当前系统中不存在名字为%s的员工.\n", name);
		return;
	}
	else
	{
		int begin = pos + 1;
		while (begin < ps->size)
		{
			ps->emp[begin -1] = ps->emp[begin];
			begin++;
		}
		ps->size--;
	}
	printf("删除成功.\n");
}

void SearByName(SeqList* ps)
{
	char name[20] = { 0 };
	printf("请输入您要查询的员工信息的员工姓名:>");
	scanf("%s", name);
	int pos = FindByName(ps, name);
	if (pos == -1)
	{
		printf("当前系统中不存在名字为%s的员工.\n", name);
		return;
	}
	else
	{
		printf("%-20s %-20s %-10s %-10s %-10s %-10s %-10s %-10s\n", "员工姓名", "员工工号", "基本工资", "奖金", "扣款", "应发工资", "税款", "实发工资");
		printf("%-20s %-20s %-10.2lf %-10.2lf %-10.2lf %-10.2lf %-10.2lf %-10.2lf\n", \
			ps->emp[pos].name, ps->emp[pos].id, ps->emp[pos].InitWages, ps->emp[pos].bonus, ps->emp[pos].deduct, ps->emp[pos].NextWages, ps->emp[pos].taxes, ps->emp[pos].FinaWages);
	}
}



void ModifySeqList(SeqList* ps)
{

	char name[20] = { 0 };
	printf("请输入您要修改的员工信息的员工姓名:>");
	scanf("%s", name);
	int pos = FindByName(ps, name);
	if (pos == -1)
	{
		printf("当前系统中不存在名字为%s的员工.\n", name);
		return;
	}
	char choice[20] = { 0 };
rechoice:
	printf("请输入你要修改的信息项目名称");
	printf("(提示:您可选择输入员工工号,基本工资,奖金,扣款,税款):>");
	scanf("%s", choice);
	printf("请输入修改后的该信息项目值:>");
	if (strcmp(choice, "员工工号") == 0)
	{
		scanf("%s", ps->emp[pos].id);
	}
	else if (strcmp(choice, "基本工资") == 0)
	{
		scanf("%lf", &(ps->emp[pos].InitWages));
	}
	else if (strcmp(choice, "奖金") == 0)
	{
		scanf("%lf", &(ps->emp[pos].bonus));
	}
	else if (strcmp(choice, "扣款") == 0)
	{
		scanf("%lf", &(ps->emp[pos].deduct));
	}
	else if (strcmp(choice, "税款") == 0)
	{
		scanf("%lf", &(ps->emp[pos].taxes));
	}
	else
	{
		printf("请在提示中给出的选项中做出选择!\n");
		goto rechoice;
	}
	printf("修改成功.\n");
}

void Swap(Employee* a, Employee* b)
{
	Employee temp = *a;
	*a = *b;
	*b = temp;
}


void QsortByFinaWages(SeqList* ps, int begin, int end)
{
	if (begin >= end)
	{
		return;
	}
	int keyi = begin;
	int left = begin, right = end;
	while (left < right)
	{
		while (left < right && ps->emp[right].FinaWages <= ps->emp[keyi].FinaWages)
		{
			right--;
		}
		while (left < right && ps->emp[left].FinaWages >= ps->emp[keyi].FinaWages)
		{
			left++;
		}
		Swap(&(ps->emp[left]), &(ps->emp[right]));
	}
	int meeti = left;
	Swap(&(ps->emp[keyi]), &(ps->emp[meeti]));

	QsortByFinaWages(ps, begin, meeti - 1);
	QsortByFinaWages(ps, meeti+1, end);
}

void ShellSortById(SeqList* ps)
{
	int gap = ps->size;
	while (gap > 1)
	{
		gap = (gap / 3 + 1);
		for (int i = 0; i < ps->size - gap; i++)
		{
			int end = i;
			Employee temp = ps->emp[i + gap];
			while (end >= 0)
			{
				if (strcmp(ps->emp[end].id , temp.id)>0)
				{
					ps->emp[end + gap] = ps->emp[end];
					end -= gap;
				}
				else
				{
					break;
				}
			}
			ps->emp[end + gap] = temp;
			
		}
	}
}



void CountSeqList(SeqList* ps)
{
	for (int i = 0; i < ps->size; i++)
	{
		ps->emp[i].NextWages = ps->emp[i].InitWages + ps->emp[i].bonus - ps->emp[i].deduct;
		ps->emp[i].FinaWages = ps->emp[i].NextWages - ps->emp[i].taxes;
	}

}

void PrintSeqList(SeqList* ps)
{
	printf("%-20s %-20s %-10s %-10s %-10s %-10s %-10s %-10s\n", "员工姓名", "员工工号", "基本工资", "奖金", "扣款", "应发工资","税款","实发工资");
	for (int i = 0; i < ps->size; i++)
	{
		printf("%-20s %-20s %-10.2lf %-10.2lf %-10.2lf %-10.2lf %-10.2lf %-10.2lf\n",\
			ps->emp[i].name,ps->emp[i].id, ps->emp[i].InitWages, ps->emp[i].bonus, ps->emp[i].deduct,ps->emp[i].NextWages,ps->emp[i].taxes,ps->emp[i].FinaWages);
	}
}

int main()
{
	int input = 0;

	SeqList ST;
	InitSeqList(&ST);

	do//先执行一次
	{
		Meau();
		printf("请输入您的选择(提示:请在0-9之间选择):>");
		scanf("%d", &input);
		switch (input)
		{
		case 0:
			printf("欢迎您的使用,即将退出程序.\n");
			SaveSeqList(&ST);
			break;
		case 1:
			printf("********增加工资信息.********\n");
			int nums = 0;
			printf("请输入您要增加的员工信息的员工数量:>");
			scanf("%d", &nums);
			Employee temp = { 0 };
			for (int i = 1; i <= nums; i++)
			{
				InitEmployee(&temp,i);
				AddSeqList(&ST,temp);
			}
			CountSeqList(&ST);
			break;
		case 2:
			printf("********删除工资信息.********\n");
			DelSeqList(&ST);
			break;
		case 3:
			printf("********查询工资信息.********\n");
			SearByName(&ST);
			break;
		case 4:
			printf("********修改工资信息.********\n");
			ModifySeqList(&ST);
			CountSeqList(&ST);
			break;
		case 5:
			printf("********按照工资排序.(从高到低)********\n");
			QsortByFinaWages(&ST,0,ST.size);
			PrintSeqList(&ST);
			break;
		case 6:
			printf("********按照工号排序.(从小到大)********\n");
			ShellSortById(&ST);
			PrintSeqList(&ST);
			break;
		case 7:
			printf("********计算员工工资********\n");
			CountSeqList(&ST);
			printf("计算成功.\n");
			break; 
		case 8:
			printf("********打印工资信息********\n");
			PrintSeqList(&ST);
			break;
		case 9:
			printf("********统计员工人数********\n");
			printf("当前系统人数:%d\n", ST.size);
		default:
			printf("********输入错误,请重新输入********\n");
			break;
		}
	} while (input);
}

17.效果预览图

 

类似的,下面为学生信息管理系统 

#define _CRT_SECURE_NO_WARNINGS 1

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

typedef struct Student
{
	char name[20];
	char id[20];
	float ChiScore;
	float MatScore;
	float EngScore;
	float SumScore;
}Student;

typedef struct SeqList
{
	Student* stu;
	int size;
	int capacity;
}SeqList;


void meau1(void)
{
	printf("--------------------------------\n");
	printf("--- 欢迎使用学生信息管理系统 ---\n");
	printf("--------------------------------\n");
	printf("----     0.退出程序         ----\n");
	printf("----     1.录入学生信息     ----\n");
	printf("----     2.删除学生信息     ----\n");
	printf("----     3.查询指定课程     ----\n");
	printf("----     4.查询指定学生     ----\n");
	printf("----     5.修改学生信息     ----\n");
	printf("----     6.按照学号排序     ----\n");
	printf("----     7.按照总分排序     ----\n");
	printf("----     8.统计学生总数     ----\n");
	printf("----     9.打印学生信息     ----\n");
	printf("--------------------------------\n");
}

//case -1 初始化: 初始化函数(3)

void CheckCapacity(SeqList* ps)
{
	if (ps->size == ps->capacity)
	{
		int newcapacity = 2 * ps->capacity;
		Student* temp = (Student*)realloc(ps->stu, sizeof(Student) * newcapacity);
		if (temp == NULL)
		{
			perror("CheckCapacity::realloc:>");
			return;
		}
		ps->capacity = newcapacity;
		ps->stu = temp;
		printf("扩容成功.\n");
	}
}

void LoadSeqList(SeqList* ps)
{
	FILE* pf = fopen("D:\\桌面\\test.txt", "rb");
	if (pf == NULL)
	{
		perror("LoadSeqList::fopen:>");
		return;
	}

	Student temp = { 0 };
	while (fread(&temp, sizeof(Student), 1, pf))
	{
		CheckCapacity(ps);
		ps->stu[ps->size] = temp;
		ps->size++;
	}

	fclose(pf);
	pf = NULL;
}

void InitSeqList(SeqList* ps)
{
	ps->stu = (Student*)malloc(sizeof(Student) * 4);
	if (ps->stu == NULL)
	{
		perror("InitSeqList::malloc:>");
		return;
	}
	ps->capacity = 4;
	ps->size = 0;

	LoadSeqList(ps);
}

//case 0 退出程序:保存数据到文件函数(1)

void SaveSeqList(SeqList* ps)
{
	FILE* pf = fopen("D:\\桌面\\test.txt", "wb");
	if (pf == NULL)
	{
		perror("LoadSeqList::fopen:>");
		return;
	}

	for (int i = 0; i < ps->size; i++)
	{
		fwrite(ps->stu + i, sizeof(Student), 1, pf);
    }

	fclose(pf);
	pf = NULL;
}



//case 1:录入学生信息 装配和插入函数(2)

void InitStudent(Student* ps,int i)
{
	printf("请输入第%d个待录入的学生信息:\n", i);

	printf("请输入学生的名字:>");
	scanf("%s", ps->name);
	printf("请输入学生的学号:>");
	scanf("%s", ps->id);

	printf("请输入学生的语文成绩(0-100):>");
	scanf("%f", &(ps->ChiScore));
	printf("请输入学生的数学成绩(0-100):>");
	scanf("%f", &(ps->MatScore));
	printf("请输入学生的英语成绩(0-100):>");
	scanf("%f", &(ps->EngScore));

	ps->SumScore = ps->ChiScore + ps->MatScore + ps->EngScore;
}

void AddSeqList(SeqList* ps,Student temp)
{
	CheckCapacity(ps);
	ps->stu[ps->size] = temp;
	ps->size++;
}



//case 2:删除学生信息  菜单函数,定位和删除函数(4) 

void meau2(void)
{
	printf("--  1.按照名字查找  --\n");
	printf("--  2.按照学号查找  --\n");
}

//找得到返回下标,找不到返回-1
int FindByName(SeqList* ps, const char* name)
{
	for (int i = 0; i < ps->size; i++)
	{
		if (strcmp(ps->stu[i].name, name) == 0)
		{
			return i;
		}
	}
	return -1;
}

int FindById(SeqList* ps, const char* id)
{
	for (int i = 0; i < ps->size; i++)
	{
		if (strcmp(ps->stu[i].id, id) == 0)
		{
			return i;
		}
	}
	return -1;
}

void DelSeqList(SeqList* ps)
{
	if (ps->size == 0)
	{
		printf("当前学生总数为0,无法删除.\n");
		return;
	}
	meau2();
	int choice1 = 0;
	rechoice1:
	printf("请根据您要按何种方式查找到要删除的学生,输入相应的编号:>");
	scanf("%d", &choice1);
	int pos = 0;
	if (choice1 == 1)
	{
		char name[20] = { 0 };
		printf("请输入您要删除学生的学生姓名:>");
		scanf("%s", name);
		pos = FindByName(ps, name);
	}
	else if (choice1 == 2)
	{
		char id[20] = { 0 };
		printf("请输入您要删除学生的学生学号:>");
		scanf("%s", id);
		pos = FindById(ps, id);
	}
	else
	{
		printf("选择错误,请重新选择(提示:请在1-2中选择).\n");
		goto rechoice1;
	}

	if (pos == -1)
	{
		printf("您现在要删除的学生不存在当前学生信息管理系统中,请重新选择.\n");
		goto rechoice1;
	}
	else
	{
		int begin = pos + 1;
		while (begin < ps->size)
		{
			ps->stu[begin - 1] = ps->stu[begin];
			begin++;
		}
		ps->size--;
		printf("删除成功.\n");
	}

}



//case 3: 查询指定课程 菜单函数,定位和杂碎函数(10)

void meau3(void)
{
	printf("--  1.语文  --\n");
	printf("--  2.数学  --\n");
	printf("--  3.英语  --\n");
}

void Swap(char* buff1, char* buff2,int width)
{
	while (width--)
	{
		char temp = *buff1;
		*buff1 = *buff2;
		*buff2 = temp;
		buff1++;
		buff2++;
	}
}


int cmp_struct_by_ChiScore(const void* e1, const void* e2)
{
	return ((Student*)e2)->ChiScore - ((Student*)e1)->ChiScore;
}

int cmp_struct_by_MatScore(const void* e1, const void* e2)
{
	return ((Student*)e2)->MatScore - ((Student*)e1)->MatScore;
}

int cmp_struct_by_EngScore(const void* e1, const void* e2)
{
	return ((Student*)e2)->EngScore - ((Student*)e1)->EngScore;
}

void BubbleSort(void* base, int size, int width, float (*cmp)(const void*, const void*))
{
	int flag = 0;
	for (int i = 0; i < size - 1; i++)
	{
		for (int j = 0; j < size-1- i; j++)
		{
			if (cmp((char*)base + j * width, (char*)base + (j + 1) * width) > 0)
			{
				Swap((char*)base + j * width, (char*)base + (j + 1) * width, width);
				flag = 1;
			}
		}
		if (flag == 0)
		{
			break;
		}
	}
}

void DisPlayByChiScore(SeqList* ps)
{
	printf("%-20s %-20s %-20s %-20s\n", "姓名", "学号", "语文成绩", "语文成绩排名");
	for (int i = 0; i < ps->size; i++)
	{
		printf("%-20s %-20s %-20.2f %-20d\n", ps->stu[i].name, ps->stu[i].id, ps->stu[i].ChiScore, i + 1);
	}
}

void DisPlayByMatScore(SeqList* ps)
{
	printf("%-20s %-20s %-20s %-20s\n", "姓名", "学号", "数学成绩", "数学成绩排名");
	for (int i = 0; i < ps->size; i++)
	{
		printf("%-20s %-20s %-20.2f %-20d\n", ps->stu[i].name, ps->stu[i].id, ps->stu[i].MatScore, i + 1);
	}
}

void DisPlayByEngScore(SeqList* ps)
{
	printf("%-20s %-20s %-20s %-20s\n", "姓名", "学号", "英语成绩", "英语成绩排名");
	for (int i = 0; i < ps->size; i++)
	{
		printf("%-20s %-20s %-20.2f %-20d\n", ps->stu[i].name, ps->stu[i].id, ps->stu[i].EngScore, i + 1);
	}
}

void SearByCourse(SeqList* ps)
{
	if (ps->size == 0)
	{
		printf("当前学生总数为0,无法修改.\n");
		return;
	}
	meau3();
	int choice2 = 0;
	rechoice2:
	printf("请根据您要查询课程的名称,选择相应的编号:>");
	scanf("%d", &choice2);
	float sum = 0.00;
	int count1 = 0, count2 = 0;
	printf("\t\t***********查询课程结果***********\n\n");
	if (choice2 == 1)
	{
		for (int i = 0; i < ps->size; i++)
		{
			sum += ps->stu[i].ChiScore;

			if (ps->stu[i].ChiScore < 60)
			{
				count1++;
			}
			else if (ps->stu[i].ChiScore < 90)
			{
				count2++;
			}
		}
		BubbleSort(ps->stu, ps->size, sizeof(Student), cmp_struct_by_ChiScore);
		DisPlayByChiScore(ps);
	}
	else if (choice2 == 2)
	{
		for (int i = 0; i < ps->size; i++)
		{
			sum += ps->stu[i].MatScore;

			if (ps->stu[i].MatScore < 60)
			{
				count1++;
			}
			else if (ps->stu[i].MatScore < 90)
			{
				count2++;
			}
		}
		BubbleSort(ps->stu, ps->size, sizeof(Student), cmp_struct_by_MatScore);
		DisPlayByMatScore(ps);
	}
	else if (choice2 == 3)
	{
		for (int i = 0; i < ps->size; i++)
		{
			sum += ps->stu[i].EngScore;

			if (ps->stu[i].EngScore < 60)
			{
				count1++;
			}
			else if (ps->stu[i].EngScore < 90)
			{
				count2++;
			}
		}
		BubbleSort(ps->stu, ps->size, sizeof(Student), cmp_struct_by_EngScore);
		DisPlayByEngScore(ps);
	}
	else
	{
		printf("选择错误,请重新选择(提示:请在1-3中选择).\n");
		goto rechoice2;
	}

	printf("\n\n% -20s % -20s % -20s % -20s % -20s\n", "不及格人数(0-60)", "及格人数(60-100)", "良好人数(60-90)", "优秀人数(90-100)","平均分");
	printf("%-20d %-20d %-20d %-20d %-20.2f\n", count1, ps->size - count1, count2, ps->size - count1 - count2, sum / ps->size);


}


//case 4:查询指定学生 

void SearByStudent(SeqList* ps)
{
	if (ps->size == 0)
	{
		printf("当前学生总数为0,无法查询.\n");
		return;
	}
	meau2();
	int choice3 = 0;
rechoice3:
	printf("请根据您要按何种方式查找学生,输入相应的编号:>");
	scanf("%d", &choice3);
	int pos = 0;
	if (choice3 == 1)
	{
		char name[20] = { 0 };
		printf("请输入您要查询学生的学生姓名:>");
		scanf("%s", name);
		pos = FindByName(ps, name);
	}
	else if (choice3 == 2)
	{
		char id[20] = { 0 };
		printf("请输入您要查询学生的学生学号:>");
		scanf("%s", id);
		pos = FindById(ps, id);
	}
	else
	{
		printf("选择错误,请重新选择(提示:请在1-2中选择).\n");
		goto rechoice3;
	}

	if (pos == -1)
	{
		printf("您现在要查询的学生不存在当前学生信息管理系统中,请重新选择.\n");
		goto rechoice3;
	}
	else
	{
		printf("\t\t***********查询学生结果***********\n");
		printf("%-20s %-20s %-20s %-20s %-20s %-20s %-20s\n","姓名","学号", "语文成绩", "数学成绩", "英语成绩", "总成绩", "总成绩排名");
		printf("%-20s %-20s %-20.2f %-20.2f %-20.2f %-20.2f %-20d\n", ps->stu[pos].name, ps->stu[pos].id, \
			ps->stu[pos].ChiScore, ps->stu[pos].MatScore, ps->stu[pos].EngScore, ps->stu[pos].SumScore, pos + 1);
	}
}

//case 5: 修改学生成绩 -菜单函数和修改函数 -先找到这个学生,再进行修改

void ModSeqList(SeqList* ps)
{
	if (ps->size == 0)
	{
		printf("当前学生总数为0,无法修改.\n");
		return;
	}
	meau2();
	int choice4 = 0;
	rechoice4:
	printf("请根据您要按何种方式查找到要修改的学生,输入相应的编号:>");
	scanf("%d", &choice4);
	int pos = 0;
	if (choice4 == 1)
	{
		char name[20] = { 0 };
		printf("请输入您要修改的学生信息的学生姓名:>");
		scanf("%s", name);
		pos = FindByName(ps, name);
	}
	else if (choice4 == 2)
	{
		char id[20] = { 0 };
		printf("请输入您要修改的学生信息的学生学号:>");
		scanf("%s", id);
		pos = FindById(ps, id);
	}
	else
	{
		printf("选择错误(提示:请在1-2中选择).\n");
		goto rechoice4;
	}

	if (pos == -1)
	{
		printf("您现在要查询的学生不存在当前学生信息管理系统中,请重新选择.\n");
		goto rechoice4;
	}
	else
	{
		meau3();
		int input = 0;
		again:
		printf("请根据您要修改的课程成绩的课程名称,选择相应的编号:>");
		scanf("%d", &input);
		printf("请输入修改后的值:>");
		if (input == 1)
		{
			scanf("%f", &(ps->stu[pos].ChiScore));
		}
		else if (input == 2)
		{
			scanf("%f", &(ps->stu[pos].EngScore));
		}
		else if (input == 3)
		{
			scanf("%f", &(ps->stu[pos].EngScore));
		}
		else
		{
			printf("选择错误,请重新选择 (提示:请在1-3中选择):>");
			goto again;
		}
		printf("修改成功.\n");
	}
}

//case 6: 按照学号排序 -排升序

void Swap2(Student* s1, Student* s2)
{
	Student temp = *s1;
	*s1 = *s2;
	*s2 = temp;
}

void QsortById(SeqList* ps, int begin, int end)
{
	if (begin >= end)
	{
		return;
	}
	int keyi = begin;
	int left = begin, right = end;
	while (left < right)
	{
		while (left < right && strcmp(ps->stu[right].id, ps->stu[keyi].id)>0)
		{
			right--;
		}
		while (left < right && strcmp(ps->stu[left].id, ps->stu[keyi].id) <= 0)
		{
			left++;
		}
		Swap2(&(ps->stu[left]), &(ps->stu[right]));
	}
	int meeti = left;
	Swap2(&(ps->stu[meeti]), &(ps->stu[keyi]));
	QsortById(ps, begin, meeti - 1);
	QsortById(ps, meeti + 1, end);
}

//case 7: 按照总成绩排序-排降序

void ShellSortBySumScore(SeqList* ps)
{
	int gap = ps->size;
	while (gap > 1)
	{
		gap = (gap / 3 + 1);
		for (int i = 0; i < ps->size - gap; i++)
		{
			int end = i;
			Student temp = ps->stu[end + gap];
			while (end >= 0)
			{
				if (temp.SumScore > ps->stu[end].SumScore)
				{
					ps->stu[end + gap] = ps->stu[end];
					end--;
				}
				else
				{
					break;
				}
			}
			ps->stu[end + gap] = temp;
		}
	}
}

//case 9: 打印学生信息

void PrintSeqList(SeqList* ps)
{
	if (ps->size == 0)
	{
		printf("当前学生总数为0,无法打印.\n");
		return;
	}
	printf("%-20s %-20s %-20s %-20s %-20s %-20s %-20s\n", "姓名", "学号", "语文成绩", "数学成绩", "英语成绩", "总成绩", "排名");

	for (int i = 0; i < ps->size; i++)
	{
		printf("%-20s %-20s %-20.2f %-20.2f %-20.2f %-20.2f %-20d\n", ps->stu[i].name, ps->stu[i].id, \
			ps->stu[i].ChiScore, ps->stu[i].MatScore, ps->stu[i].EngScore, ps->stu[i].SumScore, i+1);
	}
}

int main()
{
	SeqList ST;
	InitSeqList(&ST);

	int input = 0;

	do
	{
		meau1();
		printf("请输入您的选择(提示:只能在0-9之间做选择):>");
		scanf("%d", &input);

		switch (input)
		{
		case 0:
			printf("\t******感谢您的使用,即将退出程序******\n");
			SaveSeqList(&ST);
			break;

		case 1:
			printf("\t******录入学生信息******\n");
			int nums = 0;
			printf("请输入您要录入的学生个数:>");
			scanf("%d", &nums);

			Student temp = { 0 };
			for (int i = 1; i <= nums; i++)
			{
				InitStudent(&temp, i);
				AddSeqList(&ST, temp);
			}
			printf("录入成功.\n");
			break;

		case 2:
			printf("\t******删除学生信息******\n");
			DelSeqList(&ST);
			break;

		case 3:
			printf("\t******查询指定课程******\n");
			SearByCourse(&ST);
			break;

		case 4:
			printf("\t******查询指定学生******\n");
			SearByStudent(&ST);
			break;

		case 5:
			printf("\t******修改学生信息******\n");
			ModSeqList(&ST);
			break;

		case 6:
			printf("\t******按照学号排序 (由小到大)******\n");
			QsortById(&ST, 0, ST.size);
			PrintSeqList(&ST);
			break;

		case 7:
			printf("\t******按照总分排序 (由高到低)******\n");
			ShellSortBySumScore(&ST);
			PrintSeqList(&ST);
			break;

		case 8:
			printf("\t******统计学生总数******\n");
			printf("当前学生总数:%d\n", ST.size);
			break;

		case 9:
			printf("\t******打印学生信息******\n");
			PrintSeqList(&ST);
			break;

		default:
			printf("选择错误,请重新选择(提示:请在0-9中选择):>");
			break;
		}
	} while (input);
}

  • 18
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 19
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 19
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码上心头

为爱发电

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值