c语言垃圾分类系统

#C语言简单的垃圾分类查询系统

目的:(1)通过实践提高C语言的开发能力。(2)通过充分编程实践和实际应用,进一步体会数据结构中的常用算法的实质,建立算法优劣的概念以及算法评估分析和比较的方法。(3)培养学生科技论文写作技能,如文献查找和引用,论文格式规范等。

课程设计内容:
基于C语言的简单垃圾分类查询系统,对垃圾进行分类管理,包括新增、删除、修改、查询、浏览等功能。
(1)新增:采用交互式方式录入垃圾信息,保存在文件中,之后可导入;
(2)删除:从垃圾信息管理文件中删除一个垃圾对象;
(3)修改:检索某个垃圾对象,对其某些属性进行修改;
(4)查找:从垃圾信息管理文件中查询符合某些条件的垃圾信息;
(5)浏览:从垃圾信息管理文件中按垃圾类别排列好,再浏览。

#以下是函数关系流程图和运行截图:(运行环境:Visual Studio 2019)
1.各函数层次关系图
c语言垃圾分类系统_第1张图片
2.add
c语言垃圾分类系统_第2张图片c语言垃圾分类系统_第3张图片
3.delete
c语言垃圾分类系统_第4张图片c语言垃圾分类系统_第5张图片
4.update
c语言垃圾分类系统_第6张图片c语言垃圾分类系统_第7张图片
5.select
c语言垃圾分类系统_第8张图片c语言垃圾分类系统_第9张图片
6.browse
c语言垃圾分类系统_第10张图片c语言垃圾分类系统_第11张图片
#以下贴上我的代码:
(在该项目相同目录下新建一个文本文件:garbage.txt 即可运行)

#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include

//垃圾的内部结构
typedef struct _Garbage
{
	char garbageName[20];//垃圾名称
	char garbageType[20];//垃圾类型 1.厨余垃圾 2.可回收垃圾 3.有毒有害垃圾 4.不可回收垃圾
	char garbageDescribe[20];//垃圾描述
}Garbage;

//垃圾双向循环链表节点
typedef struct _GarbageNode
{
	Garbage garbage;//数据域放整个垃圾
	struct _GarbageNode* prior;//指针域指向前一个 垃圾节点
	struct _GarbageNode* next;//指针域指向下一个 垃圾节点
}GarbageNode;

//遍历链表并打印出原始的垃圾表格
void readLink(GarbageNode* g_head, GarbageNode* tail)
{
	GarbageNode* p;
	p = g_head->next;
	int i = 1;
	printf("\n\t垃圾表:\n");
	printf("\t-----------------------------------------------------------------\n");
	printf("\t|编号 \t|名字\t\t|类型\t\t\t|描述\t\t\n");
	printf("\t-----------------------------------------------------------------\n");
	while (p != g_head)
	{
		printf("\t| %d\t|%s\t\t|%s\t\t|%s\t\t\n",
			i,
			p->garbage.garbageName,
			p->garbage.garbageType,
			p->garbage.garbageDescribe);
		p = p->next;
		i++;
	}
	printf("\t-----------------------------------------------------------------");
	printf("\n");
}

