C Implementation of Library Management System

Source: Internet
Author: User
Tags fread

/* Main functions:
1. Input basic information of new books.
2. Show All records
3. query the basic information of a book by its name.
4. Delete the withdrawn book information based on the book name.
5. sort by book name from small to large.
6. count the number of books at a price or higher.
7. list all books that have not been lent out.
Basic information: book number, book name, unit price, author, existing status, borrower name, gender, student ID, etc */
# Include <stdio. h>
# Include <stdlib. h>
# Include <time. h>
# Include <string. h>
# Include <conio. h>
Typedef int bool;

Typedef struct bookinfo
{
Char number [15];/* Product Number */
Char name [30];/* product name */
Float price;/* unit price */
Char auther [20];/* Author */
Bool isexit;/* status */
Char lendername [20];/* Name of the borrower */
Char lendersex [2];/* borrower Gender */
Char lendernum [15];/* student ID of the borrower */
} Bookinfo;

Void menu ()
{
Printf ("\ n ");
Printf ("\ t Library Management System \ n ");
Printf ("\ t \ t1: input of basic information of new books \ n ");
Printf ("\ t \ t2: show all records \ n ");
Printf ("\ t \ T3: query the basic information of a book \ n ");
Printf ("\ t \ T4: Delete the canceled book information based on the book name \ n ");
Printf ("\ t \ T5: sort by book name from small to large \ n ");
Printf ("\ t \ T6: count the number of books above a certain price \ n ");
Printf ("\ t \ T7: list all books that have not been lent out \ n ");
Printf ("\ t \ T8: Exit \ n ");
}
Void choice_1_input_new ()/* enter the new library information */
{
Char choice;
File * P;
Bookinfo newbook;

System ("CLS ");
While (1)
{
Printf ("enter the library number :");
Gets (newbook. number );

Printf ("Enter the book name :");
Gets (newbook. Name );

Printf ("input book price :");
Scanf ("% F", & newbook. Price );
While (getchar ()! = '\ N ');

Printf ("author of the input book :");
Gets (newbook. auther );

Printf ("enter the name of the Borrower :");
Gets (newbook. lendername );

Printf ("Enter the borrower's Gender :");
Gets (newbook. lendersex );

Printf ("Enter the student ID of the Borrower :");
Gets (newbook. lendernum );

Printf ("do you want to save this bibliography? (Y/n) \ n ");
Choice = getch ();
While (choice! = 'Y' & choice! = 'Y' & choice! = 'N' & choice! = 'N ')
Choice = getch ();
If (choice = 'y' | choice = 'y ')
{
Newbook. isexit = 1;/* set the book status to 1, indicating no lending */

P = fopen ("C: \ bookinfo. Data", "AB ");
Fwrite (& newbook, sizeof (bookinfo), 1, P );
Fclose (P );

Printf ("\ n this bibliography has been added to the C: \ bookinfo. data file! \ N ");
}
Else
{
Printf ("\ n this bibliography is not saved! \ N ");
}
Printf ("\ n do you want to add another bibliography? (Y/n) \ n ");
Choice = getch ();
While (choice! = 'Y' & choice! = 'Y' & choice! = 'N' & choice! = 'N ')
Choice = getch ();
If (choice = 'y' | choice = 'y ')
Continue;
Else
Break;
}
}
Void choice_2_display_all ()/* display all book information */
{
File * P;
Int N;
Bookinfo [2, 100];
Int booknumber = 0;

System ("CLS ");
P = fopen ("C: \ bookinfo. Data", "rb ");
While (! Feof (p ))
{
Fread (& bookinfo [booknumber], sizeof (bookinfo), 1, P );
Booknumber ++;
}
/* Booknumber --; Use the feof () function to read one more row. Therefore, the booknumber must be automatically subtracted once to make the number of books correct */
Fclose (P );
Booknumber --;
If (booknumber = 0)
{
Printf ("no book information! \ N ");
}
Else
{
N = 0;
Printf ("Book information: \ n ");
Printf ("Book information | borrower information \ n ");
Printf ("number name unit price author's book Status | Name gender student ID \ n ");
While (n <booknumber)
{
Printf ("%-6 S %-12 S %-8.1f %-8 S %-9d | %-12 S %-8 S %-8s \ n ",
Bookinfo [N]. Number, bookinfo [N]. Name, bookinfo [N]. Price,
Bookinfo [N]. auther, bookinfo [N]. isexit, bookinfo [N]. lendername,
Bookinfo [N]. lendersex, bookinfo [N]. lendernum );
N ++;
}
}
Printf ("\ n press any key to return to the main menu! \ N ");
Getch ();
}

