当前位置:网站首页>食堂用户菜品关系系统(C语言课设)

食堂用户菜品关系系统(C语言课设)

2022-07-06 21:52:00 云琤

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<windows.h>
//用户的相关操作
int Umanu();//用户操作列表
void Uoperate(int function);//用户的指令判定
struct Users* ReadFile(char* filename);//读取之前文件之中的用户数据
struct Users* login(struct Users* Uhead);//创建用户的操作
void reg(struct Users* Uhead);//登录操作
void writeFile(struct Users* head, char* filename);//将用户信息写入文件
void Uchange(struct Users* head);//修改用户密码

void operate();//操作列表
void SearchCook(struct cook* head);//寻找对应菜品,内部调用search和print
struct cook* search(struct cook* head);//小的搜索函数,执行指定搜索
void Write(struct cook* head, char* filename);//写入菜品数据
void Comment(struct cook* head);//评价菜品
void Add(struct cook* head);//在链表末尾加入新的菜品
void Allprint(struct cook* head);//打印链表中的全部元素
void print(struct cook* temp);//打印单个节点,作为内部工具类函数存在
void Change(struct cook* head);//修改菜品内容
struct cook* Read(char* filename);//读出存入的菜品内容

struct Users
{
	char ID[21];//用户的ID
	char passWord[21];//用户的密码
	struct Users* next;//链表操作,指向下一个存储块
};
struct Users* login(struct Users* Uhead)
{
	struct Users* newUser = (struct Users*)malloc(sizeof(struct Users));
	struct Users* temp = Uhead;
	printf("创建您的ID(1-20位):\n");
	scanf("%s", newUser->ID);
	printf("请输入您设置的密码(1-20位) :\n");
	scanf("%s", newUser->passWord);
	if (temp != NULL)
	{
		while (temp->next != NULL)
		{
			temp = temp->next;
		}
		temp->next = newUser;
		temp = temp->next;
	}
	else
	{
		temp = newUser;
		Uhead = temp;
	}
	newUser->next = NULL;
	return Uhead;
}
void reg(struct Users* Uhead)
{
	int a = 0;
	printf("请输入您的ID :\n");
	char UID[20];
	scanf("%s", &UID);
	struct Users* temp = Uhead;
	while (temp != NULL)
	{
		if (strcmp(temp->ID, UID) == 0)
		{
			a = 1;
			break;
		}
		else
		{
			temp = temp->next;
		}
	}
	if (a != 1)
	{
		printf("该ID还未注册!请先注册!\n");
		Sleep(1500);
		int function = Umanu();
		Uoperate(function);
		return;
	}
	char passWord[20];
	int flag = 1;
	while (flag)
	{
		printf("请输入您的密码 :\n");
		scanf("%s", passWord);
		if (strcmp(passWord,temp->passWord)==0)
		{
			flag = 0;
			printf("密码正确 !欢迎使用 !\n");
			Sleep(1000);
			return;
		}
		else
		{
			printf("密码错误 !请30秒后重试 !\n");
			Sleep(30000);
		}
	}
}
struct Users* ReadFile(char* filename)
{
	struct Users* temp = NULL;
	struct Users* Uhead = temp = (struct Users*)malloc(sizeof(struct Users));;
	struct Users* newUser = (struct Users*)malloc(sizeof(struct Users));
	newUser->next = NULL;
	FILE* fp = fopen(filename, "r");
	if (fp == NULL)
	{
		return 0;
	}
	fscanf(fp, "%s\t%s\n", temp->ID, temp->passWord);
	Uhead = temp;
	temp->next = NULL;
	while(fscanf(fp,"%s\t%s\n",newUser->ID,newUser->passWord)!=EOF)
	{
		temp->next = newUser;
		temp = temp->next;
		newUser = (struct Users*)malloc(sizeof(struct Users));
		newUser->next = NULL;
	}
	fclose(fp);
	return Uhead;
}
void destory(struct Users*Uhead)
{
	char UID[21];
	printf("请输入您的ID :\n");
	char chioce[5];
	scanf("%s", UID);
	char passWord[21];
	struct Users* temp = Uhead;
	while (temp->ID == UID)
	{
		temp = temp->next;
	}
	printf("请输入密码 :\n");
	scanf("%s", passWord);
	while (passWord == temp->passWord)
	{
		printf("请输入您的密码 :\n");
		scanf("%d", &passWord);
		if (passWord == temp->passWord)
		{
			int flag = 1;
			printf("密码正确 !确定要销毁账号吗 ?(yes/no)\n");
			while (flag)
			{
				scanf("%s", chioce);
				if (chioce == "yes")
				{
					flag = 0;
					struct Users* n = NULL;
					struct Users* p = Uhead;
					n = temp->next;
					while (p->next == temp)
					{
						p = p->next;
					}
					p->next = n;
					free(temp);
					return;
				}
				else if (chioce == "no")
				{
					flag = 0;
					printf("已取消该操作 !\n");
					Sleep(5);
					system("cls");
					int function = Umanu();
					Uoperate(function);
					return;
				}
				else
				{
					printf("请输入正确操作 !\n");
				}
				return;
			}
		}
		else
		{
			printf("密码错误 !请30秒后重试 !\n");
			//			Sleep(30);
		}
	}
}
void Uchange(struct Users* head)
{
	struct Users* temp = head;
	printf("请输入您的ID :\n");
	char ID[20];
	scanf("%s", ID);
	while (temp != NULL)
	{
		if (strcmp(ID, temp->ID) == 0)
		{
			char password[20];
			while (1)
			{
				printf("请输入原本的密码 :\n");
				scanf("%s", password);
				if (strcmp(password, temp->passWord) == 0)
				{
					printf("请输入修改之后的密码 :\n");
					scanf("%s", temp->passWord);
					break;
				}
				else
				{
					printf("密码错误!!!输入正确后方可修改!!!\n");
					Sleep(3000);
				}
			}
		}
		else
		{
			temp = temp->next;
		}
	}
}
void writeFile(struct Users* head, char *filename)
{
	FILE* fp = NULL;
	fp = fopen(filename, "w+");
	struct Users* temp = head;
	while (temp != NULL)
	{
		fprintf(fp, "%s\t%s\n", temp->ID, temp->passWord);
		temp = temp->next;
	}
	fclose(fp);
}
int Umanu()
{
	printf("----------------------------------------------------------------------\n");
	printf("|                       食堂用户菜品管理系统                         |\n");
	printf("|--------------------------------------------------------------------|\n");
	printf("|  1.注册新用户                                                      |\n");
	printf("|  2.登录用户                                                        |\n");
	printf("|  3.修改账户密码                                                    |\n");
	printf("----------------------------------------------------------------------\n");
	int function = 0;
	printf("请输入指令 :\n");
	scanf("%d", &function);
	return function;
}

