C语言:程序设计综合训练

综合实验二:学生成绩管理系统

1、要求

(一)程序运行时,首先显示主菜单如下:

  1. 新建数据
  2. 添加数据
  3. 删除数据
  4. 排序
  5. 查询
  6. 退出

屏幕提示:请输入序号选择相应操作。
要求当用户输入任一序号后,程序都能进行相应操作。

(二)在主菜单中选择序号4,弹出子菜单选择排序方式,子菜单如下:

  1. 数学成绩排序
  2. 程序设计成绩排序
  3. 总分排序。
  4. 返回主菜单

请按序号选择相应操作。
选择子菜单的序号后,程序能正确运行并在屏幕上显示按要求排序后的相关信息。
(三)在主菜单中选择序号5,弹出子菜单选择查询方式,子菜单如下:

  1. 学号查询
  2. 姓名查询
  3. 数学成绩查询
  4. 程序设计成绩查询
  5. 总分查询
  6. 返回主菜单

请按序号选择相应操作。

在子菜单中选择序号后,程序按以下方式工作:

1)学号查询:输入学号后,若该学号存在则显示与其相关的所有信息,否则显示找不到的提示信息;(提示:查询到满足条件的结果后,查询即可结束)
2)姓名查询:输入姓名后,若该姓名存在则显示与其相关的所有信息,否则显示找不到的提示信息;(提示:使用字符串比较函数进行比较)
3)按科目查询:输入指定分数,程序运行后显示该科目中考试成绩大于等于指定分数的同学的学号、姓名以及该科成绩并统计满足条件的人数;
4)总分查询:输入指定分数,程序运行后显示总分成绩大于等于指定分数的同学的学号、姓名以及各科成绩并统计满足条件的人数。

2、代码

#include<stdio.h>
#include<windows.h>
#include<stdlib.h>
#include<string.h>
typedef struct Node
{
	long int ID;// 学号
	char Name[50];// 姓名
	int Math;// 数学成绩
	int C;// 程序设计成绩
	int Score;// 总分

	struct Node* next;// 指针域
}node;

node list;// 链表

// 读取文件
int Read_FILE(node* L);

// 保存文件
int  Save_FILE(node* L);

// 主菜单界面
void welcome();

// 新建学生信息
void Build(node* L);

// 增加学生信息
void Add(node* L, node e);// 功能
void Add_Printf();// 界面

// 删除学生信息
void Delete_Printf(node* L);// 界面
void Delete(node* s);// 功能

// 排序学生信息
void Sort(node* L);
void Print(node* L);// 输出学生信息
void Print_Printf();// 界面
bool cmp_big_Math(node e1, node e2);// 数学成绩从大到小
bool cmp_small_Math(node e1, node e2);// 数学成绩从小到大
bool cmp_big_C(node e1, node e2);// 程序设计成绩从大到小
bool cmp_small_C(node e1, node e2);// 程序设计成绩从小到大
bool cmp_big_Score(node e1, node e2);// 成绩从大到小
bool cmp_small_Score(node e1, node e2);// 成绩从小到大

// 查询学生信息
void Search_Printf(node* L);// 界面
node* Search_id(int id, node* L);// 按学号进行查找
node* Search_name(char name[], node* L);// 按姓名进行查找
void Search_math(int math, node* L);// 按数学成绩进行查找
void Search_c(int c, node* L);// 按程序设计成绩进行查找
void Search_score(int score, node* L);// 按总分进行查找

// 退出管理系统
void goodbye();

int main()
{
	int choice = 0;
	Read_FILE(&list);
	while (true)
	{
		welcome();
		scanf("%d", &choice);
		switch (choice)
		{
		case 1:// 新建学生信息
			Build(&list);
			break;
		case 2:// 增加学生信息
			Add_Printf();
			break;
		case 3:// 删除学生信息
			Delete_Printf(&list);
			break;
		case 4:// 排序学生信息
			Sort(&list);
			break;
		case 5:// 查询学生信息
			Search_Printf(&list);
			break;
		case 6:// 退出管理系统
			goodbye();
			break;
		}
		printf("是否需要继续操作?(Yes:1 / No:0):");
		scanf("%d", &choice);
		if (choice == 0)
		{
			break;
		}
	}
	return 0;
}

void welcome()
{
	system("cls");
	printf("****************************************************************\n");
	printf("***********             学生成绩管理系统             ***********\n");
	printf("***********          1 ---- 新建学生信息             ***********\n");
	printf("***********          2 ---- 增加学生信息             ***********\n");
	printf("***********          3 ---- 删除学生信息             ***********\n");
	printf("***********          4 ---- 排序学生信息             ***********\n");
	printf("***********          5 ---- 查询学生信息             ***********\n");
	printf("***********          6 ---- 退出管理系统             ***********\n");
	printf("****************************************************************\n");

	printf("请选择想要实现的功能(数字):");
}