//功能函数一:新增垃圾 尾插法
int add(GarbageNode* g_head, GarbageNode* tail)
{
	int reback;
	int num;//选择垃圾类型1-4
	FILE* fp;
	char a[20];//1 厨余垃圾
	char b[20];//2 可回收垃圾
	char c[20];//3 有毒有害垃圾
	char d[20];//4 不可回收垃圾
	char getName[20];
reAdd:
	printf("\n");
	printf("\t| 请输入想插入的垃圾名称:");
	scanf("%s",getName);
	GarbageNode* p;
	p = g_head->next;
	while (0 != strcmp(getName, p->garbage.garbageName) && p != g_head)//C 标准库 - 
	{
		p = p->next;
	}
	if (0 == strcmp(getName, p->garbage.garbageName))
	{
		printf("\t| 该垃圾已添加过了,请重新输入");
		goto reAdd;
	}

	GarbageNode* gNewNode = (GarbageNode*)malloc(sizeof(GarbageNode));//生成新节点
	strcpy(gNewNode->garbage.garbageName, getName);// C 标准库 - 
	printf("\n");
	printf("\t|-------------------------------------------------\n");
	printf("\t|1 厨余垃圾\t\t2 可回收垃圾\t\t|\n\n");
	printf("\t|3 有毒有害垃圾\t\t4 不可回收垃圾\t\t|\n");
	printf("\t|-------------------------------------------------\n");
	printf("\t| 请选择该垃圾类型: ");
	scanf("%d", &num);
	switch (num)
	{
	case 1:
		strcpy(a, "厨余垃圾");
		strcpy(gNewNode->garbage.garbageType, a);
		break;
	case 2:
		strcpy(b, "可回收垃圾");
		strcpy(gNewNode->garbage.garbageType, b);
		break;
	case 3:
		strcpy(c, "有毒有害垃圾");
		strcpy(gNewNode->garbage.garbageType, c);
		break;
	case 4:
		strcpy(d, "不可回收垃圾");
		strcpy(gNewNode->garbage.garbageType, d);
		break;
	}
	printf("\n");
	printf("\t| 请输入该垃圾描述:");
	scanf("%s", gNewNode->garbage.garbageDescribe);

	tail->next = gNewNode;//让前一个节点和新节点连接
	gNewNode->prior = tail;// 新节点的前驱指针指向前一个
	tail = gNewNode;//tail指向新节点,即最后一个节点
	g_head->prior = tail;
	tail->next = g_head;

	fp = fopen("garbage.txt", "at");//C 标准库 - 
	if (fp == NULL)
	{
		printf("找不到文件");
		exit(0);
	}
	fprintf(fp, "%s\t\t%s\t\t%s\n", 
		gNewNode->garbage.garbageName, 
		gNewNode->garbage.garbageType, 
		gNewNode->garbage.garbageDescribe);//按格式追加数据在文档
	printf("\n");
	printf("\t|添加成功,您添加的垃圾是:\n\n");
	printf("\t|名字: %s\n", gNewNode->garbage.garbageName);
	printf("\t|类型: %s\n", gNewNode->garbage.garbageType);
	printf("\t|描述: %s \n",gNewNode->garbage.garbageDescribe);
	printf("\n");
	fclose(fp);
	system("pause");
	printf("\t|按 1 返回主菜单\n\t|按其他键退出系统\n");
	printf("\t|请选择: ");
	scanf("%d", &reback);
	if (reback == 1)
	{
		system("cls");
		return reback;
	}
	else
	{
		exit(0);
	}
}

//功能函数二:根据垃圾名删除
int delet(GarbageNode* g_head, GarbageNode* tail)
{
	int reback;
	FILE* fp;
	readLink(g_head, tail);
reDelete:
	if (g_head->next == g_head && g_head->prior == g_head)
	{
		printf("\n\t|目前垃圾表为空\n");
	}
	else
	{ 
		printf("\t|请选择要删除的垃圾名称:");
		char getName[20];
		int ch;//1 确定删除\t  2 取消并重新选择删除的垃圾
		scanf("%s", getName);
		GarbageNode* del = g_head->next;
		GarbageNode* reWrite;
		while (del != g_head)
		{
			if (0 != strcmp(getName, del->garbage.garbageName))
			{
				del = del->next; //不相等就找下一个
			}
			else
			{
				printf("\n");
				printf("\t|要删除的是:\n\n");
				printf("\t|名字: %s\n", del->garbage.garbageName);
				printf("\t|类型: %s\n", del->garbage.garbageType);
				printf("\t|描述: %s \n", del->garbage.garbageDescribe);
				printf("\n");
				printf("\t| 1 确定删除\n\t| 2 取消并重新选择删除的垃圾\n");
				printf("\t|请选择:");
				scanf("%d", &ch);

				if (ch == 1)
				{
					del->prior->next = del->next;
					del->next->prior = del->prior;
					free(del);
					fp = fopen("garbage.txt", "w");//删除完重新写入文件
					if (fp == NULL)
					{
						printf("\t|找不到文件");
						exit(0);
					}
					else
					{
						reWrite = g_head->next;
						while (reWrite != g_head)
						{
							fprintf(fp, "%s\t\t%s\t\t%s\n",
								reWrite->garbage.garbageName,
								reWrite->garbage.garbageType,
								reWrite->garbage.garbageDescribe);//按格式覆盖数据在文档
							reWrite = reWrite->next;
						}
						fclose(fp);
						printf("\n\t|删除完毕后的结果:");
						readLink(g_head, tail);
						break;
					}

				}

				else if (ch == 2)
				{
					goto reDelete;
				}
				else
				{
					printf("\t|输入错误,请重新选择删除");
					goto reDelete;
				}

			}
		}//while 结束

		if (del == g_head)
		{
			int num;
			printf("\t|无此垃圾信息\n");
			printf("\t|重新删除请按 1 , 其他键返回主菜单: ");
			scanf("%d", &num);
			if (num == 1)
			{
				goto reDelete;
			}
		}
	}
	system("pause");
	printf("\t|按 1 返回主菜单\n\t|按其他键退出系统\n");
	printf("\t|请选择: ");
	scanf("%d", &reback);
	if (reback ==  1)
	{
		system("cls");
		return reback;
	}
	else
	{
		exit(0);
	}
}

