[C language] realize a simple library management system

Keywords: C

preface:

I was a freshman. I had a whim and made a simple library management system some time ago. I gained a lot from doing this. Basically, I connected the basic knowledge of C language in series. Next, let's share how I implemented the system. (there is still much room for improvement)

1, First, some files to implement the project:

The compiler uses DEV:

  Next is a basic function diagram: (very few functions)

2, Some header files:

1. The following is the basic structure of some linked lists created in C language: (the creation method is based on Mr. Weng Kai's C language course)

//Linked list of book information storage 
typedef struct Node{
	book data;
	struct Node *next;
}node;

//Storage linked list of the number of users 
typedef struct Usernode{
	user data;
	struct Usernode *next;
}usernode;

  2. Some structures store user and Book Information:

//book basic information 
typedef struct Book{
	char name[20];//bookname
	char author[20];//author 
	char press[20];//press 
	float price;//bookprice 
	int num;//booknum
}book;
//Account information structure 
typedef struct User{
	char id[100];
	char password[100]; 
	int num[100];//Number of books borrowed by users
}user; 

  3. Header of two linked lists:

(because it needs to be defined to the global, it is lazy to write it directly into the header file)

//Linked list header of books 
typedef struct List{
	node *head;
}List;
List list;

//Header of user linked list
typedef struct uList{
	usernode *head;
}uList;
uList ulist;

Finally, the code of the whole header file:

#define _CRT_SECURE_NO_WARINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#ifndef _HEAD_H_
#define _HEAD_H_
//book basic information 
typedef struct Book{
	char name[20];//bookname
	char author[20];//author 
	char press[20];//press 
	float price;//bookprice 
	int num;//booknum
}book;
//Account information structure 
typedef struct User{
	char id[100];
	char password[100]; 
	int num[100];
}user; 
//Linked list of book information storage 
typedef struct Node{
	book data;
	struct Node *next;
}node;

//Storage linked list of the number of users 
typedef struct Usernode{
	user data;
	struct Usernode *next;
}usernode;

//Linked list header of books 
typedef struct List{
	node *head;
}List;
List list;

//Header of user linked list
typedef struct uList{
	usernode *head;
}uList;
uList ulist;
void firstkeydown();
#endif

3, Some function files used:

1, Text interface

This part is basically printf. There's no big problem.

        1. User login:

void firstmenu()
{
	printf("fw------------------------------fw\n");
	printf("\t Welcome fw Library management system\n");
	printf("\t0.Exit the system\n");
	printf("\t1.User login\n");
	printf("\t2.Register a new account\n"); 
	printf("\t3.Change Password\n");
	printf("fw------------------------------fw\n");
	printf("\t(Please enter a number to select and press enter to confirm)\n");
} 

        2. Administrator interface:

//Administrator menu interface 
void mangemakemenu()
{
	printf("fw------------------------------fw\n");
	printf("\t Welcome fw Library management system ([administrator])\n");
	printf("\t0.Exit the system\n");
	printf("\t1.Registration book\n");
	printf("\t2.Browse books\n");
	printf("\t3.Borrow books\n");
	printf("\t4.Return books\n");
	printf("\t5.Book sorting\n");
	printf("\t6.Delete books\n");
	printf("\t7.Find books\n");
	printf("\t8.View currently registered users\n");
	printf("fw------------------------------fw\n");
	printf("Please enter (0)~8): "); 
	printf("\t Please enter a number to select and press enter to confirm");
}

        3. General user interface:

void makemenu()
{
	printf("fw------------------------------fw\n");
	printf("\t Welcome fw Library management system ([ordinary user])\n");
	printf("\t0.Exit the system\n");
	printf("\t1.Browse books\n");
	printf("\t2.Borrow books\n");
	printf("\t3.Return books\n");
	printf("\t4.Find books\n");
	printf("fw------------------------------fw\n");
	printf("Please enter (0)~4): "); 
	printf("\t Please enter a number to select and press enter to confirm");		

}

2, Interactive interface

This part is mainly realized by switch case;

Note: all temporary variables to be used are defined before switch case;

switch (A variable)
{
    case 1 :
        xxxxxx;
        break;
    case 2 :
        xxxxxx;
        break;
    default :
       xxxxxx;
       break;
}

         1. Administrator:

//Administrator interaction 
void mangekeydown()
{

	book tempbook;//Temporarily store book information
	node *tempdata=NULL;//Temporary storage of linked list of books
	int userkey=0;
	scanf("%d",&userkey);
	switch (userkey)
		{
			case 0:
			printf("[Exit]\n");
			printf("Exit succeeded!");
			system("pause");
			exit(0);//Exit the entire program 
			break;
			case 1:
			printf("[Registration]\n");
			printf("Please enter the information of the book( name,author,press,price,num):");
			scanf("%s%s%s%f%d",tempbook.name,tempbook.author,tempbook.press,&tempbook.price,&tempbook.num);
			insertnodebyhead(&list,tempbook);
			printf("Registration succeeded\n");
			saveinfortofile("bookinfo.txt",&list);
			break;	
			case 2:
			printf("[Browse]\n");
			printlist(&list);
			break;	
			case 3:
			printf("[[borrowing]\n");
			printf("Please enter the books you want to borrow:\n");
			scanf("%s",tempbook.name);
			tempdata=searchbookname(&list,tempbook.name);
			if (tempdata){
				if(tempdata->data.num>0){
					printf("Borrowing succeeded!");
					tempdata->data.num--;
				} else {
					printf("This book is out of stock!");
				} 
			} else {
				printf("The book does not exist!\n");
			}
			saveinfortofile("bookinfo.txt",&list);
			break;	
			case 4:
			printf("[Return]\n");
			printf("Please enter the books you want to return:\n");
			scanf("%s",tempbook.name);
			tempdata=searchbookname(&list,tempbook.name);
			if (tempdata){
				
					printf("Return succeeded!");
					tempdata->data.num++;
					saveinfortofile("bookinfo.txt",&list);

			} else {
				printf("Abnormal book source!\n");
			}
			break;	
			case 5:
			printf("[Sort]\n");
			bubblesortlist(&list);
			saveinfortofile("bookinfo.txt",&list);
			break;	
			case 6:
			printf("[Delete]\n");
			printf("Please enter the name of the book to be deleted:");
			scanf("%s",tempbook.name); 
			deletenodebyname(&list,tempbook.name);
			//You need to synchronize to the file and modify the file
			saveinfortofile("bookinfo.txt",&list);
			break;	
			case 7:
			printf("[[find]\n");
			printf("Please enter the title of the book you want to find:");
			scanf("%s",tempbook.name);
			tempdata=searchbookname(&list,tempbook.name);
			if (tempdata){
				printf("The information of books is:\n"); 
				printlistnow(tempdata);
			} else {
				printf("The book you are looking for does not exist\n");
			}
			break;
			case 8 :
			printf("The following are registered users:\t\n");
			printulist(&ulist);
			break;	
			default :
			printf("Access error\n");	
			break;
		}
}

        2. Ordinary users:

//General user interface
void keydown()
{
	book tempbook;
	node *tempdata=NULL;
	int userkey=0;
	usernode *nowusernode;
	scanf("%d",&userkey);
	switch (userkey)
		{
			case 0:
			printf("[Exit]\n");
			printf("Exit succeeded!");
			system("pause");
			exit(0);//Exit the entire program 
			break;
			case 1:
			printf("[Browse]\n");
			printlist(&list);
			break;	
			case 2:
			printf("[[borrowing]\n");
			printf("Please enter the books you want to borrow:\n");
			scanf("%s",tempbook.name);
			tempdata=searchbookname(&list,tempbook.name);
			if (tempdata){
				if(tempdata->data.num>0){
					printf("Borrowing succeeded!");
					tempdata->data.num--;
					saveinfortofile("bookinfo.txt",&list);
				} else {
					printf("This book is out of stock!");
				} 
			} else {
				printf("The book does not exist!\n");
			}
			break;	
			case 3:
			printf("[Return]\n");
			printf("Please enter the books you want to return:\n");
			scanf("%s",tempbook.name);
			tempdata=searchbookname(&list,tempbook.name);
			if (tempdata){
				
					printf("Return succeeded!");
					tempdata->data.num++;
					saveinfortofile("bookinfo.txt",&list);

			} else {
				printf("Abnormal book source!\n");
			}
			break;		
			case 4:
			printf("[[find]\n");
			printf("Please enter the title of the book you want to find:");
			scanf("%s",tempbook.name);
			tempdata=searchbookname(&list,tempbook.name);
			if (tempdata){
				printf("The information of books is:\n"); 
				printlistnow(tempdata);
			} else {
				printf("The book you are looking for does not exist\n");
			}
			break;	
			default :
			printf("Access error\n");	
			break;
		}
}

         3. Start interface interaction:

//Start interface 
void firstkeydown()
{
	char *m="manger";//Administrator's account and password 
	int userkey=0;
	user tempuser;
	user newpassword;//New password 
	usernode *tempusernode;
	scanf("%d",&userkey);
	switch (userkey)
	{
		case 0 :
			printf("\t[Exit]\n"); 
			printf("Exit succeeded!");
			system("pause");
			exit(0);
		case 1 :
			
			printf("\t[Start login]\n");
			printf("\t Please enter your account and password:\n");
			printf("Account:\n");
			scanf("%s",tempuser.id);
			printf("password:\n");
			scanf("%s",tempuser.password);
			tempusernode=searchusernameandpassword(&ulist,tempuser.id,tempuser.password);
			if (strcmp(tempuser.id,m)==0&&strcmp(tempuser.password,m)==0) {
				system("pause");
				system("cls"); 
				while (1)
				{
					mangemakemenu();
					mangekeydown();
					system("pause");
					system("cls");	
				}
			} else if (tempusernode){
				system("pause");
				system("cls");
				while (1)
				{
					makemenu();
					keydown();
					system("pause");
					system("cls");
				}
			} else {
				printf("Wrong account or password!\n");
			}
	
			break; 
		case 2 :
			printf("\t[Start registration]\n");
			printf("Please enter your user name:\n");
			scanf("%s",tempuser.id);
			printf("Please enter your password\n");
			scanf("%s",tempuser.password);
			userinsertnodebyhead(&ulist,tempuser);
			usersaveinfortofile("userid.txt",&ulist);
			printf("[Registration succeeded!]\n");
			break;
		case 3 :
			printf("\t[Start modification]\n"); 
			printf("Please enter the account to be modified\t\n");
			scanf("%s",tempuser.id);
			printf("Please enter your original password\t\n");
			scanf("%s",tempuser.password);
			tempusernode=searchusernameandpassword(&ulist,tempuser.id,tempuser.password);
			if (tempusernode){
				printf("Please enter a new password:\t\n");
				scanf("%s",newpassword.password);
				strcpy(tempusernode->data.password,newpassword.password);
				printf("Modification succeeded!\t\n");
				usersaveinfortofile("userid.txt",&ulist); 
			} else {
				printf("Your account or password is entered incorrectly!\t\n");
			}
			break;
	} 
	
} 

3. * *. Function part**

This part is our play. Let's start to realize some of our functions

First, let's take a look at how the start interface is implemented:

        1. The start interface mainly has the following functions:

               (1) Exit the system:

                        This is relatively simple. Just exit(0);

case 0 :
			printf("\t[Exit]\n"); 
			printf("Exit succeeded!");
			system("pause");
			exit(0);

                (2) User login:

                Here, we need to let the user enter the account and password in turn;

                Then we need to start comparing whether the input data is the same as the data stored in the user file in advance:

              (you need to master the basic operation of the document in advance)

                The same or different give different results:

                If the administrator's account is entered, we will jump to the administrator's user interface accordingly, and ordinary users will jump accordingly. If the account is not found, the "account or password error" will be output to jump out of the structure;

case 1 :
			
			printf("\t[Start login]\n");
			printf("\t Please enter your account and password:\n");
			printf("Account:\n");
			scanf("%s",tempuser.id);//A temporary variable tempuser created before starting the function of the interface;
			printf("password:\n");
			scanf("%s",tempuser.password);
			tempusernode=searchusernameandpassword(&ulist,tempuser.id,tempuser.password);
			if (strcmp(tempuser.id,m)==0&&strcmp(tempuser.password,m)==0) {
				system("pause");
				system("cls"); 
                while (1)
				{
					mangemakemenu();
					mangekeydown();
					system("pause");
					system("cls");	
				}
			} else if (tempusernode){
				system("pause");
				system("cls");
				while (1)
				{
					makemenu();
					keydown();
					system("pause");
					system("cls");
				}
			} else {
				printf("Wrong account or password!\n");
			}
	
			break; 

                (3) User registration:

                This part actually stores the input data into our user file through the user linked list;

                Note that when we modify the original data, we must remember to save our modifications;

                 (you need to master the basic operation of the document in advance)

case 2 :
			printf("\t[Start registration]\n");
			printf("Please enter your user name:\n");
			scanf("%s",tempuser.id);
			printf("Please enter your password\n");
			scanf("%s",tempuser.password);
			userinsertnodebyhead(&ulist,tempuser);
			usersaveinfortofile("userid.txt",&ulist);
			printf("[Registration succeeded!]\n");
			break;

                (4) Change Password:

                In essence, the stored user file is modified by modifying the data of a node in the user linked list;

                Note whether the modified account exists. If it does not exist, "account or password error" will be output

        2. User interface:

        Here, the administrator interface is used to explain:

                (1) Exit the system:

                        Or directly exit(0);

case 0:
			printf("[Exit]\n");
			printf("Exit succeeded!");
			system("pause");
			exit(0);//Exit the entire program 

                 (2)   Registration book:

                        Input the information of the book, use the book linked list to store it and store it in the book file;

case 1:
			printf("[Registration]\n");
			printf("Please enter the information of the book( name,author,press,price,num):");
			scanf("%s%s%s%f%d",tempbook.name,tempbook.author,tempbook.press,&tempbook.price,&tempbook.num);
			insertnodebyhead(&list,tempbook);
			printf("Registration succeeded\n");
			saveinfortofile("bookinfo.txt",&list);
			break;	

                        Use insert nodebyhead (& list, tempbook); Store the input book information in the book linked list;

void insertnodebyhead(List *plist,book data)
{
	node *newnode=creatnode(data);
	newnode->next=plist->head;
	plist->head=newnode;	
} 

                     Then save infortofile ("bookinfo. TXT", & list); Perform file operation and store the information in the book file;

void saveinfortofile(const char *filename,List *plist)
{
	FILE *fp=fopen(filename,"w");
	node *pmove=plist->head; 
	while (pmove != NULL) 
	{
		fprintf(fp,"%s\t%s\t%s\t%.1f\t%d\t\n",pmove->data.name,pmove->data.author,pmove->data.press,pmove->data.price,pmove->data.num);
		pmove=pmove->next;
	}
	fclose(fp);
} 

                (3) Browse books

                        The essence is to traverse and print the book files through the book linked list;

case 2:
			printf("[Browse]\n");
			printlist(&list);
			break;	

                         & use printlist; conduct

void printlist(List *plist)
{
	node *pmove=plist->head;
	printf("title\t\t author\t\t press\t\t Price\t\t quantity\t\n");
	while (pmove!=NULL) {
		printf("%s\t\t%s\t\t%s\t\t%.1f\t\t%d\t\t\n",pmove->data.name,pmove->data.author,pmove->data.press,pmove->data.price,pmove->data.num);
		pmove=pmove->next;
	}	
}

                 (4) Borrow books

case 3:
			printf("[[borrowing]\n");
			printf("Please enter the books you want to borrow:\n");
			scanf("%s",tempbook.name);
			tempdata=searchbookname(&list,tempbook.name);
			if (tempdata){
				if(tempdata->data.num>0){
					printf("Borrowing succeeded!");
					tempdata->data.num--;
				} else {
					printf("This book is out of stock!");
				} 
			} else {
				printf("The book does not exist!\n");
			}
			saveinfortofile("bookinfo.txt",&list);
			break;	

                        The essence of borrowing books is to find the target books by traversing the book list and reduce the number of books in the list node. Let tempdata - > data. Num --; Remember to save the data after modification. Use the save info file ("bookinfo. TXT", & list); Perform file operations.

                 (5) Return books:

                        The method is basically the same as borrowing, except that the data of the target linked list node is added and saved.

case 4:
			printf("[Return]\n");
			printf("Please enter the books you want to return:\n");
			scanf("%s",tempbook.name);
			tempdata=searchbookname(&list,tempbook.name);
			if (tempdata){
				
					printf("Return succeeded!");
					tempdata->data.num++;
					saveinfortofile("bookinfo.txt",&list);

			} else {
				printf("Abnormal book source!\n");
			}
			break;	

                        It should be noted that we need to take one step to determine whether the book originally exists. Use tempdata = searchbookname (& list, tempbook. Name); If the book is found in the original book file, it will succeed, otherwise it will fail;

node *searchbookname(List *plist,char *bookname)
{
	node *p=NULL;
	for (p=plist->head;p;p=p->next){
		if (strcmp(bookname,p->data.name)==0)
		{
			return p;//Return if found
		} 	
	}
	return NULL;//Return null not found	
} 

                 (6) Book sorting:

                        The essence is to sort the linked list of books. The method of "bubble sorting" is used here.

                       Pay attention to the storage of files after modification.

case 5:
			printf("[Sort]\n");
			bubblesortlist(&list);
			saveinfortofile("bookinfo.txt",&list);
			break;	

                        Bubble sorting used in Book linked list:

void bubblesortlist(List *plist)
{
	node *p;
	node *q;
	for (p=plist->head;p!=NULL;p=p->next){
		for (q=plist->head;q->next!=NULL;q=q->next)
		{
			if (q->data.price>q->next->data.price) {
				book tempdata;
				tempdata=q->data;
				q->data=q->next->data;
				q->next->data=tempdata;
			}
		}
	}
	printlist(&list);//Print the linked list after sorting
} 

                 (7) Delete book

                        It is basically the same as borrowing books and returning books, except that the entire target node needs to be cleared;

                        Delete and search by book title;

case 6:
			printf("[Delete]\n");
			printf("Please enter the name of the book to be deleted:");
			scanf("%s",tempbook.name); 
			deletenodebyname(&list,tempbook.name);
			//You need to synchronize to the file and modify the file
			saveinfortofile("bookinfo.txt",&list);
			break;	

                        Delete this node by using deletenodebyname (& list, tempbook. Name); finally, remember to save the modification of the file saveinfofile ("bookinfo. TXT", & list);

void deletenodebyname(List *plist,char *bookname)
{
	int judge=0;//To determine if it was found
	node *nodeleft;
	node *nodepos;
	for (nodeleft=NULL,nodepos=plist->head;nodepos;nodeleft=nodepos,nodepos=nodepos->next)
	{
		if (strcmp(nodepos->data.name,bookname)==0) {
			if (nodeleft){
			nodeleft->next=nodepos->next;
			} else {
				plist->head=nodepos->next;
			}
			judge=1;
			free(nodepos);//Delete this node
			break;
		}
		
	}
	if (judge) {
		printf("Delete succeeded!\n");
	} else {
		printf("Deletion failed, the book was not found!");
	}
} 

                 (8) Find books:

                        As before borrowing, deleting and returning, traverse to find the target node and print.

                        No, the output does not exist.

case 7:
			printf("[[find]\n");
			printf("Please enter the title of the book you want to find:");
			scanf("%s",tempbook.name);
			tempdata=searchbookname(&list,tempbook.name);
			if (tempdata){
				printf("The information of books is:\n"); 
				printlistnow(tempdata);
			} else {
				printf("The book you are looking for does not exist\n");
			}

                (9) View registered users:

                        The same method is used to traverse and print the user linked list;

case 8 :
			printf("The following are registered users:\t\n");
			printulist(&ulist);
			break;	

                        Output user information & printulist;

void printulist(uList *plist)
{
	usernode *pmove=plist->head;
	printf("User name\t\n");
	while (pmove!=NULL) {
		printf("%s\t\n",pmove->data.id);
		pmove=pmove->next;
	}	

The above is the introduction of the administrator's operation interface. The following ordinary user operation interface only deletes some functions on its basis, which will not be repeated here.

The following is all the code for the function file:

#include "head.h"
void firstmenu()
{
	printf("fw------------------------------fw\n");
	printf("\t Welcome fw Library management system\n");
	printf("\t0.Exit the system\n");
	printf("\t1.User login\n");
	printf("\t2.Register a new account\n"); 
	printf("\t3.Change Password\n");
	printf("fw------------------------------fw\n");
	printf("\t(Please enter a number to select and press enter to confirm)\n");
} 

//Administrator menu interface 
void mangemakemenu()
{
	printf("fw------------------------------fw\n");
	printf("\t Welcome fw Library management system ([administrator])\n");
	printf("\t0.Exit the system\n");
	printf("\t1.Registration book\n");
	printf("\t2.Browse books\n");
	printf("\t3.Borrow books\n");
	printf("\t4.Return books\n");
	printf("\t5.Book sorting\n");
	printf("\t6.Delete books\n");
	printf("\t7.Find books\n");
	printf("\t8.View currently registered users\n");
	printf("fw------------------------------fw\n");
	printf("Please enter (0)~8): "); 
	printf("\t Please enter a number to select and press enter to confirm");
}
void makemenu()
{
	printf("fw------------------------------fw\n");
	printf("\t Welcome fw Library management system ([ordinary user])\n");
	printf("\t0.Exit the system\n");
	printf("\t1.Browse books\n");
	printf("\t2.Borrow books\n");
	printf("\t3.Return books\n");
	printf("\t4.Find books\n");
	printf("fw------------------------------fw\n");
	printf("Please enter (0)~4): "); 
	printf("\t Please enter a number to select and press enter to confirm");		

}
//Add linked list node 
node *creatnode(book data)
{
	node *newnode=(node*)malloc(sizeof(node));
	newnode->data=data;
	newnode->next=NULL;
	return newnode; 
}
usernode *usercreatnode(user data)
{
	usernode *newnode=(usernode*)malloc(sizeof(usernode));
	newnode->data=data;
	newnode->next=NULL;
	return newnode; 
}
//Print all data 
void printlist(List *plist)
{
	node *pmove=plist->head;
	printf("title\t\t author\t\t press\t\t Price\t\t quantity\t\n");
	while (pmove!=NULL) {
		printf("%s\t\t%s\t\t%s\t\t%.1f\t\t%d\t\t\n",pmove->data.name,pmove->data.author,pmove->data.press,pmove->data.price,pmove->data.num);
		pmove=pmove->next;
	}	
}
void printulist(uList *plist)
{
	usernode *pmove=plist->head;
	printf("User name\t\n");
	while (pmove!=NULL) {
		printf("%s\t\n",pmove->data.id);
		pmove=pmove->next;
	}	
	
}
void printlistnow(node *pmove)
{
	printf("title\t author\t press\t Price\t quantity\t\n");
	printf("%s\t%s\t%s\t%.1f\t%d\t\n",pmove->data.name,pmove->data.author,pmove->data.press,pmove->data.price,pmove->data.num);	
} 
//Head insertion
void insertnodebyhead(List *plist,book data)
{
	node *newnode=creatnode(data);
	newnode->next=plist->head;
	plist->head=newnode;	
} 
void userinsertnodebyhead(uList *plist,user data)
{
	usernode *newnode=usercreatnode(data);
	newnode->next=plist->head;
	plist->head=newnode;	
}
//Tail interpolation
void insertnodebytail(List *plist,book data)
{
	node *newnode =creatnode(data);
	node *pmove=plist->head;
	if (pmove){
	while (pmove->next!=NULL) {
		pmove=pmove->next;
		}
		pmove->next=newnode;
	} else {
		plist->head=newnode;
	}
} 
//Delete at specified location
//Title deletion 
void deletenodebyname(List *plist,char *bookname)
{
	int judge=0;
	node *nodeleft;
	node *nodepos;
	for (nodeleft=NULL,nodepos=plist->head;nodepos;nodeleft=nodepos,nodepos=nodepos->next)
	{
		if (strcmp(nodepos->data.name,bookname)==0) {
			if (nodeleft){
			nodeleft->next=nodepos->next;
			} else {
				plist->head=nodepos->next;
			}
			judge=1;
			free(nodepos);
			break;
		}
		
	}
	if (judge) {
		printf("Delete succeeded!\n");
	} else {
		printf("Deletion failed, the book was not found!");
	}
} 
//lookup
//book
node *searchbookname(List *plist,char *bookname)
{
	node *p=NULL;
	for (p=plist->head;p;p=p->next){
		if (strcmp(bookname,p->data.name)==0)
		{
			return p;
		} 	
	}
	return NULL;	
} 
//user
usernode *searchusernameandpassword(uList *plist,char *username,char *userpassword)
{
	usernode *p=NULL;
	for (p=plist->head;p;p=p->next){
		if (strcmp(username,p->data.id)==0&&strcmp(userpassword,p->data.password)==0)
		{
			return p;
		} 
	}	
	return NULL;
}

//Insert at specified location
/*void addnode(List *plist,char *bookname,book data)
{
	//Using string comparison functions 
	node *p;
	node *q;
	node *addnode=creatnode(data); 
	for (q=NULL,p=plist->head;p;q=p,p=p->next)
	{
		if (strcmp(bookname,p->data.name)) {
			if (q){
			q->next=addnode;
			addnode->next=p;
			} else {
				plist->head=p->next;
				p->next=addnode;
			}
		}
		break;
	}
} */
//File operation
//Storage of book information 
//Write file 
void saveinfortofile(const char *filename,List *plist)
{
	FILE *fp=fopen(filename,"w");
	node *pmove=plist->head; 
	while (pmove != NULL) 
	{
		fprintf(fp,"%s\t%s\t%s\t%.1f\t%d\t\n",pmove->data.name,pmove->data.author,pmove->data.press,pmove->data.price,pmove->data.num);
		pmove=pmove->next;
	}
	fclose(fp);
} 
//read file 
void readinfortofile(const char *filename,List *plist)
{
	FILE *fp=fopen(filename,"r");//First open does not exist, create file 
	if (fp==NULL)
	{
		fp=fopen(filename,"w+");
	}
	//Define a temporary variable tempdata 
	book tempdata;
	while (fscanf(fp,"%s\t%s\t%s\t%f\t%d\n",tempdata.name,tempdata.author,tempdata.press,&tempdata.price,&tempdata.num)!=EOF) 
	{
		insertnodebyhead(&list,tempdata);
	}
	fclose(fp);
}
//Storage of user information 
void usersaveinfortofile(const char *filename,uList *plist)
{
	FILE *ufp=fopen(filename,"w");
	usernode *pmove=plist->head; 
	while (pmove != NULL) 
	{
		fprintf(ufp,"%s\t%s\t\n",pmove->data.id,pmove->data.password);
		pmove=pmove->next;
	}
	fclose(ufp);
} 
void userreadinfortofile(const char *filename,List *plist)
{
	FILE *ufp=fopen(filename,"r");//First open does not exist, create file 
	if (ufp==NULL)
	{
		ufp=fopen(filename,"w+");
	}
	//Define a temporary variable tempdata 
	user tempdata;
	while (fscanf(ufp,"%s\t%s\t\n",tempdata.id,tempdata.password)!=EOF) 
	{
		userinsertnodebyhead(&ulist,tempdata);
	}
	fclose(ufp);
}

//Sort by price 
void bubblesortlist(List *plist)
{
	node *p;
	node *q;
	for (p=plist->head;p!=NULL;p=p->next){
		for (q=plist->head;q->next!=NULL;q=q->next)
		{
			if (q->data.price>q->next->data.price) {
				book tempdata;
				tempdata=q->data;
				q->data=q->next->data;
				q->next->data=tempdata;
			}
		}
	}
	printlist(&list);	
} 
//Interactive interface
 
//Administrator interaction 
void mangekeydown()
{

	book tempbook;
	node *tempdata=NULL;
	int userkey=0;
	scanf("%d",&userkey);
	switch (userkey)
		{
			case 0:
			printf("[Exit]\n");
			printf("Exit succeeded!");
			system("pause");
			exit(0);//Exit the entire program 
			break;
			case 1:
			printf("[Registration]\n");
			printf("Please enter the information of the book( name,author,press,price,num):");
			scanf("%s%s%s%f%d",tempbook.name,tempbook.author,tempbook.press,&tempbook.price,&tempbook.num);
			insertnodebyhead(&list,tempbook);
			printf("Registration succeeded\n");
			saveinfortofile("bookinfo.txt",&list);
			break;	
			case 2:
			printf("[Browse]\n");
			printlist(&list);
			break;	
			case 3:
			printf("[[borrowing]\n");
			printf("Please enter the books you want to borrow:\n");
			scanf("%s",tempbook.name);
			tempdata=searchbookname(&list,tempbook.name);
			if (tempdata){
				if(tempdata->data.num>0){
					printf("Borrowing succeeded!");
					tempdata->data.num--;
				} else {
					printf("This book is out of stock!");
				} 
			} else {
				printf("The book does not exist!\n");
			}
			saveinfortofile("bookinfo.txt",&list);
			break;	
			case 4:
			printf("[Return]\n");
			printf("Please enter the books you want to return:\n");
			scanf("%s",tempbook.name);
			tempdata=searchbookname(&list,tempbook.name);
			if (tempdata){
				
					printf("Return succeeded!");
					tempdata->data.num++;
					saveinfortofile("bookinfo.txt",&list);

			} else {
				printf("Abnormal book source!\n");
			}
			break;	
			case 5:
			printf("[Sort]\n");
			bubblesortlist(&list);
			saveinfortofile("bookinfo.txt",&list);
			break;	
			case 6:
			printf("[Delete]\n");
			printf("Please enter the name of the book to be deleted:");
			scanf("%s",tempbook.name); 
			deletenodebyname(&list,tempbook.name);
			//You need to synchronize to the file and modify the file
			saveinfortofile("bookinfo.txt",&list);
			break;	
			case 7:
			printf("[[find]\n");
			printf("Please enter the title of the book you want to find:");
			scanf("%s",tempbook.name);
			tempdata=searchbookname(&list,tempbook.name);
			if (tempdata){
				printf("The information of books is:\n"); 
				printlistnow(tempdata);
			} else {
				printf("The book you are looking for does not exist\n");
			}
			break;
			case 8 :
			printf("The following are registered users:\t\n");
			printulist(&ulist);
			break;	
			default :
			printf("Access error\n");	
			break;
		}
}
//General user interface
void keydown()
{
	book tempbook;
	node *tempdata=NULL;
	int userkey=0;
	usernode *nowusernode;
	scanf("%d",&userkey);
	switch (userkey)
		{
			case 0:
			printf("[Exit]\n");
			printf("Exit succeeded!");
			system("pause");
			exit(0);//Exit the entire program 
			break;
			case 1:
			printf("[Browse]\n");
			printlist(&list);
			break;	
			case 2:
			printf("[[borrowing]\n");
			printf("Please enter the books you want to borrow:\n");
			scanf("%s",tempbook.name);
			tempdata=searchbookname(&list,tempbook.name);
			if (tempdata){
				if(tempdata->data.num>0){
					printf("Borrowing succeeded!");
					tempdata->data.num--;
					saveinfortofile("bookinfo.txt",&list);
				} else {
					printf("This book is out of stock!");
				} 
			} else {
				printf("The book does not exist!\n");
			}
			break;	
			case 3:
			printf("[Return]\n");
			printf("Please enter the books you want to return:\n");
			scanf("%s",tempbook.name);
			tempdata=searchbookname(&list,tempbook.name);
			if (tempdata){
				
					printf("Return succeeded!");
					tempdata->data.num++;
					saveinfortofile("bookinfo.txt",&list);

			} else {
				printf("Abnormal book source!\n");
			}
			break;		
			case 4:
			printf("[[find]\n");
			printf("Please enter the title of the book you want to find:");
			scanf("%s",tempbook.name);
			tempdata=searchbookname(&list,tempbook.name);
			if (tempdata){
				printf("The information of books is:\n"); 
				printlistnow(tempdata);
			} else {
				printf("The book you are looking for does not exist\n");
			}
			break;	
			default :
			printf("Access error\n");	
			break;
		}
}

//Start interface 
void firstkeydown()
{
	char *m="manger";//Administrator's account and password 
	int userkey=0;
	user tempuser;
	user newpassword;//New password 
	usernode *tempusernode;
	scanf("%d",&userkey);
	switch (userkey)
	{
		case 0 :
			printf("\t[Exit]\n"); 
			printf("Exit succeeded!");
			system("pause");
			exit(0);
		case 1 :
			
			printf("\t[Start login]\n");
			printf("\t Please enter your account and password:\n");
			printf("Account:\n");
			scanf("%s",tempuser.id);
			printf("password:\n");
			scanf("%s",tempuser.password);
			tempusernode=searchusernameandpassword(&ulist,tempuser.id,tempuser.password);
			if (strcmp(tempuser.id,m)==0&&strcmp(tempuser.password,m)==0) {
				system("pause");
				system("cls"); 
				while (1)
				{
					mangemakemenu();
					mangekeydown();
					system("pause");
					system("cls");	
				}
			} else if (tempusernode){
				system("pause");
				system("cls");
				while (1)
				{
					makemenu();
					keydown();
					system("pause");
					system("cls");
				}
			} else {
				printf("Wrong account or password!\n");
			}
	
			break; 
		case 2 :
			printf("\t[Start registration]\n");
			printf("Please enter your user name:\n");
			scanf("%s",tempuser.id);
			printf("Please enter your password\n");
			scanf("%s",tempuser.password);
			userinsertnodebyhead(&ulist,tempuser);
			usersaveinfortofile("userid.txt",&ulist);
			printf("[Registration succeeded!]\n");
			break;
		case 3 :
			printf("\t[Start modification]\n"); 
			printf("Please enter the account to be modified\t\n");
			scanf("%s",tempuser.id);
			printf("Please enter your original password\t\n");
			scanf("%s",tempuser.password);
			tempusernode=searchusernameandpassword(&ulist,tempuser.id,tempuser.password);
			if (tempusernode){
				printf("Please enter a new password:\t\n");
				scanf("%s",newpassword.password);
				strcpy(tempusernode->data.password,newpassword.password);
				printf("Modification succeeded!\t\n");
				usersaveinfortofile("userid.txt",&ulist); 
			} else {
				printf("Your account or password is entered incorrectly!\t\n");
			}
			break;
	} 
	
} 

Main function:

In fact, the main function is very simple. It just calls the file written in the function file. It should be noted that each time, you must read in the user file and the book file, and use the readinfortofile ("bookinfo. TXT", & list);
     userreadinfortofile("userid.txt",&ulist); conduct.

#include "head.h"
//Main function 
int main() {
	readinfortofile("bookinfo.txt",&list);
	userreadinfortofile("userid.txt",&ulist);
	while (1)
	{
		firstmenu();
		firstkeydown();
		system("pause");
		system("cls");
	}
	
	system("pause");
	return 0;
}

Book file read in:

//read file 
void readinfortofile(const char *filename,List *plist)
{
	FILE *fp=fopen(filename,"r");//First open does not exist, create file 
	if (fp==NULL)
	{
		fp=fopen(filename,"w+");
	}
	//Define a temporary variable tempdata 
	book tempdata;
	while (fscanf(fp,"%s\t%s\t%s\t%f\t%d\n",tempdata.name,tempdata.author,tempdata.press,&tempdata.price,&tempdata.num)!=EOF) 
	{
		insertnodebyhead(&list,tempdata);
	}
	fclose(fp);
}

User file read in:

void userreadinfortofile(const char *filename,List *plist)
{
	FILE *ufp=fopen(filename,"r");//First open does not exist, create file 
	if (ufp==NULL)
	{
		ufp=fopen(filename,"w+");
	}
	//Define a temporary variable tempdata 
	user tempdata;
	while (fscanf(ufp,"%s\t%s\t\n",tempdata.id,tempdata.password)!=EOF) 
	{
		userinsertnodebyhead(&ulist,tempdata);
	}
	fclose(ufp);
}

Write at the end

In general, the functions of this library management system are still very few, and there are many functions, such as who borrowed the books, how many books have been borrowed, reminding users to return the books in time, users to view the books they borrowed, and so on. However, limited to the current ability, it can not be well realized. In fact, the implementation of this system relies on the basic operation of the linked list, creates two linked lists for separate operation, makes boring copy and paste, and finally realizes.

Some tutorials and articles for reference:

Station B: C/C + + knowledge project tutorial: library management system! Hand in hand to teach you to write University C language library management system, simple and easy to use, University C language will_ Beep beep beep_ bilibilihttps://www.bilibili.com/video/BV11p4y1i7JH?spm_id_from=333.999.0.0csdn: C language programming: library management system (super detailed login system, with code and experimental report)_ Blog of i6223671 - CSDN blog_ Library management system C languagehttps://blog.csdn.net/i6223671/article/details/82936523?utm_source=app&app_version=4.18.0&code=app_1562916241&uLinkId=usr1mkqgl919blen

Posted by Hannes2k on Thu, 18 Nov 2021 06:12:31 -0800