void Uoperate(int function)
{
	struct Users* Uhead = (struct Users*)malloc(sizeof(struct Users));
	Uhead->next = NULL;
	char filename[100] = "D:\\关于学习勇敢的尝试\\食堂用户菜品管理系统\\食堂用户菜品管理系统\\用户资料";
	Uhead = ReadFile(filename);
	if (function == 1)
	{
		Uhead = login(Uhead);
		printf("注册成功 !欢迎使用 !\n");
		writeFile(Uhead, filename);
		Sleep(5);
		system("cls");
		return;
	}
	else if (function == 2)
	{
		reg(Uhead);
		writeFile(Uhead,filename);
	}
	else if (function == 3)
	{
		Uchange(Uhead);
		writeFile(Uhead, filename);
		int function = Umanu();
		Uoperate(function);
	}
	else
	{
		printf("请输入正确的指令!!!\n");
		int function = Umanu();
		Uoperate(function);
	}
}
void manu()
{
	printf("----------------------------------------------------------------------\n");
	printf("|                       食堂用户菜品管理系统                         |\n");
	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("----------------------------------------------------------------------\n");
}
struct cook
{
	char name[50];//菜名
	float price;//价格
	char met[150];//材料
	int num = 0;//评价人数
	float avecom = 0;//平均评价分数
	struct cook* next;//链表操作
};
void Write(struct cook* head, char* filename)
{
	FILE* fp = NULL;
	fp = fopen(filename, "w");
	struct cook* newUser = (struct cook*)malloc(sizeof(struct cook));
	struct cook* temp = head;
	while (temp != NULL)
	{
		fprintf(fp, "%s\t%f\t%s\t%f\t%d\n", temp->name, temp->price, temp->met, temp->avecom, temp->num);
		temp = temp->next;
	}
	return;
}
void Comment(struct cook* head)
{
	struct cook* temp = head;
	struct cook* flag = search(temp);
	if (flag != NULL)
	{
		printf("请输入您的评分 :\n");
		float comment = 0;
		scanf("%f", &comment);
		flag->avecom = ((flag->avecom * flag->num) + comment) / (flag->num + 1);
		flag->num++;
		printf("评价成功!\n");
		return;
	}
	else
	{
		printf("找不到对应菜品!\n");
		return;
	}
}
void SearchCook(struct cook* head)
{
	struct cook* flag = search(head);
	if (flag != NULL)
	{
		printf("找到了!\n");
		print(flag);
	}
	else
	{
		printf("未能找到对应菜品!\n");
	}
}
struct cook* search(struct cook* head)
{
	struct cook* temp = head;
	char name[50];
	printf("请输入您要操作的菜名 :\n");
	scanf("%s", name);
	int flag = 0;
	while (temp != NULL)
	{
		if (strcmp(name, temp->name) == 0)
		{
			flag = 1;
			return temp;
		}
		else
		{
			temp = temp->next;
		}
	}
	if (flag == 0)
	{
		return NULL;
	}
}
void Add(struct cook* head)
{
	struct cook* temp = head;
	while (temp->next != NULL)
	{
		temp = temp->next;
	}
	struct cook* newCook = (struct cook*)malloc(sizeof(struct cook));
	printf("请输入您想添加的菜品的具体信息(菜名/价格/材料/综合评分/评价人数) :\n");
	scanf("%s", newCook->name);
	scanf("%f", &newCook->price);
	scanf("%s", newCook->met);
	scanf("%f", &newCook->avecom);
	scanf("%d", &newCook->num);
	temp->next = newCook;
	newCook->next = NULL;
	printf("添加成功!\n");
}
void Allprint(struct cook* head)
{
	struct cook* temp = head;
	while (temp->next != NULL)
	{
		print(temp);
		temp = temp->next;
	}
	print(temp);
	return;
}
void print(struct cook* temp)
{
	printf("菜名:%s\n", temp->name);
	printf("价格:%0.2f\n", temp->price);
	printf("材料:%s\n", temp->met);
	printf("综合评分:%0.2f\n", temp->avecom);
	printf("评价人数(%d)\n", temp->num);
}
void Change(struct cook* head)
{
	struct cook* temp = head;
	int flag = 0;
	int function;
	struct cook* fflag = search(head);
	if (fflag == NULL)
	{
		printf("没有对应菜品!!!\n");
		return;
	}
	else
	{
		while (1)
		{
			printf("请输入想修改的内容:(1.菜品名称/2.材料/3.价格)\n");
			scanf("%d", &function);
			if (function == 1)
			{
				printf("请输入新的菜名 :\n");
				scanf("%s", fflag->name);
				printf("修改成功!\n");
				break;
			}
			else if (function == 2)
			{
				printf("请输入新的材料列表 :\n");
				scanf("%s", fflag->met);
				printf("修改成功!\n");
				break;
			}
			else if (function == 3)
			{
				printf("请输入新的价格 :\n");
				scanf("%f", &fflag->price);
				printf("修改成功!\n");
				break;
			}
			else
			{
				printf("请输入正确的指令!!!\n");
				Sleep(10000);
			}
		}
	}
}
struct cook* Delete(struct cook* head)
{
	struct cook* temp = head;
	struct cook* next = NULL;
	struct cook* flag = search(head);
	if (flag != NULL)
	{
		if (flag == head)
		{
			head = head->next;
			return head;
		}
		else
		{
			while (temp->next != flag)
			{
				temp = temp->next;
			}
			temp->next = flag->next;
			return head;
		}
	}
	else
	{
		printf("未能找到对应菜品!\n");
	}
	
}
struct cook* Create(struct cook* head)
{
	struct cook* temp = head;
	if (head != NULL)
	{
		while (temp->next != NULL)
		{
			temp = temp->next;
		}
	}
	struct cook* Next = (struct cook*)malloc(sizeof(cook));
	Next->next = NULL;
	printf("请输入你想要录入的数据(菜名/价格/材料/综合评分/评价人数) :\n若想结束,请输入end\n");
	while (1)
	{
		scanf("%s", Next->name);
		if (strcmp(Next->name, "end") == 0)
		{
			break;
		}
		scanf("%f", &Next->price);
		scanf("%s", Next->met);
		scanf("%f", &Next->avecom);
		scanf("%d", &Next->num);
		if (temp == NULL)
		{
			temp = Next;
			head = temp;
		}
		else
		{
			Next->next = NULL;
			temp->next = Next;
			temp = temp->next;
		}
		Next= (struct cook*)malloc(sizeof(cook));
	}
	return head;
}
struct cook* Read(char* filename)
{
	struct cook* head = NULL;
	struct cook* temp = (struct cook*)malloc(sizeof(cook));
	struct cook* newCook = (struct cook*)malloc(sizeof(struct cook));
	newCook->next = NULL;
	FILE* fp = fopen(filename, "r");
	if (fp == NULL)
	{
		return NULL;
	}
	fscanf(fp, "%s%f%s%f%d", temp->name, &temp->price, temp->met, &temp->avecom, &temp->num);
	head = temp;
	temp->next = NULL;
	while (fscanf(fp, "%s%f%s%f%d", newCook->name, &newCook->price, newCook->met, &newCook->avecom, &newCook->num) != EOF)
	{
		temp->next = newCook;
		temp = temp->next;
		newCook = (struct cook*)malloc(sizeof(struct cook));
		newCook->next = NULL;
	}
	fclose(fp);
	return head;
}
void operate()
{
	int fun = 0;
	printf("请输入指令 :\n");
	struct cook* Head = NULL;
	char filename[100]="D:\\关于学习勇敢的尝试\\食堂用户菜品管理系统\\食堂用户菜品管理系统\\菜品资料";
	struct cook* head = Read(filename);
	while (scanf("%d", &fun) != EOF)
	{
		if (fun == 1)
		{
			head = Create(head);
		}
		else if (fun == 2)
		{
			head = Delete(head);
		}
		else if (fun == 3)
		{
			Change(head);
		}
		else if (fun == 4)
		{
			Allprint(head);
		}
		else if (fun == 5)
		{
			Add(head);
		}
		else if (fun == 6)
		{
			SearchCook(head);
		}
		else if (fun == 7)
		{
			Comment(head);
		}
		else if (fun == 8)
		{
			Write(head,filename);
			printf("感谢您的使用!\n");
			return;
		}
		else
		{
			printf("请输入正确的指令!!!\n");
		}
		manu();
		printf("请输入指令 :\n");
	}
}
int main()
{
	int function = Umanu();
	Uoperate(function);
	system("cls");
	manu();
	operate();
}

        该系统我主要划分为两个大模块,分别就是对于用户的操作和对于菜品的操作,对用户的操作,首先提供void Uoperate(),是用户的操作面板,之后选用if-else if-else对指令的解析和调用函数的选择都在这个操作面板,设计内容为
        1、注册新用户
        2、登录用户
        3、修改账户密码
        对于不同的指令执行不同的操作,菜品模块类似于用户模块,提供void operate()函数,是菜品的操作面板,也是对于指令的解析和选择调用函数
        1、导入菜品信息
        2、删除菜品信息
        3、修改菜品信息
        4、打印菜品信息
        5、增加菜品信息
        6、查找菜品信息
        7、评价菜品
        8、退出系统
        主函数main中主要调用就是Uoperate和operate函数,对指令的读取判定和执行都在这两个函数中,这两个函数返回空类型,只负责执行函数。

        此系统中还有两个结构体用来存储不同信息

struct Users
{
	char ID[21];//用户的ID
	char passWord[21];//用户的密码
	struct Users* next;//链表操作,指向下一个存储块
};
struct cook
{
	char name[50];//菜名
	float price;//价格
	char met[150];//材料
	int num = 0;//评价人数
	float avecom = 0;//平均评价分数
	struct cook* next;//链表操作
};

        该系统主要用于用户管理,此时在此段代码中一共有两个链表,一个链表用于存储用户信息,另一个链表用于存储菜品信息。此处特地减少了在主函数中的代码,主函数只负责调用四个主要函数,分别为用户操作菜单,用户操作面板,菜品操作菜单和菜品操作面板。

        仍有许多方面可以改善,比如注册用户和登录用户时候密码的保密性欠缺,用户可以直接看到自己输入的密码,但是实际上应该隐藏数字,防止密码泄露。同时对于用户面板的操作比较单一,而且操作过少,应该增加部分功能,比如销毁账户和退出系统。面板颜色也可以更改,但是在如上代码中没有加入相应操作。这些都是可以在之后弥补的点。

原网站

版权声明
本文为[云琤]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_63291917/article/details/125607752