//功能函数三:根据垃圾名修改
int update(GarbageNode* g_head, GarbageNode* tail)
{
	int reback;
	FILE* fp;
	int num;
	char a[20];//1 厨余垃圾
	char b[20];//2 可回收垃圾
	char c[20];//3 有毒有害垃圾
	char d[20];//4 不可回收垃圾
	readLink(g_head, tail);
reUpdate:
	if (g_head->next == g_head && g_head->prior == g_head)
	{
		printf("\n\t|目前垃圾表为空\n");
	}
	else
	{
		printf("\t|请选择要修改的垃圾名称:");
		char getName[20];
		int ch;//1 确定修改  2 取消并重新选择修改的垃圾
		scanf("%s", getName);
		GarbageNode* upd = g_head->next;
		GarbageNode* reWrite = g_head->next;
		while (upd != g_head)
		{
			if (0 != strcmp(getName, upd->garbage.garbageName))
			{
				upd = upd->next; //不相等就找下一个
			}
			else
			{
				printf("\n");
				printf("\t|要修改的是:\n");
				printf("\t|名字: %s\n", upd->garbage.garbageName);
				printf("\t|类型: %s\n", upd->garbage.garbageType);
				printf("\t|描述: %s \n", upd->garbage.garbageDescribe);
				printf("\n");
				printf("\t| 1 确定要修改垃圾信息\n\t| 2 取消并重新选择修改的垃圾\n");
				printf("\t| 请选择:");

				scanf("%d", &ch);
				if (ch == 1)
				{
					printf("\t|请输入修改成的垃圾名字:");
					scanf("%s", upd->garbage.garbageName);
					printf("\n");
					printf("\t|请选择要修改成的垃圾类型:");
					printf("\n");
					printf("\t|-------------------------------------------------\n");
					printf("\t|1 厨余垃圾\t\t2 可回收垃圾\t\t|\n");
					printf("\t|3 有毒有害垃圾\t\t4 不可回收垃圾\t\t|\n");
					printf("\t|-------------------------------------------------\n");
					printf("\t| 请选择该垃圾类型: ");
					scanf("%d", &num);
					switch (num)
					{
					case 1:
						strcpy(a, "厨余垃圾");
						strcpy(upd->garbage.garbageType, a);
						break;
					case 2:
						strcpy(b, "可回收垃圾");
						strcpy(upd->garbage.garbageType, b);
						break;
					case 3:
						strcpy(c, "有毒有害垃圾");
						strcpy(upd->garbage.garbageType, c);
						break;
					case 4:
						strcpy(d, "不可回收垃圾");
						strcpy(upd->garbage.garbageType, d);
						break;
					}
					printf("\n");
					printf("\t|请输入修改成的垃圾描述:");
					scanf("%s", upd->garbage.garbageDescribe);

					fp = fopen("garbage.txt", "w");
					if (fp == NULL)
					{
						printf("找不到文件");
						exit(0);
					}
					else
					{
						while (reWrite != g_head)
						{
							fprintf(fp, "%s\t\t%s\t\t%s\n",
								reWrite->garbage.garbageName,
								reWrite->garbage.garbageType,
								reWrite->garbage.garbageDescribe);
							reWrite = reWrite->next;
						}
						fclose(fp);
						printf("\t|修改完毕后的结果:\n");
						readLink(g_head, tail);
						break;
					}
				}
				else if (ch == 2)
				{
					goto reUpdate;
				}
				else
				{
					printf("\t|输入错误,请重新选择修改: ");
					goto reUpdate;
				}
			}
		}//while 结束

		if (upd == g_head)
		{
			int reupdate;
			printf("\t|无此垃圾信息\n");
			printf("\t|重新修改请按 1 , 其他键返回主菜单: ");
			scanf("%d", &reupdate);
			if (reupdate == 1)
			{
				goto reUpdate;
			}
		}
	}
	system("pause");
	printf("\t|按 1 返回主菜单\n\t|按其他键退出系统\n");
	printf("\t|请选择: ");
	scanf("%d", &reback);
	if (reback == 1)
	{
		system("cls");
		return reback;
	}
	else
	{
		exit(0);
	}
}