Void choice_3_search_according_name ()/* display the book information based on the book name */
{
Char search [20] = "";/* search is used to store the name of the title to be queried */
Int N;
File * P;
Char choice;
Bookinfo [2, 100];
Int booknumber = 0;

System ("CLS ");
P = fopen ("C: \ bookinfo. Data", "rb ");
While (! Feof (p ))
{
Fread (& bookinfo [booknumber], sizeof (bookinfo), 1, P );
Booknumber ++;
}
Booknumber --;/* use the feof () function to read one more row. Therefore, the booknumber must be automatically subtracted once to make the number of books correct */
Fclose (P );
While (1)
{
Printf ("enter the name of the book to be queried :");
Gets (Search );
If (booknumber = 0)
{
Printf ("no information in the library! \ N ");
Printf ("press any key to return to the main menu! \ N ");
Getch ();
Break;
}/* If ends */
Else
{
For (n = 0; n <booknumber; n ++)
If (strcmp (bookinfo [N]. Name, search) = 0)
{
Printf ("the detailed information of this book is as follows: \ n ");
Printf ("Book information | borrower information \ n ");
Printf ("number name unit price author's book Status | Name gender student ID \ n ");
Printf ("%-6 S %-12 S %-8.1f %-8 S %-9d | %-12 S %-8 S %-8s \ n ",
Bookinfo [N]. Number, bookinfo [N]. Name, bookinfo [N]. Price,
Bookinfo [N]. auther, bookinfo [N]. isexit, bookinfo [N]. lendername,
Bookinfo [N]. lendersex, bookinfo [N]. lendernum );
Break;
}
If (n> = booknumber)
Printf ("No information is found in this book! \ N ");
Printf ("\ n continue to query? (Y/n) \ n ");
Choice = getch ();
While (choice! = 'Y' & choice! = 'Y' & choice! = 'N' & choice! = 'N ')
Choice = getch ();
If (choice = 'y' | choice = 'y ')
Continue;
Else
Break;
}/* Else end */
}/* While (1) end */
}
Void choice_4_delete_according_name ()/* Delete the book information based on the book name */
{
Char search [20] = "";/* search is used to store the name of the title to be deleted */
Int N, I;
File * P;
Char choice;
Bookinfo [2, 100];
Int booknumber;

System ("CLS ");
While (1)
{
Printf ("enter the name of the book to be deleted :");
Gets (Search );
P = fopen ("C: \ bookinfo. Data", "rb ");
Booknumber = 0;
While (! Feof (p ))
{
Fread (& bookinfo [booknumber], sizeof (bookinfo), 1, P );
Booknumber ++;
}
Booknumber --;/* use the feof () function to read one more row. Therefore, the booknumber must be automatically subtracted once to make the number of books correct */
Fclose (P );
If (booknumber = 0)
{
Printf ("no information in the library! \ N ");
Printf ("press any key to return to the main menu! \ N ");
Getch ();
Break;
}/* If ends */
Else
{
For (n = 0; n <booknumber; n ++)
If (strcmp (bookinfo [N]. Name, search) = 0)
{
Break;
}
If (n> = booknumber)
Printf ("No information is found in this book! \ N ");
Else
{
Printf ("are you sure you want to delete this bibliography? (Y/n )");
Choice = getch ();
While (choice! = 'Y' & choice! = 'Y' & choice! = 'N' & choice! = 'N ')
Choice = getch ();
If (choice = 'y' | choice = 'y ')
{
For (I = N; I <booknumber-1; I ++)
Bookinfo [I] = bookinfo [I + 1];
Booknumber --;

P = fopen ("C: \ bookinfo. Data", "WB ");
For (n = 0; n <booknumber; n ++)
Fwrite (& bookinfo [N], sizeof (bookinfo), 1, P );
Fclose (P );

Printf ("deleted successfully! \ N ");
}
Else
Printf ("\ n this bibliography has not been deleted! ");
}
Printf ("\ n continue to delete? (Y/n) \ n ");
Choice = getch ();
While (choice! = 'Y' & choice! = 'Y' & choice! = 'N' & choice! = 'N ')
Choice = getch ();
If (choice = 'y' | choice = 'y ')
Continue;
Else
Break;
}/* Else end */
}/* While (1) end */
}
Void choice_5_sort_according_name ()/* sort by book name */
{
File * P;
Int M, N;
Bookinfo temp;
Bookinfo [2, 100];
Int booknumber;

P = fopen ("C: \ bookinfo. Data", "rb ");
Booknumber = 0;
System ("CLS ");
While (! Feof (p ))
{
Fread (& bookinfo [booknumber], sizeof (bookinfo), 1, P );
Booknumber ++;
}
Booknumber --;/* use the feof () function to read one more row. Therefore, the booknumber must be automatically subtracted once to make the number of books correct */
Fclose (P );
If (booknumber = 0)
{
Printf ("no book information! \ N ");
}
Else
{
For (m = 0; m <booknumber-1; m ++)
For (n = m + 1; n <booknumber; n ++)
If (strcmp (bookinfo [M]. Name, bookinfo [N]. Name)> 0)
{
Temp = bookinfo [m];
Bookinfo [m] = bookinfo [N];
Bookinfo [N] = temp;
}
P = fopen ("C: \ bookinfo. Data", "WB ");
For (m = 0; m <booknumber; m ++)
Fwrite (& bookinfo [m], sizeof (bookinfo), 1, P );
Fclose (P );
Printf ("\ n to complete sorting! \ N ");
}
Printf ("press any key to return to the main menu! \ N ");
Getch ();
}