// 读取文件
int Read_FILE(node* L)
{
	FILE* pfRead = fopen("student_information.txt", "r");
	node st;
	node* s;
	node* t = L;

	if (pfRead == NULL)
	{
		return 0;
	}

	while (fscanf(pfRead, "%ld %s %d %d %d", &st.ID, st.Name, &st.Math, &st.C, &st.Score) != EOF)
	{
		s = (node*)malloc(sizeof(node));

		*s = st;

		// 尾插法
		t->next = s;
		t = s;
		t->next = NULL;
	}

	return 1;
}

// 保存文件
int Save_FILE(node* L)
{
	FILE* pfWrite = fopen("student_information.txt", "w");
	if (pfWrite == NULL)
	{
		return 0;
	}

	node* p = L->next;

	while (p != NULL)
	{
		fprintf(pfWrite, " %ld %s %d %d %d\n", p->ID, p->Name, p->Math, p->C, p->Score);
		p = p->next;
	}

	return 1;
}

// 新建学生信息
void Build(node* L)
{
	node* p, * q;
	p = L->next;
	while (p != NULL) {
		q = p->next;
		free(p);
		p = q;
	}
	L->next = NULL;
	printf("新建成功!\n");
}

// 增加学生信息
void Add_Printf()
{
	system("cls");
	node st;
	printf("请输入新增学生的相关信息:\n");
	printf("学号:");
	scanf("%ld", &st.ID);
	printf("姓名:");
	scanf("%s", st.Name);
	printf("数学成绩:");
	scanf("%d", &st.Math);
	printf("程序设计成绩:");
	scanf("%d", &st.C);
	st.Score = st.Math + st.C;

	Add(&list, st);
}

void Add(node* L, node e)
{
	// 头插法
	node* p = L;
	node* s = (node*)malloc(sizeof(node));
	*s = e;

	s->next = p->next;
	p->next = s;

	Save_FILE(L);
}

// 删除学生信息
void Delete_Printf(node* L)
{
	system("cls");
	long int id;

	node* p;

	printf("请输入要删除的学生的学号:");
	scanf("%ld", &id);
	node* st = Search_id(id, L);
	p = st;

	if (st == NULL)
	{
		printf("查无此人!\n");
		return;
	}

	st = st->next;
	printf("________________________________________________________\n");
	printf("|学号\t|姓名\t|数学成绩\t|程序设计成绩\t|总分\t|\n");
	printf("________________________________________________________\n");
	printf("%ld|%s\t|%d\t\t|%d\t\t|%d\t|\n", st->ID, st->Name, st->Math, st->C, st->Score);
	printf("________________________________________________________\n");

	Delete(p);
	// 保存信息
	Save_FILE(L);
}

void Delete(node* s)
{
	node* t = s->next;

	s->next = t->next;
	t->next = NULL;

	free(t);
}

// 排序学生信息
void Sort(node* L)
{
	while (1)
	{
		system("cls");
		int choice1 = 0, choice2 = 0;
		printf("按照数学成绩排序     ---- 1\n");
		printf("按照程序设计成绩排序 ---- 2\n");
		printf("按照总分排序         ---- 3\n");
		printf("返回主菜单           ---- 4\n");
		printf("请选择排序方式:");
		scanf("%d", &choice1);

		system("cls");

		if (choice1 == 1 || choice1 == 2 || choice1 == 3)
		{
			printf("从大到小排序 ---- 1\n");
			printf("从小到大排序 ---- 2\n");
			printf("请选择排序方式:");
			scanf("%d", &choice2);
		}

		int flag = 0;
		for (node* p = L->next; p != NULL; p = p->next)
		{
			for (node* q = p; q != NULL; q = q->next)
			{
				switch (choice1)
				{
				case 1:
					if (choice2 == 1) 
					{
						if (!cmp_big_Math(*p, *q))
						{
							flag = 1;
						}
					}
					else if (choice2 == 2)
					{
						if (!cmp_small_Math(*p, *q))
						{
							flag = 1;
						}
					}
					break;
				case 2:
					if (choice2 == 1) 
					{
						if (!cmp_big_C(*p, *q))
						{
							flag = 1;
						}
					}
					else if (choice2 == 2)
					{
						if (!cmp_small_C(*p, *q))
						{
							flag = 1;
						}
					}
					break;
				case 3:
					if (choice2 == 1)
					{
						if (!cmp_big_Score(*p, *q))
						{
							flag = 1;
						}
					}
					else if (choice2 == 2)
					{
						if (!cmp_small_Score(*p, *q))
						{
							flag = 1;
						}
					}
					break;
				case 4:
					return;
				}
				if (flag == 1)
				{
					// 交换数据域
					node t = *p;
					*p = *q;
					*q = t;
					// 处理指针域
					t.next = p->next;
					p->next = q->next;
					q->next = t.next;
					flag = 0;
				}
			}
		}

		Print(L);

		printf("是否继续排序该学生信息?(Yes:1 / No:0):");
		scanf("%d", &choice1);
		if (choice1 == 0)
		{
			break;
		}
	}
}

