Current location - Training Enrollment Network - Books and materials - How to establish a library database management system with C++?
How to establish a library database management system with C++?
# include & ltstdio.h & gt

# include & ltstring.h & gt

# include & ltstdlib.h & gt

//

#define MAXSIZE 100 // The maximum value is defined as 100.

# definelist _ init _ size 100//The maximum number of users of the library card is defined as100.

//The structure of the borrower

Typedef structure Boro// borrowing behavior

{

char BNum[20]; //The book number of the borrowed book

char RetDate[8]; //Return date

struct Boro * next

} Bor

Typedef structure link book

{

Bor * next// The borrowing behavior of this library card

char CNum[20]; //Certificate number

Int Total// Number of books borrowed

} lend[LIST _ INIT _ SIZE]; //borrower array

//structural information of the book

Typedef structure LNode

{

char card num[20]; //library ID number

struct LNode * next

} Linked list; //Borrower

Typedef structure book

{//The contents to be registered for each book include ISBN, title, author, publisher, total inventory and existing inventory.

Character number [20]; //Book number

char name[20]; //Book title

char auth[20]; //author

char pub[20]; //Press

Int TotNum// total inventory

Int NowNum// Current inventory

LinkList * next// borrower

} ook[MAXSIZE];

//

Int Retotal// number of readers

Int total// defines external variables. Number of books

//

//Structure initialization

void InitBo(ook & amp; Boo) // Initialize book information

{

for(int I = 0; I & ltMAXSIZEi++)

{

Shh [me]. NowNum = 0;

Shh [me]. tot num = 0;

Shh [me]. next = NULL

}

}

void InitRe(lend & amp; Lin) // Initialize borrower information

{

for(int I = 0; I & ltLIST _ INIT _ SIZEi++)

Lin [I]. next = NULL

}

//

int mid = 0; //The external function mid is used to return the found position.

Method of bisection (OOkboo, Charsearchnum[])// Dichotomy to find the comparison book number.

{//bool function is used, but since the function cannot have two return values, an external variable mid is set to return the found position.

int low=0,high = total- 1;

int found = 0;

while(low & lt; = high)

{

Mid= (low+high)/2; //intermediate point

If (strcmp (boo [mid])。 Num, search num)= = 0)// The book number is the same.

{

Found =1;

Return true

}//Search succeeded.

if(strcmp(boo[mid])。 num,SearchNum)! =0)// The book number is different.

High = medium1;

Else low = mid+1;

}

If (discovery ==0)

Returns false// Search failed.

}

Invalid purchase (ook&boo, char BuyNum[])

{/1.Edit warehousing: if you buy a new book, if it already exists in the book account, increase its inventory (including

//including total inventory and current inventory), if the book does not exist, add a book to the book, and the total inventory and current inventory are both 1.

If(BinarySearch(boo, BuyNum)) // If there is this book in the stacks.

{

Shh [middle]. totnum++; //Total inventory plus 1

Shh [middle]. nownum++; //Current inventory plus 1

Printf ("Received successfully. \ n ");

Printf ("The information of this book in the library has been changed. Book %s with number %s is written by %s and published by% s. The current total inventory is %d and the current inventory is% d, \n ",boo[mid]. num,boo[mid]。 Name, boo[mid]. auth,boo[mid]。 Pub, boo[mid] Tottenham, boo[mid] NowNum);

}

If (! BinarySearch(boo,BuyNum))

{

int I;

For(I = total; Me> mid-term and mid-term. & total; I-)// Insert it in the right place and keep it in order.

boo[I]= boo[I- 1]; //Vacant the insertion position

Printf ("This book is not in the library. To create a new bibliography, please fill in the details of the whole book. \ n ");

strcpy(boo[i]。 num,BuyNum);

Printf ("quantity of books purchased:");

Scanf(" %d ",&shh [me]. NowNum);

Shh [me]. TotNum=boo[i]。 Nome;

Printf ("The name of the book is");

Scanf(" %s ",&shh [me]. Name);

Printf ("The author of this book is");

Scanf(" %s ",&shh [me]. auth);

Printf ("The publisher of this book is");

Scanf(" %s ",&shh [me]. pub); //complete information

Shh [me]. next = NULL

total++; //Total amount+1

Printf ("The information in this book has been added. Book %s with number %s is written by %s and published by% s. The current total inventory is %d, and the current inventory is% d, \n ",boo[i]. num,boo[i]。 Name, boo[i]. auth,boo[i]。 pub,boo[i]。 Tottenham, shh [me]. NowNum);

Printf ("Received successfully. \ n ");

}

}

Invalid deletion (ook&boo, char DeleteNum[])

{//2. Clearing inventory: A book has no retained value, so it will be written off from the book.

If (method of bisection) = = false || Total = = 0)//If there are no books,

Printf ("The book is not in the library. \ n ");

If (method of bisection, delete num))///If yes.