//功能函数四:分别根据垃圾名,垃圾类别(一对多),垃圾描述查找
int select(GarbageNode* g_head, GarbageNode* tail)
{
	int reback;
	int ch;
	int i = 0;//垃圾個數
	char getMassage[20];
	GarbageNode* p;
	readLink(g_head, tail);//调用函数读取文件内容

reSelect:
	if (g_head->next == g_head && g_head->prior == g_head)
	{
		printf("\n\t|目前垃圾表为空\n");
	}
	else
	{
		printf("\t| 请选择您要查询的方式\n\n");
		printf("\t| 1.按名称查 ******** \n");
		printf("\t| 2.按类型查 ******** \n");
		printf("\t| 3.按描述查 ******** \n");
		printf("\t|请选择:");
		scanf("%d", &ch);
		p = g_head->next;
		switch (ch)
		{
		case 1:
			printf("\t|请输入想查询的垃圾名字:");
			scanf("%s", getMassage);
			i = 0;
			while (p != g_head)
			{
				if (0 != strcmp(getMassage, p->garbage.garbageName))
				{
					p = p->next; //不相等就找下一个
				}
				else
				{
					printf("\n");
					printf("\t|查找成功,您查找的垃圾信息如下:\n\n");
					printf("\t|名字: %s\n", p->garbage.garbageName);
					printf("\t|类型: %s\n", p->garbage.garbageType);
					printf("\t|描述: %s \n", p->garbage.garbageDescribe);
					printf("\n");
					i++;
					break;
				}
			}
			break;

		case 2:
			printf("\t|请输入想查询的垃圾类型:");
			scanf("%s", getMassage);
			i = 0;
			while (p != g_head)
			{	
				if (0 == strcmp(getMassage, p->garbage.garbageType))
				{
					i++;
					printf("\t|----------------------------------------------------------------\n");
					printf("\t|%d 名字: %s\t 类型: %s\t描述: %s\n",
						i, p->garbage.garbageName, p->garbage.garbageType,
						p->garbage.garbageDescribe);
				}
				p = p->next; //相等也找下一个	
			}
			printf("\t|----------------------------------------------------------------\n");
			break;

		case 3:
			printf("\t|请输入想查询的垃圾描述:");
			scanf("%s", getMassage);
			i = 0;
			while (p != g_head)
			{
				if (0 != strcmp(getMassage, p->garbage.garbageDescribe))
				{
					p = p->next; //不相等就找下一个
				}
				else
				{
					printf("\n");
					printf("\t|查找成功,您查找的垃圾信息如下:\n\n");
					printf("\t|名字: %s\n", p->garbage.garbageName);
					printf("\t|类型: %s\n", p->garbage.garbageType);
					printf("\t|描述: %s \n", p->garbage.garbageDescribe);
					printf("\n");
					i++;
					break;
				}
			}
			break;
		}
		if (p == g_head && i==0)
		{
			printf("\t|无此垃圾信息\n");	
		}
		int reselect;
		printf("\t|重新查找请按 1 , 其他键返回主菜单: ");
		scanf("%d", &reselect);
		if (reselect == 1)
		{
			goto reSelect;
		}
	}
	system("pause");
	printf("\t|按 1 返回主菜单\n\t|按其他键退出系统\n");
	printf("\t|请选择: ");
	scanf("%d", &reback);
	if (reback == 1)
	{
		system("cls");
		return reback;
	}
	else
	{
		exit(0);
	}

}