// 输出学生信息
void Print(node* L)
{
	system("cls");
	node* p = L->next;
	Print_Printf();
	if (p != NULL)
	{
		while (p != NULL)
		{
			printf("________________________________________________________\n");
			printf("%ld|%s\t|%d\t\t|%d\t\t|%d\t|\n", p->ID, p->Name, p->Math, p->C, p->Score);
			printf("________________________________________________________\n");
			p = p->next;
		}
	}
}

void Print_Printf()
{
	printf("________________________________________________________\n");
	printf("|学号\t|姓名\t|数学成绩\t|程序设计成绩\t|总分\t|\n");
	printf("________________________________________________________\n");
}

// 数学成绩从大到小
bool cmp_big_Math(node e1, node e2)
{
	return e1.Math > e2.Math;
}

// 数学成绩从小到大
bool cmp_small_Math(node e1, node e2)
{
	return e1.ID < e2.ID;
}

// 程序设计成绩从大到小
bool cmp_big_C(node e1, node e2)
{
	return e1.C > e2.C;
}

// 程序设计成绩从小到大
bool cmp_small_C(node e1, node e2)
{
	return e1.C < e2.C;
}

// 成绩从大到小
bool cmp_big_Score(node e1, node e2)
{
	return e1.Score > e2.Score;
}

// 成绩从小到大
bool cmp_small_Score(node e1, node e2)
{
	return e1.Score < e2.Score;
}

// 查询学生信息
void Search_Printf(node* L)
{
	while (1)
	{
		system("cls");
		int choice = 0;
		printf("学号查询         ---- 1\n");
		printf("姓名查询         ---- 2\n");
		printf("数学成绩查询     ---- 3\n");
		printf("程序设计成绩查询 ---- 4\n");
		printf("总分查询         ---- 5\n");
		printf("返回主菜单       ---- 6\n");
		printf("请输入查询方式:");
		scanf("%d", &choice);

		int id, math, c, score;
		char name[50];
		node* st;
		if (choice == 1)
		{
			printf("请输入要查询的学号:");
			scanf("%d", &id);
			st = Search_id(id, L);

			if (st == NULL)
			{
				printf("查无此人!\n");
			}
			else
			{
				st = st->next;
				printf("________________________________________________________\n");
				printf("|学号\t|姓名\t|数学成绩\t|程序设计成绩\t|总分\t|\n");
				printf("________________________________________________________\n");
				printf("%ld|%s\t|%d\t\t|%d\t\t|%d\t|\n", st->ID, st->Name, st->Math, st->C, st->Score);
				printf("________________________________________________________\n");
			}
		}
		else if (choice == 2)
		{
			printf("请输入要查询的姓名:");
			scanf("%s", name);
			st = Search_name(name, L);

			if (st == NULL)
			{
				printf("查无此人!\n");
			}
			else
			{
				st = st->next;
				printf("________________________________________________________\n");
				printf("|学号\t|姓名\t|数学成绩\t|程序设计成绩\t|总分\t|\n");
				printf("________________________________________________________\n");
				printf("%ld|%s\t|%d\t\t|%d\t\t|%d\t|\n", st->ID, st->Name, st->Math, st->C, st->Score);
				printf("________________________________________________________\n");
			}
		}
		else if (choice == 3)
		{
			printf("请输入要查询的数学成绩:");
			scanf("%d", &math);

			Search_math(math, L);
		}
		else if (choice == 4)
		{
			printf("请输入要查询的程序设计成绩:");
			scanf("%d", &c);

			Search_c(c, L);
		}
		else if (choice == 5)
		{
			printf("请输入要查询的总分:");
			scanf("%d", &score);

			Search_score(score, L);
		}
		else if (choice == 6)
			return;
		printf("是否继续查询该学生信息?(Yes:1 / No:0):");
		scanf("%d", &choice);
		if (choice == 0)
		{
			break;
		}
	}
}

// 按学号进行查找
node* Search_id(int id, node* L)
{
	node* p = L;

	while (p->next != NULL)
	{
		if (p->next->ID == id)
		{
			return p;
		}
		p = p->next;
	}
	return NULL;
}

// 按姓名进行查找
node* Search_name(char name[], node* L)
{
	node* p = L;

	while (p->next != NULL)
	{
		if (strcmp(name, p->next->Name) == 0)
		{
			return p;
		}
		p = p->next;
	}
	return NULL;
}