{

If (! Shh [middle]. Next)

{

int j;

for(j = mid; J< total; j++)

boo[j]= boo[j+ 1];

strcpy(boo[j].num,boo[j+ 1]。 num);

Strcpy(boo[j]. Name, boo[j+ 1]. Name);

strcpy(boo[j].auth,boo[j+ 1]。 auth);

strcpy(boo[j].pub,boo[j+ 1]。 pub);

boo[j]。 TotNum=boo[j+ 1]。 TotNum

boo[j]。 NowNum=boo[j+ 1]。 Nome;

Printf ("The book has been deleted successfully. \ n ");

}

Else printf ("This book has borrowers and cannot be deleted. \ n ");

}

}

Invalid borrowing (ook&boo, lend & forest, char BorrowNum[], char CaNum[])

{//3. Borrowing: If the existing inventory of a book is greater than zero, lend a book to reduce the existing inventory 1.

//and register the borrower's library card number and return period.

Bor *p,* q;

Linked list *m, * n;;

If (! Method of bisection (boo, borrownum)| | Total = = 0)// If you don't find this book,

Printf ("This book is not in the library. \ n "); //If you have this book,

If (method of bisection)//There are some in the library.

{

if(boo[mid]。 NowNum & gt0) // Check whether the current inventory is greater than 0.

{

Shh [middle]. NowNum-; //Borrow a book, 1 less.

If(boo[mid]。 Next==NULL) // If the book information shows that the book has not been borrowed.

{

m =(LinkList *)malloc(sizeof(LNode)); /distribution

Shh [middle]. next = m; //The first node of the linked list in the book information.

strcpy(m-& gt; CardNum,CaNum);

m-& gt; Next = NULL// The last node is empty.

}

Else // If someone is already borrowing this book.

{

m=boo[mid]。 Next;

while(m->; Next) // Traverse to the last node.

m = m-& gt; Next;

n =(LinkList *)malloc(sizeof(LNode)); //Allocate space and add 1 nodes.

m-& gt; next = n;

strcpy(n-& gt; CardNum,CaNum); //Record the certificate number

n->; next = NULL

}

int I = 0;

for(I = 0; Me & ltRetotali++)//

{

If (! Lin [i].CNum, CaNum))// If you already have the information of this library card,

{

P = Lin [me]. Next;

while(p->; Next) p = p-& gt;; Next; //Traverse to the last node

q =(Bor *)malloc(sizeof(Boro)); //Allocate space

p->; next = q;

strcpy(q-& gt; BNum,BorrowNum); //Record the book number

Printf ("Enter return date:");

scanf("%s ",& ampq->; RetDate);

q->; next = NULL

Printf ("The loan was successful. \ n ");

Break; //If the certificate is found, jump out of the loop.

}

}

If(i==Retotal)// If there is no information about this certificate,

{

Forest [1]. CNum,CaNum); //Record the certificate number

p =(Bor *)malloc(sizeof(Boro)); //Allocate space

Lin [I]. next = p;

strcpy(p-& gt; BNum,BorrowNum);

Printf ("Enter return date:");

scanf(" %s ",& ampp->; RetDate);

p->; next = NULL

retotal++; //Total information of library card number plus 1.

Printf ("The loan was successful. \ n ");

}

}

Else printf ("The loan failed. This book is now in stock at £ 0. \ n ");

}

}

Invalid return (ook&boo, lend & ampLin, char ReturnNum[], char BorrowerNum[])