Void choice_6_display_high_price ()/* List book information by price */
{
Float price;
File * P;
Int N;
Int COUNT = 0;
Bookinfo [2, 100];
Int booknumber;

System ("CLS ");
Printf ("Enter the price :");
Scanf ("% F", & price );
P = fopen ("C: \ bookinfo. Data", "rb ");
Booknumber = 0;
While (! Feof (p ))
{
Fread (& bookinfo [booknumber], sizeof (bookinfo), 1, P );
Booknumber ++;
}
Booknumber --;/* use the feof () function to read one more row. Therefore, the booknumber must be automatically subtracted once to make the number of books correct */
Fclose (P );
If (booknumber = 0)
{
Printf ("no book information! \ N ");
}
Else
{
For (n = 0; n <booknumber; n ++)
{
If (bookinfo [N]. Price> = Price)
Count ++;
}
If (COUNT = 0)
Printf ("there are no books with a higher price than %. 1f $ in the library! ", Price );
Else
{
Printf ("the books whose prices are higher than %. 1f $ are as follows \ n", price );
Printf ("Book information | borrower information \ n ");
Printf ("number name unit price author's book Status | Name gender student ID \ n ");
For (n = 0; n <booknumber; n ++)
{
If (bookinfo [N]. Price> = Price)
Printf ("%-6 S %-12 S %-8.1f %-8 S %-9d | %-12 S %-8 S %-8s \ n ",
Bookinfo [N]. Number, bookinfo [N]. Name, bookinfo [N]. Price,
Bookinfo [N]. auther, bookinfo [N]. isexit, bookinfo [N]. lendername,
Bookinfo [N]. lendersex, bookinfo [N]. lendernum );

}
}
}
Printf ("\ n press any key to return to the main menu! \ N ");
Getch ();
}
Void choice_7_display_according_exitflag ()/* List book information based on the existing status */
{
File * P;
Int N;
Int COUNT = 0;
Bookinfo [2, 100];
Int booknumber;

System ("CLS ");
Booknumber = 0;
P = fopen ("C: \ bookinfo. Data", "rb ");
While (! Feof (p ))
{
Fread (& bookinfo [booknumber], sizeof (bookinfo), 1, P );
Booknumber ++;
}
Booknumber --;
Fclose (P );
If (booknumber = 0)
{
Printf ("No bibliography exists in the library! \ N ");
}
Else
{
For (n = 0; n <booknumber; n ++)
If (bookinfo [N]. isexit = 1)
Count ++;
If (COUNT = 0)
Printf ("all books in the library are lent out! \ N ");
Else
{
Printf ("the information of books not lent in the library is as follows: \ n ");
Printf ("Book information | borrower information \ n ");
Printf ("number name unit price author's book Status | Name gender student ID \ n ");
For (n = 0; n <booknumber; n ++)
If (bookinfo [N]. isexit = 1)
{
Printf ("%-6 S %-12 S %-8.1f %-8 S %-9d | %-12 S %-8 S %-8s \ n ",
Bookinfo [N]. Number, bookinfo [N]. Name, bookinfo [N]. Price,
Bookinfo [N]. auther, bookinfo [N]. isexit, bookinfo [N]. lendername,
Bookinfo [N]. lendersex, bookinfo [N]. lendernum );

}
}
}
Printf ("\ n press any key to return to the main menu! \ N ");
Getch ();
}

Void main ()
{
Char choice;
Clock_t TM;
File * P;
If (P = fopen ("C: \ bookinfo. Data", "rb") = NULL)
{
P = fopen ("C: \ bookinfo. Data", "WB ");
Fclose (P );
}
While (1)
{
System ("CLS ");
Menu ();
Choice = getch ();
Switch (choice)
{
Case '1': choice_1_input_new (); break;/* enter new book information */
Case '2': choice_2_display_all (); break;/* display all book information */
Case '3': choice_3_search_according_name (); break;/* display the book information based on the book name */
Case '4': choice_4_delete_according_name (); break;/* Delete the book information based on the book name */
Case '5': choice_5_sort_according_name (); break;/* sort by book name */
Case '6': choice_6_display_high_price (); break;/* List book information by price */
Case '7': choice_7_display_according_exitflag (); break;/* List book information based on the existing status */
Case '8': printf ("\ n \ t thank you for using it. Goodbye! \ N \ t press any key to exit! \ N "); getch (); return;
Default:
Printf ("\ n \ t please enter a number in the menu! (1 ~ 8 )");
TM = clock ();
While (clock () <TM + 1800 );
Break;
}
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.