// 按数学成绩进行查找
void Search_math(int math, node* L)
{
	node* p = L;
	int count = 0;

	while (p->next != NULL)
	{
		if (p->next->Math >= math)
		{
			if(!count)
			{
				printf("________________________________________________________\n");
				printf("|学号\t|姓名\t|数学成绩\t|程序设计成绩\t|总分\t|\n");
				printf("________________________________________________________\n");
			}
			printf("________________________________________________________\n");
			printf("%ld|%s\t|%d\t\t|%d\t\t|%d\t|\n", p->next->ID, p->next->Name, p->next->Math, p->next->C, p->next->Score);
			printf("________________________________________________________\n");

			count++;
		}
		p = p->next;
	}

	if (count)
		printf("满足条件的人数:%d\n", count);
	else
		printf("没有满足条件的人!\n");
}

// 按程序设计成绩进行查找
void Search_c(int c, node* L)
{
	node* p = L;
	int count = 0;

	while (p->next != NULL)
	{
		if (p->next->C >= c)
		{
			if (!count)
			{
				printf("________________________________________________________\n");
				printf("|学号\t|姓名\t|数学成绩\t|程序设计成绩\t|总分\t|\n");
				printf("________________________________________________________\n");
			}
			printf("________________________________________________________\n");
			printf("%ld|%s\t|%d\t\t|%d\t\t|%d\t|\n", p->next->ID, p->next->Name, p->next->Math, p->next->C, p->next->Score);
			printf("________________________________________________________\n");

			count++;
		}
		p = p->next;
	}

	if (count)
		printf("满足条件的人数:%d\n", count);
	else
		printf("没有满足条件的人!\n");
}

// 按总分进行查找
void Search_score(int score, node* L)
{
	node* p = L;
	int count = 0;

	while (p->next != NULL)
	{
		if (p->next->Score >= score)
		{
			if (!count)
			{
				printf("________________________________________________________\n");
				printf("|学号\t|姓名\t|数学成绩\t|程序设计成绩\t|总分\t|\n");
				printf("________________________________________________________\n");
			}
			printf("________________________________________________________\n");
			printf("%ld|%s\t|%d\t\t|%d\t\t|%d\t|\n", p->next->ID, p->next->Name, p->next->Math, p->next->C, p->next->Score);
			printf("________________________________________________________\n");

			count++;
		}
		p = p->next;
	}

	if (count)
		printf("满足条件的人数:%d\n", count);
	else
		printf("没有满足条件的人!\n");
}

// 退出管理系统
void goodbye()
{
	system("cls");
	printf("欢迎下次使用学生信息管理系统!");
	exit(0);// 结束程序
}

综合实验五 :万年历系统

1、要求

  1. 日期对星期的转换。
  2. 输出指定日期N天后的日期。
  3. 输出当前日期。
  4. 输出指定日期的节假日。
  5. 判断指定年是否是闰年还是平年。

2、代码

#include<stdio.h>
#include<windows.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
int year, month, day;