//功能函数五:按分类好的4类垃圾浏览,也可浏览排列好的总垃圾表
int browse(GarbageNode* g_head, GarbageNode* tail)
{
	int reback;
	int num;
	int rebrowse;
	GarbageNode* p;
	int i;
	char getTypeOne[20] = "厨余垃圾";//得到第一个垃圾类型
	char getTypeTow[20] = "可回收垃圾";//得到第二个垃圾类型
	char getTypeThree[20] = "有毒有害垃圾";//得到第三个垃圾类型
	char getTypeFour[20] = "不可回收垃圾";//得到第四个垃圾类型
	printf("\n");
	printf("\t|------------------------------------------------\n");
	printf("\t|1 厨余垃圾表\t\t2 可回收垃圾表\t\t|\n\n");
	printf("\t|3 有毒有害垃圾表\t4 不可回收垃圾表\t|\n\n");
	printf("\t|5 按以上四种垃圾排列好的总垃圾表\t\t|\n");
	printf("\t|------------------------------------------------\n");
reBrowse:
	i = 1;
	printf("\t|请选择想浏览的垃圾表编号: ");
	scanf("%d", &num);
	switch (num)
	{
		case 1:
			p = g_head->next;
			printf("\n\t厨余垃圾表:\n");
			printf("\t--------------------------------------------------\n");
			printf("\t|编号 \t|名字\t\t|描述\t\t\t|\n");
			printf("\t--------------------------------------------------\n");
			while (p != g_head)
			{
				if (0 == strcmp(getTypeOne, p->garbage.garbageType))
				{
					printf("\t| %d\t|%s\t\t|%s\t\t\t|\n",
						i,
						p->garbage.garbageName,
						p->garbage.garbageDescribe);
					i++;
				}	
				p = p->next;
				
			}
			printf("\t--------------------------------------------------");
			printf("\n");
		break;
	
		case 2:
			p = g_head->next;//回到链头
			printf("\n\t可回收垃圾表:\n");
			printf("\t--------------------------------------------------\n");
			printf("\t|编号 \t|名字\t\t|描述\t\t\t|\n");
			printf("\t--------------------------------------------------\n");
			while (p != g_head)
			{
				if (0 == strcmp(getTypeTow, p->garbage.garbageType))
				{
					printf("\t| %d\t|%s\t\t|%s\t\t\t|\n",
						i,
						p->garbage.garbageName,
						p->garbage.garbageDescribe);
					i++;
				}
				p = p->next;
			}
			printf("\t--------------------------------------------------");
			printf("\n");
		break;

		case 3:
			p = g_head->next;
			printf("\n\t有毒有害垃圾表:\n");
			printf("\t--------------------------------------------------\n");
			printf("\t|编号 \t|名字\t\t|描述\t\t\t|\n");
			printf("\t--------------------------------------------------\n");
			while (p != g_head)
			{
				if (0 == strcmp(getTypeThree, p->garbage.garbageType))
				{
					printf("\t| %d\t|%s\t\t|%s\t\t\t\n",
						i,
						p->garbage.garbageName,
						p->garbage.garbageDescribe);
					i++;
				}
				p = p->next;
				
			}
			printf("\t--------------------------------------------------");
			printf("\n");
		break;

		case 4:
			p = g_head->next;
			printf("\n\t不可回收垃圾表:\n");
			printf("\t--------------------------------------------------\n");
			printf("\t|编号 \t|名字\t\t|描述\t\t\t|\n");
			printf("\t--------------------------------------------------\n");
			while (p != g_head)
			{
				if (0 == strcmp(getTypeFour, p->garbage.garbageType))
				{
					printf("\t| %d\t|%s\t\t|%s\t\t\t|\n",
						i,
						p->garbage.garbageName,
						p->garbage.garbageDescribe);
					i++;
				}
				p = p->next;
				
			}
			printf("\t--------------------------------------------------");
			printf("\n");
			break;
		case 5:
			p = g_head->next;//回到链头
			printf("\n\t总垃圾表:\n");
			printf("\t------------------------------------------------------------\n");
			printf("\t|编号 \t|名字\t\t|类型\t\t\t|描述\t\t\n");
			printf("\t------------------------------------------------------------\n");
			while (p != g_head)
			{
				if (0 == strcmp(getTypeOne, p->garbage.garbageType))
				{
					printf("\t| %d\t|%s\t\t|%s\t\t|%s\t\t\n",
						i,
						p->garbage.garbageName,
						p->garbage.garbageType,
						p->garbage.garbageDescribe);
					i++;
				}
				p = p->next;
			}

			p = g_head->next;//回到链头
			while (p != g_head)
			{
				if (0 == strcmp(getTypeTow, p->garbage.garbageType))
				{
					printf("\t| %d\t|%s\t\t|%s\t\t|%s\t\t\n",
						i,
						p->garbage.garbageName,
						p->garbage.garbageType,
						p->garbage.garbageDescribe);
					i++;
				}
				p = p->next;
			}

			p = g_head->next;//回到链头
			while (p != g_head)
			{
				if (0 == strcmp(getTypeThree, p->garbage.garbageType))
				{
					printf("\t| %d\t|%s\t\t|%s\t\t|%s\t\t\n",
						i,
						p->garbage.garbageName,
						p->garbage.garbageType,
						p->garbage.garbageDescribe);
					i++;
				}
				p = p->next;
			}

			p = g_head->next;//回到链头
			while (p != g_head)
			{
				if (0 == strcmp(getTypeFour, p->garbage.garbageType))
				{
					printf("\t| %d\t|%s\t\t|%s\t\t|%s\t\t\n",
						i,
						p->garbage.garbageName,
						p->garbage.garbageType,
						p->garbage.garbageDescribe);
					i++;
				}
				p = p->next;
			}
			printf("\t------------------------------------------------------------");
			printf("\n");
			break;

		default:
			printf("\t|输入不规范,请重新选择\n");
			goto reBrowse;
			break;
	}
	printf("\t|继续浏览其他垃圾表请按 1 , 按其他键将返回主菜单:\n");
	printf("\t|请选择: ");
	scanf("%d", &rebrowse);
	if (rebrowse == 1)
	{
		goto reBrowse;
	}
	else 
	{
		system("pause");
		printf("\t|按 1 返回主菜单\n\t|按其他键退出系统\n");
		printf("\t|请选择: ");
		scanf("%d", &reback);
		if (reback == 1)
		{
			system("cls");
			return reback;
		}
		else
		{
			exit(0);
		}
	}	
}

