当前位置:网站首页>Canteen user dish relationship system (C language course design)

Canteen user dish relationship system (C language course design)

2022-07-07 04:43:00 Yun Cheng

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<windows.h>
// User related operations 
int Umanu();// User action list 
void Uoperate(int function);// User's instruction decision 
struct Users* ReadFile(char* filename);// Read the user data in the previous file 
struct Users* login(struct Users* Uhead);// Create user actions 
void reg(struct Users* Uhead);// Login operation 
void writeFile(struct Users* head, char* filename);// Write user information to file 
void Uchange(struct Users* head);// Change user password 

void operate();// Operation list 
void SearchCook(struct cook* head);// Look for the corresponding dishes , Internal calls search and print
struct cook* search(struct cook* head);// Small search function , Perform the specified search 
void Write(struct cook* head, char* filename);// Write the dish data 
void Comment(struct cook* head);// Evaluate dishes 
void Add(struct cook* head);// Add new dishes at the end of the list 
void Allprint(struct cook* head);// Print all the elements in the linked list 
void print(struct cook* temp);// Print a single node , Exists as an internal tool class function 
void Change(struct cook* head);// Modify the dishes 
struct cook* Read(char* filename);// Read out the contents of the stored dishes 

struct Users
{
	char ID[21];// User ID
	char passWord[21];// User's password 
	struct Users* next;// List operation , Point to the next storage block 
};
struct Users* login(struct Users* Uhead)
{
	struct Users* newUser = (struct Users*)malloc(sizeof(struct Users));
	struct Users* temp = Uhead;
	printf(" Create your ID(1-20 position ):\n");
	scanf("%s", newUser->ID);
	printf(" Please enter the password you set (1-20 position ) :\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(" Please enter your 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(" The ID Not registered yet ! Please register first !\n");
		Sleep(1500);
		int function = Umanu();
		Uoperate(function);
		return;
	}
	char passWord[20];
	int flag = 1;
	while (flag)
	{
		printf(" Please enter your password  :\n");
		scanf("%s", passWord);
		if (strcmp(passWord,temp->passWord)==0)
		{
			flag = 0;
			printf(" The password is correct  ! Welcome to use  !\n");
			Sleep(1000);
			return;
		}
		else
		{
			printf(" Wrong password  ! please 30 Try again in seconds  !\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(" Please enter your ID :\n");
	char chioce[5];
	scanf("%s", UID);
	char passWord[21];
	struct Users* temp = Uhead;
	while (temp->ID == UID)
	{
		temp = temp->next;
	}
	printf(" Please input a password  :\n");
	scanf("%s", passWord);
	while (passWord == temp->passWord)
	{
		printf(" Please enter your password  :\n");
		scanf("%d", &passWord);
		if (passWord == temp->passWord)
		{
			int flag = 1;
			printf(" The password is correct  ! Are you sure you want to destroy the account  ?(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(" The operation was canceled  !\n");
					Sleep(5);
					system("cls");
					int function = Umanu();
					Uoperate(function);
					return;
				}
				else
				{
					printf(" Please input the correct operation  !\n");
				}
				return;
			}
		}
		else
		{
			printf(" Wrong password  ! please 30 Try again in seconds  !\n");
			//			Sleep(30);
		}
	}
}
void Uchange(struct Users* head)
{
	struct Users* temp = head;
	printf(" Please enter your ID :\n");
	char ID[20];
	scanf("%s", ID);
	while (temp != NULL)
	{
		if (strcmp(ID, temp->ID) == 0)
		{
			char password[20];
			while (1)
			{
				printf(" Please enter the original password  :\n");
				scanf("%s", password);
				if (strcmp(password, temp->passWord) == 0)
				{
					printf(" Please enter the modified password  :\n");
					scanf("%s", temp->passWord);
					break;
				}
				else
				{
					printf(" Wrong password !!! Correct input can be modified !!!\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("|                        Canteen user dish management system                          |\n");
	printf("|--------------------------------------------------------------------|\n");
	printf("|  1. Register new users                                                       |\n");
	printf("|  2. The logged in user                                                         |\n");
	printf("|  3. Change account password                                                     |\n");
	printf("----------------------------------------------------------------------\n");
	int function = 0;
	printf(" Please enter the command  :\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:\\ About learning brave attempts \\ Canteen user dish management system \\ Canteen user dish management system \\ User information ";
	Uhead = ReadFile(filename);
	if (function == 1)
	{
		Uhead = login(Uhead);
		printf(" Registered successfully  ! Welcome to use  !\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(" Please enter the correct command !!!\n");
		int function = Umanu();
		Uoperate(function);
	}
}
void manu()
{
	printf("----------------------------------------------------------------------\n");
	printf("|                        Canteen user dish management system                          |\n");
	printf("|--------------------------------------------------------------------|\n");
	printf("|  1. Import dish information                                                     |\n");
	printf("|  2. Delete the dish information                                                     |\n");
	printf("|  3. Modify dish information                                                     |\n");
	printf("|  4. Print dish information                                                     |\n");
	printf("|  5. Add dish information                                                     |\n");
	printf("|  6. Find dish information                                                     |\n");
	printf("|  7. Evaluate dishes                                                         |\n");
	printf("|  8. Exit the system                                                         |\n");
	printf("----------------------------------------------------------------------\n");
}
struct cook
{
	char name[50];// Dish name 
	float price;// Price 
	char met[150];// material 
	int num = 0;// Number of evaluators 
	float avecom = 0;// Average evaluation score 
	struct cook* next;// List operation 
};
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(" Please enter your rating  :\n");
		float comment = 0;
		scanf("%f", &comment);
		flag->avecom = ((flag->avecom * flag->num) + comment) / (flag->num + 1);
		flag->num++;
		printf(" Evaluate success !\n");
		return;
	}
	else
	{
		printf(" The corresponding dish cannot be found !\n");
		return;
	}
}
void SearchCook(struct cook* head)
{
	struct cook* flag = search(head);
	if (flag != NULL)
	{
		printf(" eureka !\n");
		print(flag);
	}
	else
	{
		printf(" Failed to find the corresponding dish !\n");
	}
}
struct cook* search(struct cook* head)
{
	struct cook* temp = head;
	char name[50];
	printf(" Please enter the name of the dish you want to operate  :\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(" Please enter the specific information of the dish you want to add ( Dish name / Price / material / Comprehensive score / Number of evaluators ) :\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(" Add success !\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(" Dish name :%s\n", temp->name);
	printf(" Price :%0.2f\n", temp->price);
	printf(" material :%s\n", temp->met);
	printf(" Comprehensive score :%0.2f\n", temp->avecom);
	printf(" Number of evaluators (%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(" There is no corresponding dish !!!\n");
		return;
	}
	else
	{
		while (1)
		{
			printf(" Please enter the content you want to modify :(1. Dish name /2. material /3. Price )\n");
			scanf("%d", &function);
			if (function == 1)
			{
				printf(" Please enter a new dish name  :\n");
				scanf("%s", fflag->name);
				printf(" Modification successful !\n");
				break;
			}
			else if (function == 2)
			{
				printf(" Please enter a new material list  :\n");
				scanf("%s", fflag->met);
				printf(" Modification successful !\n");
				break;
			}
			else if (function == 3)
			{
				printf(" Please enter a new price  :\n");
				scanf("%f", &fflag->price);
				printf(" Modification successful !\n");
				break;
			}
			else
			{
				printf(" Please enter the correct command !!!\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(" Failed to find the corresponding dish !\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(" Please enter the data you want to enter ( Dish name / Price / material / Comprehensive score / Number of evaluators ) :\n If you want to end , Please enter 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(" Please enter the command  :\n");
	struct cook* Head = NULL;
	char filename[100]="D:\\ About learning brave attempts \\ Canteen user dish management system \\ Canteen user dish management system \\ Dish information ";
	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(" Thank you for using !\n");
			return;
		}
		else
		{
			printf(" Please enter the correct command !!!\n");
		}
		manu();
		printf(" Please enter the command  :\n");
	}
}
int main()
{
	int function = Umanu();
	Uoperate(function);
	system("cls");
	manu();
	operate();
}

         The system is mainly divided into two modules , They are the operation for users and the operation for dishes , Operations on users , First of all, provide void Uoperate(), It is the user's operation panel , Then choose if-else if-else The parsing of instructions and the selection of calling functions are all in this operation panel , The design content is
        1、 Register new users
        2、 The logged in user
        3、 Change account password
         Perform different operations for different instructions , The dishes module is similar to the user module , Provide void operate() function , It is the operation panel of dishes , It is also for the parsing and selection of instructions to call functions
        1、 Import dish information
        2、 Delete the dish information
        3、 Modify dish information
        4、 Print dish information
        5、 Add dish information
        6、 Find dish information
        7、 Evaluate dishes
        8、 Exit the system
         The main function main The main call in is Uoperate and operate function , The read decision and execution of instructions are in these two functions , These two functions return empty types , Only responsible for executing functions .

        There are also two structures in this system to store different information

struct Users
{
	char ID[21];// User ID
	char passWord[21];// User's password 
	struct Users* next;// List operation , Point to the next storage block 
};
struct cook
{
	char name[50];// Dish name 
	float price;// Price 
	char met[150];// material 
	int num = 0;// Number of evaluators 
	float avecom = 0;// Average evaluation score 
	struct cook* next;// List operation 
};

         The system is mainly used for user management , At this time, there are two linked lists in this code , A linked list is used to store user information , Another linked list is used to store dish information . The code in the main function is specially reduced here , The main function is only responsible for calling four main functions , They are user operation menus , User operation panel , Menu and panel of dishes .

         There are still many aspects that can be improved , For example, when registering users and logging in users, the password is not confidential , Users can directly see the password they entered , But actually you should hide the numbers , Prevent password leaks . At the same time, the operation of the user panel is relatively simple , And there are too few operations , Some functions should be added , Such as destroying accounts and exiting the system . The panel color can also be changed , But there is no corresponding operation in the above code . These are all points that can be made up later .

原网站

版权声明
本文为[Yun Cheng]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207062152249762.html