int d[2][13] = {{0,31,28,31,30,31,30,31,31,30,31,30,31},{0,31,29,31,30,31,30,31,31,30,31,30,31} };
int run[12] = { 31,29,31,30,31,30,31,31,30,31,30,31 };
int ping[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
char weekday[7][10] = { "星期一","星期二","星期三","星期四","星期五","星期六","星期日" };

// 表头
void welcome();
// 打印星号
void print_star();

// 平闰年判断
int leap(int year);
// 计算输入的 year 与 1900年有多少天
int day_sum(int year, int month, int day, int run[], int ping[]);
// 打印日历
void print(int sum,int year,int month);

// 日期转换
void week_day();
// 判断星期几
int caculateWeekDay(int year, int month, int day);

// N天后日期
void ndays();

// 当前日期
void time_now();

// 求节假日
void fest();
int festival(int year1, int month1, int day1, int iWeek);

// 判断平闰
void pingrun();

// 退出万年历系统
void goodbye();

int main()
{
	int choice = 0;
	printf("请输入当前日期(年、月、日):");
	scanf("%d %d %d", &year, &month, &day);

	while (1)
	{
		welcome();
		int sum = day_sum(year, month, day, run, ping);
		print(sum, year, month);

		printf("请选择想要实现的功能(数字):");
		scanf("%d", &choice);

		switch (choice)
		{
		case 1:// 日期转换
			week_day();
			break;
		case 2:// N天后日期
			ndays();
			break;
		case 3:// 当前日期
			time_now();
			break;
		case 4:// 求节假日
			fest();
			break;
		case 5:// 判断平闰
			pingrun();
			break;
		case 6:// 退出系统
			goodbye();
			break;
		}
		printf("是否需要继续操作?(Yes:1 / No:0):");
		scanf("%d", &choice);
		if (choice == 0)
		{
			break;
		}
	}
	goodbye();
	return 0;
}

// 表头
void welcome()
{
	system("cls");
	print_star();
	printf("***********                 万年历系统                  ***********\n");
	printf("***********             1 ---- 日期转换                 ***********\n");
	printf("***********             2 ---- N天后日期                ***********\n");
	printf("***********             3 ---- 当前日期                 ***********\n");
	printf("***********             4 ---- 求节假日                 ***********\n");
	printf("***********             5 ---- 判断平闰                 ***********\n");
	printf("***********             6 ---- 退出系统                 ***********\n");
	print_star();
	print_star();
	for (int i = 0; i < 7; i++)
		printf("\t%s", weekday[i]);
	printf("\n");
}

// 打印星号
void print_star()
{
	printf("*******************************************************************\n");
}

// 平闰年判断
int leap(int year)
{
	int four, hundred, fourhundred;
	four = year % 4;
	hundred = year % 100;
	fourhundred = year % 400;

	if (four == 0 && hundred == 0 && fourhundred == 0)
	{
		return 1;
	}
	else if (four == 0 && hundred != 0 && fourhundred != 0)
	{
		return 1;
	}
	return 0;
}

// 计算输入的 year 与 1900年有多少天
int day_sum(int year, int month, int day, int run[], int ping[])
{
	// 1900 1 1 :星期一
	int sum = 0;
	for (int i = 1900; i <= year + 1; i++)
	{
		int flag = leap(year);// 判断平闰年
		if (flag == 1)
		{
			sum += 366;
		}
		else
		{
			sum += 365;
		}
	}
	// 加上月份的天数
	for (int i = 0; i < month - 1; i++)
	{
		if (leap(year) == 1)
		{
			sum += run[i];
		}
		else
		{
			sum += ping[i];
		}
	}
	return sum;
}

// 打印日历
void print(int sum, int year, int month)
{
	int result, temp, i;
	result = sum % 7;// 计算前面空余的天数

	for (i = 0; i < result; i++)
	{
		printf("\t");
	}
	temp = 7 - result;// 从第几格开始打印1号
	if (leap(year) == 1)
	{
		for (i = 1; i <= run[month - 1]; i++)// 打印31天
		{
			printf("\t%d", i);
			if (i == temp || (i - temp) % 7 == 0)
				printf("\n");
		}
		printf("\n");
	}
	else
	{
		for (i = 1; i <= ping[month - 1]; i++)// 打印30天
		{
			printf("\t%d", i);
			if (i == temp || (i - temp) % 7 == 0)
				printf("\n");
		}
		printf("\n");
	}
	print_star();
}

// 日期转换
void week_day()
{
	system("cls");
	int year1, month1, day1, iWeek;
	printf("请输入需要转换的年月日:");
	scanf("%d %d %d", &year1, &month1, &day1);
	iWeek = caculateWeekDay(year1, month1, day1);
	switch (iWeek)//iWeek获得返回值
	{
	case 0: printf("%4d年%2d月%2d日是星期一\n", year1, month1, day1); break;
	case 1: printf("%4d年%2d月%2d日是星期二\n", year1, month1, day1); break;
	case 2: printf("%4d年%2d月%2d日是星期三\n", year1, month1, day1); break;
	case 3: printf("%4d年%2d月%2d日是星期四\n", year1, month1, day1); break;
	case 4: printf("%4d年%2d月%2d日是星期五\n", year1, month1, day1); break;
	case 5: printf("%4d年%2d月%2d日是星期六\n", year1, month1, day1); break;
	case 6: printf("%4d年%2d月%2d日是星期日\n", year1, month1, day1); break;
	}
}

// 判断星期几
int caculateWeekDay(int year, int month, int day)
{
	int iWeek;// 基姆拉尔森计算公式W= (d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7
	if (month == 1 || month == 2) 
	{
		// 1月和2月看作上一年13月和14月
		month += 12;
		year--;
	}
	return iWeek = (day + 2 * month + 3 * (month + 1) / 5 + year + year / 4 - year / 100 + year / 400) % 7;
}

// N天后日期
void ndays()
{
	system("cls");
	int year1 = year, month1 = month, day1 = day, n, k;
	printf("请输入天数:");
	scanf("%d", &n);
	day1 += n;
	k = month;
	for (; day1 > d[leap(year1)][k]; k++)
	{
		day1 -= d[leap(year1)][k];
		if (k == 12)
			k = 0, year1++;
	}
	month1 = k;
	printf("N天后为:%04d年%02d月%02d日\n", year1, month1, day1);
}

// 当前日期
void time_now()
{
	system("cls");
	printf("现在是%4d年%2d月%2d日!\n", year, month, day);
}

// 求节假日
void fest()
{
	system("cls");
	int year1, month1, day1, iWeek;
	printf("请输入要查询的日期:");
	scanf("%d %d %d", &year1, &month1, &day1);
	iWeek = caculateWeekDay(year1, month1, day1);
	if (festival(year1, month1, day1, iWeek))
		printf("%4d年%2d月%2d日是节假日!\n", year1, month1, day1);
	else
		printf("%4d年%2d月%2d日不是节假日!\n", year1, month1, day1);
}
int festival(int year1, int month1, int day1, int iWeek)
{
	if (month1 == 1)
	{
		if (day1 <= 3)
			return 1;
	}
	if (month1 == 4)
	{
		if (day1 >= 3 && day1 <= 5)
			return 1;
	}
	if (month1 == 5)
	{
		if (day1 <= 5)
			return 1;
	}
	if (month1 == 6)
	{
		if (day1 >= 3 && day1 <= 5)
			return 1;
	}
	if (month1 == 9)
	{
		if (day1 >= 10 && day1 <= 12)
			return 1;
	}
	if (month1 == 10)
	{
		if (day1 <= 7)
			return 1;
	}
	if (iWeek >= 5)
		return 1;
	return 0;
}

// 判断平闰
void pingrun()
{
	system("cls");
	int year1;
	printf("请输入要判断的年份:");
	scanf("%d", &year1);
	if (leap(year1) == 1)
		printf("%d为闰年!\n", year1);
	else
		printf("%d为平年!\n", year1);
}

// 退出万年历系统
void goodbye()
{
	printf("欢迎下次使用万年历系统!");
	exit(0);// 结束程序
}

综合实验八:职工信息管理系统

1、要求

职工信息包括职工号、姓名、性别、年龄、学历、职务、工资、住址、电话等(职工号不重复)。试设计一职工信息管理系统,使之能提供以下功能:

  • 职工信息录入功能(职工信息用文件保存)
  • 职工信息浏览功能
  • 查询或排序功能:(至少一种查询方式)
    (1)按工资查询
    (2)按学历查询等
  • 职工信息删除、修改功能

2、代码

#include<stdio.h>
#include<windows.h>
#include<stdlib.h>
#include<string.h>
typedef struct Node
{
	int ID;// 工号
	char Name[50];// 姓名
	char Sex[10];// 性别
	int Age;// 年龄
	char Education[20];// 学历
	char Post[20];// 职务
	int Wage;// 工资
	char Room[50];// 住址
	char Telephone[20];// 电话

	struct Node* next;// 指针域
}node;

node list;// 链表

// 读取文件
int Read_FILE(node* L);

// 保存文件
int  Save_FILE(node* L);

// 主菜单界面
void welcome();

// 增加职工信息
void Add(node* L, node e);// 功能
void Add_Printf();// 界面

// 删除职工信息
void Delete_Printf(node* L);// 界面
void Delete(node* s);// 功能

// 修改职工信息
void Fix(node* L);

// 查询职工信息
void Search_Printf(node* L);// 界面
node* Search_id(int id, node* L);// 按工号进行查找
node* Search_name(char name[], node* L);// 按姓名进行查找

// 输出职工信息
void Print(node* L);// 功能
void Print_Printf();// 界面

// 排序职工信息
void Sort(node* L);
bool cmp_big_Wage(node e1, node e2);// 工资从大到小
bool cmp_small_Wage(node e1, node e2);// 工资从小到大

// 退出管理系统
void goodbye();

int main()
{
	int choice = 0;
	Read_FILE(&list);
	while (true)
	{
		welcome();
		scanf("%d", &choice);
		switch (choice)
		{
		case 1:// 增加职工信息
			Add_Printf();
			break;
		case 2:// 删除职工信息
			Delete_Printf(&list);
			break;
		case 3:// 修改职工信息
			Fix(&list);
			break;
		case 4:// 查询职工信息
			Search_Printf(&list);
			break;
		case 5:// 输出职工信息
			Print(&list);
			break;
		case 6:// 排序职工信息
			Sort(&list);
			break;
		case 0:// 退出职工系统
			goodbye();
			break;
		}
		printf("是否需要继续操作?(Yes:1 / No:0):");
		scanf("%d", &choice);
		if (choice == 0)
		{
			break;
		}
	}
	return 0;
}

void welcome()
{
	system("cls");
	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");

	printf("请选择想要实现的功能(数字):");
}

// 读取文件
int Read_FILE(node* L)
{
	FILE* pfRead = fopen("worker_information.txt", "r");
	node st;
	node* s;
	node* t = L;

	if (pfRead == NULL)
	{
		return 0;
	}

	while (fscanf(pfRead, "%d %s %s %d %s %s %d %s %s", &st.ID, st.Name, st.Sex, &st.Age, st.Education, st.Post, &st.Wage, st.Room, st.Telephone) != EOF)
	{
		s = (node*)malloc(sizeof(node));

		*s = st;

		// 尾插法
		t->next = s;
		t = s;
		t->next = NULL;
	}

	return 1;
}

// 保存文件
int  Save_FILE(node* L)
{
	FILE* pfWrite = fopen("worker_information.txt", "w");
	if (pfWrite == NULL)
	{
		return 0;
	}

	node* p = L->next;

	while (p != NULL)
	{
		fprintf(pfWrite, "%d %s %s %d %s %s %d %s %s\n", p->ID, p->Name, p->Sex, p->Age, p->Education, p->Post, p->Wage, p->Room, p->Telephone);
		p = p->next;
	}

	return 1;
}

// 增加职工信息
void Add_Printf()
{
	system("cls");
	node st;
	printf("请输入新增职工的相关信息:\n");
	printf("工号:");
	scanf("%d", &st.ID);
	printf("姓名:");
	scanf("%s", st.Name);
	printf("性别:");
	scanf("%s", st.Sex);
	printf("年龄:");
	scanf("%d", &st.Age);
	printf("学历:");
	scanf("%s", st.Education);
	printf("职务:");
	scanf("%s", st.Post);
	printf("工资:");
	scanf("%d", &st.Wage);
	printf("住址:");
	scanf("%s", st.Room);
	printf("电话:");
	scanf("%s", &st.Telephone);

	Add(&list, st);
}

void Add(node* L, node e)
{
	// 头插法
	node* p = L;
	node* s = (node*)malloc(sizeof(node));
	*s = e;

	s->next = p->next;
	p->next = s;

	Save_FILE(L);
}

// 删除职工信息
void Delete_Printf(node* L)
{
	system("cls");
	int id;

	node* p;

	printf("请输入要删除的职工的工号:");
	scanf("%d", &id);
	node* st = Search_id(id, L);
	p = st;

	if (st == NULL)
	{
		printf("查无此人!\n");
		return;
	}

	st = st->next;
	printf("________________________________________________________________________________________________\n");
	printf("|学号\t|姓名\t|性别\t|年龄\t|学历\t|职位\t|工资\t|住址\t\t\t|电话\t\t|\n");
	printf("________________________________________________________________________________________________\n");
	printf("|%d\t|%s\t|%s\t|%d\t|%s\t|%s\t|%d\t|%s\t\t|%s\t|\n", st->ID, st->Name, st->Sex, st->Age, st->Education, st->Post, st->Wage, st->Room, st->Telephone);
	printf("________________________________________________________________________________________________\n");

	Delete(p);
	// 保存信息
	Save_FILE(L);
}

void Delete(node* s)
{
	node* t = s->next;

	s->next = t->next;
	t->next = NULL;

	free(t);
}

// 修改职工信息
void Fix(node* L)
{
	system("cls");
	int id;
	printf("请输入要修改的职工的工号:");
	scanf("%d", &id);
	node* st = Search_id(id, L);

	if (st == NULL)
	{
		printf("查无此人!\n");
		return;
	}

	st = st->next;

	int choice = 0;
	while (1)
	{
		system("cls");

		// 输出一次所要修改的职工信息
		printf("________________________________________________________________________________________________\n");
		printf("|学号\t|姓名\t|性别\t|年龄\t|学历\t|职位\t|工资\t|住址\t\t\t|电话\t\t|\n");
		printf("________________________________________________________________________________________________\n");
		printf("|%d\t|%s\t|%s\t|%d\t|%s\t|%s\t|%d\t|%s\t\t|%s\t|\n", st->ID, st->Name, st->Sex, st->Age, st->Education, st->Post, st->Wage, st->Room, st->Telephone);
		printf("________________________________________________________________________________________________\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("请输入要修改的信息:");
		scanf("%d", &choice);

		switch (choice)
		{
		case 1:
			printf("请输入姓名:");
			scanf("%s", st->Name);
			break;
		case 2:
			printf("请输入性别:");
			scanf("%s", st->Sex);
			break;
		case 3:
			printf("请输入年龄:");
			scanf("%d", st->Age);
			break;
		case 4:
			printf("请输入学历:");
			scanf("%s", st->Education);
			break;
		case 5:
			printf("请输入职务:");
			scanf("%s", st->Post);
			break;
		case 6:
			printf("请输入工资:");
			scanf("%d", st->Wage);
			break;
		case 7:
			printf("请输入住址:");
			scanf("%s", st->Room);
			break;
		case 8:
			printf("请输入电话:");
			scanf("%s", st->Telephone);
			break;
		}
		printf("是否继续修改该学生信息?(Yes:1 / No:0):");
		scanf("%d", &choice);
		if (choice == 0)
		{
			break;
		}
	}

	// 修改完成后该学生的信息
	printf("________________________________________________________________________________________________\n");
	printf("|学号\t|姓名\t|性别\t|年龄\t|学历\t|职位\t|工资\t|住址\t\t\t|电话\t\t|\n");
	printf("________________________________________________________________________________________________\n");
	printf("|%d\t|%s\t|%s\t|%d\t|%s\t|%s\t|%d\t|%s\t\t|%s\t|\n", st->ID, st->Name, st->Sex, st->Age, st->Education, st->Post, st->Wage, st->Room, st->Telephone);
	printf("________________________________________________________________________________________________\n");

	// 保存信息
	Save_FILE(L);
}

// 查询职工信息
void Search_Printf(node* L)
{
	system("cls");
	int choice = 0;
	printf("按照工号查询 ---- 1\n");
	printf("按照姓名查询 ---- 2\n");
	printf("请输入查询方式:");
	scanf("%d", &choice);

	int id;
	char name[50];
	node* st;
	if (choice == 1)
	{
		printf("请输入要查询的工号:");
		scanf("%d", &id);
		st = Search_id(id, L);

		if (st == NULL)
		{
			printf("查无此人!\n");
		}
		else
		{
			st = st->next;
			printf("________________________________________________________________________________________________\n");
			printf("|学号\t|姓名\t|性别\t|年龄\t|学历\t|职位\t|工资\t|住址\t\t\t|电话\t\t|\n");
			printf("________________________________________________________________________________________________\n");
			printf("|%d\t|%s\t|%s\t|%d\t|%s\t|%s\t|%d\t|%s\t\t|%s\t|\n", st->ID, st->Name, st->Sex, st->Age, st->Education, st->Post, st->Wage, st->Room, st->Telephone);
			printf("________________________________________________________________________________________________\n");
		}
	}
	else if (choice == 2)
	{
		printf("请输入要查询的姓名:");
		scanf("%s", name);
		st = Search_name(name, L);

		if (st == NULL)
		{
			printf("查无此人!\n");
		}
		else
		{
			st = st->next;
			printf("________________________________________________________________________________________________\n");
			printf("|学号\t|姓名\t|性别\t|年龄\t|学历\t|职位\t|工资\t|住址\t\t\t|电话\t\t|\n");
			printf("________________________________________________________________________________________________\n");
			printf("|%d\t|%s\t|%s\t|%d\t|%s\t|%s\t|%d\t|%s\t\t|%s\t|\n", st->ID, st->Name, st->Sex, st->Age, st->Education, st->Post, st->Wage, st->Room, st->Telephone);
			printf("________________________________________________________________________________________________\n");
		}
	}
}

// 按学号进行查找
node* Search_id(int id, node* L)
{
	node* p = L;

	while (p->next != NULL)
	{
		if (p->next->ID == id)
		{
			return p;
		}
		p = p->next;
	}
	return NULL;
}

// 按姓名进行查找
node* Search_name(char name[], node* L)
{
	node* p = L;

	while (p->next != NULL)
	{
		if (strcmp(name, p->next->Name) == 0)
		{
			return p;
		}
		p = p->next;
	}
	return NULL;
}

// 输出职工信息
void Print(node* L)
{
	system("cls");
	node* p = L->next;
	Print_Printf();
	if (p != NULL)
	{
		while (p != NULL)
		{
			printf("________________________________________________________________________________________________\n");
			printf("|%d\t|%s\t|%s\t|%d\t|%s\t|%s\t|%d\t|%s\t\t|%s\t|\n", p->ID, p->Name, p->Sex, p->Age, p->Education, p->Post, p->Wage, p->Room, p->Telephone);
			printf("________________________________________________________________________________________________\n");
			p = p->next;
		}
	}
}

void Print_Printf()
{
	printf("________________________________________________________________________________________________\n");
	printf("|学号\t|姓名\t|性别\t|年龄\t|学历\t|职位\t|工资\t|住址\t\t\t|电话\t\t|\n");
	printf("________________________________________________________________________________________________\n");
}

// 排序职工信息
void Sort(node* L)
{
	system("cls");
	int choice = 0;
	printf("按照工资从大到小排序 ---- 1\n");
	printf("按照工资从小到大排序 ---- 2\n");
	printf("请选择排序方式:");
	scanf("%d", &choice);

	int flag = 0;
	for (node* p = L->next; p != NULL; p = p->next)
	{
		for (node* q = p; q != NULL; q = q->next)
		{
			switch (choice)
			{
			case 1:
				if (!cmp_big_Wage(*p, *q))
				{
					flag = 1;
				}
				break;
			case 2:
				if (!cmp_small_Wage(*p, *q))
				{
					flag = 1;
				}
				break;
				if (flag == 1)
				{
					// 交换数据域
					node t = *p;
					*p = *q;
					*q = t;
					// 处理指针域
					t.next = p->next;
					p->next = q->next;
					q->next = t.next;
					flag = 0;
				}
			}
		}
	}
}

// 工资从大到小
bool cmp_big_Wage(node e1, node e2)
{
	return e1.Wage > e2.Wage;
}
// 工资从小到大
bool cmp_small_Wage(node e1, node e2)
{
	return e1.Wage < e2.Wage;
}

// 退出管理系统
void goodbye()
{
	system("cls");
	printf("欢迎下次使用职工信息管理系统!");
	exit(0);// 结束程序
}
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

WE-ubytt

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值