//主菜单
void menu()
{
	printf("**********************************************************\n");
	printf("*************** 欢迎使用垃圾分类系统 *********************\n");
	printf("**********************************************************\n");
	printf("\t------------------------------------------\n");
	printf("\t|\t1.\t新增\t\t\t|\n");
	printf("\t------------------------------------------\n");
	printf("\t|\t2.\t删除\t\t\t|\n");
	printf("\t------------------------------------------\n");
	printf("\t|\t3.\t修改\t\t\t|\n");
	printf("\t------------------------------------------\n");
	printf("\t|\t4.\t查找\t\t\t|\n");
	printf("\t------------------------------------------\n");
	printf("\t|\t5.\t浏览\t\t\t|\n");
	printf("\t------------------------------------------\n");
	printf("\n");
	printf("\t| 请选择以上操作 ^_^ :  ");

}

int main()
{
//读取garbage.txt,将内容填入链表
	int reback; //reback 为0时不返回主菜单,为1时返回
	FILE* fp;
	GarbageNode* g_head;//头结点
	GarbageNode* gNewNode, * tail;//一个新节点指针,一个始终指向尾部的指针
	g_head = (GarbageNode*)malloc(sizeof(GarbageNode));//创建头结点
	g_head->next = g_head;
	g_head->prior = g_head;
	tail = g_head; // 始终指向尾部的指针

	fp = fopen("garbage.txt", "rt");
	if (fp == NULL)
	{
		printf("找不到文件");
		exit(0);
	}
	else
	{
		gNewNode = (GarbageNode*)malloc(sizeof(GarbageNode));//生成新节点
		fscanf(fp, "%s\t\t%s\t\t%s",
			gNewNode->garbage.garbageName,
			gNewNode->garbage.garbageType,
			gNewNode->garbage.garbageDescribe);//先读取完再判断feof(fp),否则下面的循环会多走一次

		while (!feof(fp))//feof()到文件末尾会返回 真值
		{
			tail->next = gNewNode;//让前一个节点和新节点连接
			gNewNode->prior = tail;// 新节点的前驱指针指向前一个
			tail = gNewNode;//尾指针后移
			tail->next = g_head;
			g_head->prior = tail;

			gNewNode = (GarbageNode*)malloc(sizeof(GarbageNode));//生成新节点
			fscanf(fp, "%s\t%s\t%s",
				gNewNode->garbage.garbageName,
				gNewNode->garbage.garbageType,
				gNewNode->garbage.garbageDescribe);		
		}
		fclose(fp);
	}

//进入主菜单选择需要的操作
	do
	{
		system("color 5F");//改背景顔色
		menu();//主菜單
		int ch;//获取键盘输入
		scanf("%d", &ch);

		switch (ch)
		{
		case 1:
			reback = add(g_head, tail);
			break;

		case 2:
			reback = delet(g_head, tail);
			break;

		case 3:
			reback = update(g_head, tail);
			break;

		case 4:
			reback = select(g_head, tail);
			break;

		case 5:
			reback = browse(g_head, tail);
			break;

		default:
			system("cls");
			printf("**********输入不规范*************\n");
			printf("\n请重新选择\n\n");
			system("pause");
			system("cls");
			reback = 1;
			break;
		}
	} while (reback);
	return 0;
}