{//4. Return: cancel the borrower's registration and change the existing number of books.

Bor *p,* q;

Linked list *m, * n;;

int flag = 0; //Set parameters

If (! BinarySearch(boo,ReturnNum)||! Total)//There are no books.

Printf ("There are no books in the library. \ n ");

If (method of bisection (boo, return num))// There are books.

{

m=boo[mid]。 Next;

If (! strcmp(m-& gt; Cardnum, borrowernum)// If it is returned by the first borrower.

{

Shh [middle]. nownum++; //Current inventory plus 1

Shh [middle]. next = m-& gt; Next; //Delete node

Free (m); //Free the space of this node.

}

other

{

while(m->; Next)//Find the borrower node of the returner.

{

If (! strcmp(m-& gt; Next-> CardNum, BorrowerNum)) // If found,

{

n = m-& gt; Next; //n is the borrower's borrowing node.

m-& gt; next = n-& gt; Next; //m points to the next node of the borrower's loan node.

Free (n); //Free up space

Shh [middle]. nownum++; //Current inventory plus 1

Break;

}

m = m-& gt; Next;

}

}

}

//Find the borrower information in the borrower table.

for(int I = 0; I & ltRetotali++)

{

If (! Lin [I].cnum, borrowernum))// If the borrower is found,

{

P = Lin [me]. Next;

If (! strcmp(p-& gt; BNum, returnnum)///If it is returned, it is the first book borrowed.

{

Lin [I]. next = p-& gt; Next; //Point to the next borrowing node

Free (p); //Release node space

Printf ("The book was returned successfully. \ n ");

flag = 1;

Break;

}

Else // not found.

{

while(p->; Next)//Find the borrowing node for returning books.

{

If (! strcmp(p-& gt; Next-> BNum, ReturnNum)) // If found.

{

q = p->; Next; //q is the borrowing node for returning books.

p->; next = q-& gt; Next; //p points to the next borrowing node.

Free (q); //Free up space

Printf ("The book was returned successfully. \ n ");

flag = 1;

Break;

}

p = p-& gt; Next;

}

}

}

}

for(int k = 0; k & ltRetotalk++)

If (! Lin [k] Next)

{

int j;

for(j = k; j & ltRetotalj++)

Lin[j]= Lin[j+ 1]; //After that, advance one place, overwriting the current information.

Lin [j].CNum, ""; //Delete the library card number

retotal-; //The number of library cards is reduced by 1

}//Delete the library card information that has not been borrowed in the current state, saving space.

If(flag==0) printf ("There is no information about this certificate. \ n ");

}

//5. Search: search by one of three query criteria: search by book number,

//Search by title and author. Note: combined search, that is, combined search with multiple conditions, may not be realized.

void search bynum(ook & amp; boo,char SeaNum[])

{//BY NUM searches by book number.

LinkList * p;

p=boo[mid]。 Next;

If (method of bisection (boo, seanum) = = false) printf ("Sorry, I can't find the book you are looking for. \ n "); //Can't find method of bisection.

Else// If found.

{

{

printf("┏━━━━━━━┳━━━━━━━┳━━━━━━━━┳━━━━━━━━┳━━━━━┳━━━━━┓\n”);

Printf ("book number ┃ title ┃ author ┃ publishing house ┃ current inventory ┃ total inventory ┃ \ n");

printf("┣━━━━━━━╋━━━━━━━╋━━━━━━━━╋━━━━━━━━╋━━━━━╋━━━━━┫\n”);

printf("┃% 14s┃% 14s┃% 16s┃% 16s┃% 10d┃% 10d┃\n",boo[mid].num,boo[mid]。 Name, boo[mid]. auth,boo[mid]。 Pub, boo[mid] Norm, shh [mid]. TotNum);

printf("┗━━━━━━━┻━━━━━━━┻━━━━━━━━┻━━━━━━━━┻━━━━━┻━━━━━┛\n”);

if(boo[mid]。 Au Suivant! = empty)

{

printf("┏━━━━━━━┓\n”);

Printf (┃\ n "of the borrowed book);

Printf("┃ library number ┃\ n ");

while(p)

{

printf("┣━━━━━━━┫\n”);

printf("┃% 14s┃\n",p->; card num);

p = p-& gt; Next;

}

printf("┗━━━━━━━┛\n”);

}

}

while(p)

{

printf(" %s ",p-& gt; card num); //The card number of the borrower is also displayed in the function of searching by book number.

p = p-& gt; Next;

}

printf(" \ n ");

}//Displays the information of the searched book.

}

void search by name(ook & amp; boo)

{//Search by name and by title.

char SeaName[20];

Printf ("Enter the title of the book you are looking for: \ n");

scanf(" %s ",& ampSeaName);

Printf ("Details of the book with this title are as follows: \ n");

for(int I = 0; I < total; i++)

{

If (strcmp (seaname,boo [i])。 Name)= = 0)// If the titles are the same.

{

Printf ("Book number:% s \ nTitle:% s \ nAuthor:% s \ nPublisher:% s \ nTotal inventory:% d \ nInventory:% d \ n \ n, boo [i]. num,boo [i]。 Name, boo [

}//Displays the information of all books that meet the information.

}

}

void search by auth(ook & amp; boo)

{//Search by author's authorization

char sea auth[20];

Printf ("Enter the author of the book you are looking for: \ n");

scanf(" %s ",& ampsea auth);

Printf ("Details of finding books matching authors are as follows: \ n");

for(int I = 0; I < total; i++)

{

If (strcmp (seaauth,boo [i])。 Auth)= = 0)// If the authors are the same.

{

Printf ("Book number:% s \ nTitle:% s \ nAuthor:% s \ nPublisher:% s \ nTotal inventory:% d \ nInventory:% d \ n \ n, boo [i]. num,boo [i]。 Name, boo [

}//Displays the information of all books that meet the information.

}

}

//6. View: You can view all books borrowed by a borrower with a library card number, as well as all expired books.

Invalid view card (ook&boo, lend & forest)

{//View all books borrowed by a borrower with a library card number.

Character number [20];

Printf ("Please enter the library card number you want to check: \ n");

scanf(" %s ",& ampnum);

bor * p;

int qqq = 0;

for(int I = 0; I & ltRetotali++)

{

If (strcmp (Lin [I].cnum, num)= = 0)// Find the certificate.

{

Printf ("The book borrowed by this credit is: \ n");

P = Lin [me]. Next;

while(p)

{

printf(" %s ",p-& gt; BNum); //Book number

p = p-& gt; Next;

}

printf(" \ n ");

qqq = 1;

Break;

}

}

If (qqq==0)

Printf ("This certificate does not exist. \ n ");

}

void view book(ook & amp; Boo, Lend & Lin)

{//View all back books.

char date[8];

bor * p;

Printf ("Please enter the date (please enter it in the format of 20060605): \ n");

Scanf(" %s ",& date);

Printf ("All overdue books are: \ n");

for(int I = 0; I & ltRetotali++)

{

P = Lin [me]. Next;

While(p)// When p is not empty

{

if(strcmp(p-& gt; RetDate,date)& lt; 0) // Expired

{

Printf ("Book number %s, certificate number %s, expiration date %s \n", p-> Lin [I]. CNum,p-& gt; RetDate);

}//Displays the information of all overdue books.

p = p-& gt; Next;

}

}

}

Void Menu() // menu

{

printf("┏—————————————————m·e·n·u————————————————┓\n”);

printf("││\ n ");

Printf("│ 1。 Editing and warehousing: new books are purchased, if they are already in the book account, │\ n ");

Printf("│ ") will increase its own inventory (including total inventory and current inventory). │\ n ");

Printf("│ If the book does not exist, add a book to the book account, │\ n ");

Printf("│ Total inventory and on-hand inventory are both input figures. │\ n ");

Printf("│ 2。 Clearing inventory: a book has no retained value and is cancelled from the book. │\ n ");

Printf("│ 3。 Borrowing: If the on-hand inventory of a book is greater than zero, lend a book to reduce the on-hand inventory 1, │\ n ");

Printf("│ ") and register the borrower's library card number and return period. │\ n ");

Printf("│ 4。 Return: Cancel the borrower's registration and change the existing number of books. │\ n ");

Printf("│ 5。 Search by ISBN. │\ n ");

Printf("│ 6。 Search by title. │\ n ");

Printf("│ 7。 Search by author. │\ n ");

Printf("│ 8。 Check all the books borrowed by a borrower with a library card number. │\ n ");

Printf("│ 9。 View all back books. │\ n ");

Printf("│ 0。 Exit the library management system. │\ n ");

printf("││\ n ");

Printf ("┗———————————\ n —— Please choose the exercise you need ———————\ n");

}

void main()

{

ook Bo

Lend to the forest;

char BNum[20];

char CNum[20];

Printf ("-Welcome to the library management system! -\ n \ n ");

int choice = 10;

int SearchCho= 10,view CHO = 10;

And (choose! =0)

{

menu(); //Display menu

Scanf(" %d ",& select);

Switch (selection)

{

Case 1:// Edit and check in

Printf ("Please enter the book number of the incoming books:");

scanf(" %s ",BNum);

Buy (Bo, bnum);

Break;

Case 2:// Clear inventory

Printf ("Please enter the book number of the book you want to clear:");

scanf(" %s ",BNum);

Delete (Bo, bnum);

Break;

Case 3://Borrowing

Printf ("Please enter the book number of the book you want to borrow: \ n");

scanf(" %s ",& ampBNum);

Printf ("Please enter the library number:");

scanf(" %s ",& ampCNum);

Borrow (Bo, Lin, BNum, cnum);

Break;

Case 4:// Return

Printf ("Please enter the book number of the book you want to return: \ n");

scanf(" %s ",& ampBNum);

Printf ("Please enter the library number:");

scanf(" %s ",& ampCNum);

Return(Bo,Lin,BNum,CNum);

Break;

Case 5://Search// Search by Book Number

Printf ("Please enter ISBN:"); //Enter the book number you want to find.

scanf(" %s ",& ampBNum);

SearchByNum(Bo,BNum);

Break;

Case 6:// Search by Title.

search by name(Bo);

Break;

Case 7:// Search by Author

SearchByAuth(Bo);

Break;

Case 8://View all the books borrowed by a library card.

Video card (Bo, Lin);

Break;

Case 9://View all overdue books.

ViewBook (blog, forest);

Break;

Case 0:// Exit the system.

Exit (0); Break;

Default: printf ("Input error! \ n "); Exit (0); Break;

}

}

}