5结语
问题回顾:1.起初对项目不知从何下手,对文件交互式不是很了解,但是努力找资料学习并实验,对知识有更深体会和理解,两天后开始了自己的垃圾分类项目。2.在对于系统中函数单独调用后不能返回主菜单而苦恼,后来想到了调用函数后返回参数,再配合while循环即可达到目的,只有不满足条件时才退出系统。3.在写删除操作前是用单向链表的,但是删除必须要回到前一个结点才能连接其前后结点,自己再释放掉,所以又改成了双向链表。
链表的时间复杂度为O(n)。最开始是每个子函数都在堆空间创建一条链,再去读取文件放入结点中,这样很浪费运行时间和堆内存空间。为了不在每次调用子函数时都去文件那里读取完再放进链表,我选择将读取的位置放在main()函数的最开始,然后再将链头链尾传给其他子函数,这样我的系统从头到尾就只有一条链,每次的增删改查操作都在同一块堆空间进行操作,这样文件和链表的变化就能同步起来,只要去遍历链表就相当于遍历了文件,我是用readLink()函数读取修改后堆空间链表。
功能扩展:系统除了最基本的增删改查,我还扩展了浏览的功能。
对数据结构课程的认识:在开始做课程设计之前我复习数据结构,仔细比较线性表,单链表和双向链表的各种不同及其适用的应用场景,更深刻体理解链表的使用极其在堆空间的存在,还有它的生命周期。
这次的课程设计对我的C语言基础帮助很大,而且更是清楚了计算机中一些内存的分配,实践和逻辑也得到质的提升了。

你可能感兴趣的:(C语言)