C language library management system concise version

  • 2020-06-01 10:41:49
  • OfStack

The library management system of DOS interface is as follows

The program is divided into two parts: administrator operation (include books, delete books, etc.) and member operation (register, borrow books, return books, etc.);

1. Administrator operation interface

2. Member operation interface

global.h header :(only one written header file is used in the program, where all the interface functions and the required header files are stored, as well as the structure definition)


#include "iostream" 
#include "string" 
#include "fstream" 
#include "conio.h"   //getch(); 
#include "algorithm"  //sort(); 
using namespace std; 
 
#define Max_BookNum 10240   // Maximum number of books stored  
#define Max_MemberNum 10240   // Maximum membership  
 
typedef enum{// Book borrowing status  
 Borrowed=1,    // Have been borrowing  
 available=0    // The current can borrow  
}Book_Status; 
 
typedef struct{// Book information  
 char name[20];   // Title:  
 char author[20];  // The author  
 string isbn;   // Book number  
 string price;   // The price  
 string pub;    // Press.  
 string pubtime;   // Publication date  
 string addtime;   // Collection date  
 string Class;   // Classification,  
 Book_Status Book_sta; // Borrowing status , Initialize as loanable  
}Book_Message; 
 
typedef struct{// Borrower information  
 string name;  // The name  
 string sex;    // gender  
 string number;   // Student id  
 char login[20];   // Log in,   In order to A1001 start  
 char code[20];   // The login password  
 Book_Message Borrow_Book[10];  // Maximum number of books available: 10 this  
 int BorBook_Cnt;  // Number of books  
}Member_Message; 
 
 
void Show_WelMenu(); //Welcome The menu  
void Book_InfoInput(); // Book information entry  
void Book_InfoShow(Book_Message *Book_Info); // Output book information  
void Book_Search();  // Search the book   0 By title; 1 According to the author  
void Book_Delete();  // Delete book information  
void Book_InfoChange(); // Modify book information  
void Book_Sort();  // Book sorting  
void Book_RecordSave(); // Book information saving   
int Book_RecordLoad(); // Read the file  
 
 
void Admin_Login();   // Administrator login  
void Member_WelMenu();  // Member menu interface  
void Book_WelMenu();  // Administrator menu interface  
 
void Member_Login();  // Member login  
void Member_Reg();  // Registered members  
void Member_Bor();  // Borrow formalities  
void Member_Ret();  // Return the book  
void Member_Search(); // Membership information enquiry  0 By name; 1 According to the student id  
void Member_Delete(); // Membership information cancellation  
void Member_RecordSave();// Member information saving  
int Member_RecordLoad();// Member information read file  
void Member_InfoChange(); 
 
 
//void Member_BookMsgSave(); 
//int Member_BookMsgLoad(); 
void Member_MsgShow(Member_Message *Member_Msg);// Information display  
void Member_CodeAdjust();// Member changes password  

Main program: main.c file:


#include "global.h" 
int Book_Record=0;     
int Member_Record=0; 
Book_Message Book_List[Max_BookNum];   // Book registration form  
Member_Message Member_List[Max_MemberNum];  // Membership form  
 
int main() 
{ 
 char key[2]; 
 while(1){ 
  Show_WelMenu(); 
  int cmd; 
  cin>>cmd; 
  if(!cmd) 
   Admin_Login();   // Enter the administrator login interface  
  else 
   Member_Login();   // Enter the member login interface  
  
  cout<<"\t\t\t\t Whether to exit the library management system ?\ty/n\n"; 
  cin>>key; 
  if(strcmp(key,"y")==0) 
   break; 
 } 
 cout<<" The system is about to exit "<<endl; 
 getch(); 
 return 0; 
} 

The function implementation of each functional module is as follows:

1. Output interface (select the login interface)


void Show_WelMenu() 
{ 
 system("cls"); 
 cout<<"\t\t\t\t Welcome to the library management system "<<endl<<endl; 
 cout<<"\t\t\t\t0 : administrator login "<<endl; 
 cout<<"\t\t\t\t1 : member login "<<endl; 
} 

2. Administrator login program (the fixed password is selected here, and Init_Code is the initial password. You can also change your password.)


void Admin_Login() 
{ 
 short count=0; 
 char code[20]; 
 char Init_Code[10]="0"; 
 cout<<" Please enter the administrator password: "<<endl; 
 while(1){ 
  cin>>code; 
  if(strcmp(code,Init_Code)==0){ 
   break; 
  } 
  else{ 
   cout<<" The password is wrong, please enter it again "<<endl; 
   count++; 
   if(count>3){ 
    cout<<" Password error exceeds 3 Time, the program is about to exit "<<endl; 
    break; 
   } 
  } 
 } 
 Book_WelMenu();   //enter Admin Menu 
} 

3. Member login procedure (use account password to log in, if there is no account, you can register by yourself)


void Member_Login() 
{  
 Book_Record = 0; 
 Member_Record = 0; 
 Book_Record = Book_RecordLoad(); 
 Member_Record = Member_RecordLoad();  // Reads the current registry from a file  
 char code[20],login[20],Req[2]; 
 bool Login_Sta = false; 
 while(1){ // Login program, log out after success  
  if(Login_Sta) break; 
  if((Member_Record==0)){ 
   cout << "\t\t\t\t The user library is empty, please register first " << endl; 
   cout << "\t\t\t\t Press any key to register "; 
   getch(); 
   system("cls"); 
   Member_Reg();  
  } 
  else{ 
   cout << "\t\t\t\t Please enter your login number: " << endl; 
   cin>>login; 
   for(Mem_Point=0; Mem_Point<Member_Record; Mem_Point++){ 
    if((strcmp(login,Member_List[Mem_Point].login) == 0)){ 
     cout << "\t\t\t\t Please enter your password: " << endl; 
     cin >> code; 
     while(strcmp(code, Member_List[Mem_Point].code) != 0){ 
       cout << " Password mistake , Please re-enter " << endl; 
       cin >> code; 
     } 
     Login_Sta = true; 
     cout << "\t\t\t\t Login successful , Press any key to continue "; 
     break; 
    } 
   } 
   if(Mem_Point >= Member_Record){ 
    cout << "\t\t\t This user is not registered and cannot use the library management system " << endl; 
    cout << "\t\t\t\t Whether to register now ( Y/N ) " << endl; 
    cin >> Req; 
    if((strcmp(Req,"y") == 0) || (strcmp(Req,"Y") == 0)) 
     Member_Reg(); 
   } 
  } 
 } 
 getch(); 
 system("cls"); 
 Member_WelMenu(); 
 getch(); 
} 

4. File operation (read in and write, only the file operation of the member information part is posted here, and the file operation of the book is similar)


/* Export the member registry to the file */ 
void Member_RecordSave()  
{ 
 ofstream outfile; 
 outfile.open("E:\\Course\\002\\Member_Info.txt",ios::ate|ios::out); // If you have a file, empty it before you write it  
 for(int i=0;i<Member_Record;i++){ 
  outfile << Member_List[i].name <<" " << Member_List[i].sex <<" " << Member_List[i].number <<" " 
    << Member_List[i].login <<" " << Member_List[i].code << endl; 
 } 
 outfile.close(); 
} 
 
/* Import the member registry from the file */ 
int Member_RecordLoad() 
{ 
 int Member_FileLoad=0; 
 ifstream infile; 
 infile.open("E:\\Course\\002\\Member_Info.txt",ios::in); 
 string n,s,num; 
 char l[20],c[20]; 
 while(1){ 
  infile >> n >> s >> num >> l >> c;  // Import the information from the file and register the membership information  
  Member_Add(n,s,num,l,c); 
  if(infile.eof() != 0)break; 
  Member_FileLoad++; 
 } 
 infile.close(); 
 return Member_FileLoad; 
} 

5, member registration procedures (first scan the registry, if the registration has been returned failed. To register is to add member information to the file.)


// Internal function  
void Member_Add(string n,string s,string num,char* l,char* c) 
{ 
 Member_List[Member_Record].name  = n; 
 Member_List[Member_Record].sex  =s; 
 Member_List[Member_Record].number =num; 
 strcpy(Member_List[Member_Record].login,(const char*)l); 
 strcpy(Member_List[Member_Record].code,(const char*)c); 
 Member_Record++; 
} 
 
/* Membership registration procedure */ 
void Member_Reg() 
{  
 string n,s,num; 
 char l[20],c[20],choice[2]; 
 cout << "\t\t\t\t Registered members " << endl; 
 cout << "\t\t Please enter here   Name, gender, student number, login number and password " << endl; 
 cin >> n >> s >> num >> l >> c; 
 for(int i=0; i<Member_Record; i++){ 
  if(strcmp(Member_List[i].login,l) == 0){ 
   cout << "\t\t\t\t The login number already exists. Registration failed " << endl; 
   getch(); 
   system("cls"); 
   return; 
  } 
 } 
 Member_Add(n,s,num,l,c); 
 cout << "\t\t\t\t Registered successfully " << endl; 
 cout << "\t\t\t\t Whether to file or not \ty/n?" << endl;; 
 cin >> choice; 
 if((strcmp(choice,"y")==0) || (strcmp(choice,"Y") == 0)) 
  Member_RecordSave(); 
 getch(); 
 system("cls");  //clear screan 
} 

6. Member logout and password modification


/* Membership information cancellation */ 
void Member_Delete() 
{ 
 bool key=false; 
 char Del_Name[20],cmd[2]; 
 cout<<" Please enter the login number of the member you want to logout "<<endl; 
 cin>>Del_Name; 
 for(int i=0;i<Member_Record;i++){ 
  if(strcmp(Member_List[i].login,Del_Name)==0){ //found 
   key=true; 
   Member_MsgShow(&Member_List[i]); 
   cout<<" Whether you need to delete this member ,y/n?"<<endl; 
   cin>>cmd; 
   if((strcmp(cmd,"y")==0)||(strcmp(cmd,"Y")==0)){ //ensure delete 
    while(i<=Member_Record-1){ 
     Member_List[i]=Member_List[i+1]; 
     i++; 
    } 
    Member_Record--; 
    Member_RecordSave(); 
    cout<<" members  "<<Del_Name<<"  Has been cancelled "<<endl; 
    break; 
   }  
  } 
 } 
 if(!key) cout<<" The member was not found "<<endl; 
} 
 
void Member_CodeAdjust() 
{  
 char TempCode[20],choice[2],Temp[20]; 
 strcpy(Temp,Member_List[Mem_Point].code); 
 while(1){ 
  cout<<" Please enter the original password: "; 
  cin>>TempCode; 
  if(strcmp(Temp,TempCode)==0){ 
   cout<<" Please enter a new password: "; 
   cin>>TempCode; 
   cout<<" Please enter your new password again: "; 
   strcpy(Member_List[Mem_Point].code,TempCode); 
   cin>>TempCode; 
   if(strcmp(Member_List[Mem_Point].code,TempCode)==0){ 
    cout<<"\t\t\t\t Password changed successfully, the new password is "<<Member_List[Mem_Point].code<<endl;    //test 
    Member_RecordSave(); 
    break; 
   } 
   else{ 
    cout<<" The password entered twice is not 1 To you, the modification failed! "<<endl; 
    cout<<"\t\t\t\t The input Y Go ahead, enter N Give up to modify "<<endl; 
    cin>>choice; 
    if((strcmp(choice,"n")==0)||(strcmp(choice,"N")==0)) 
     return ; 
    continue; 
   } 
  } 
  cout<<" Password error, please press any key to enter again! "<<endl; 
  getch(); 
 }  
} 

Administrator operation of most procedures and member operations are similar, here only posted a book sorting operation

7. Sorting books


/*  Inner function for book sorting function call */ 
bool cmp( Book_Message a, Book_Message b ){ 
 if(strcmp( a.author, b.author ) == 0) 
  return strcmp( a.name, b.name ) < 0; 
 return strcmp( a.author , b.author ) < 0; 
} 
/* 
* @brief  Books are sorted by author's name in ascending order, or by title in ascending order if the author is the same  
* @param None 
* @retval None 
*/ 
void Book_Sort() 
{ 
 sort( Book_List, Book_List+Book_Record, cmp ); 
 cout << " Sorting done " << endl; 
 cout << " All book information is as follows " << endl; 
 cout << " Title: \t The author \t The price \t Press. \t Publication date \t Classification,   Serial number   Collection date   Could you borrow " << endl; 
 for(int i=0; i<Book_Record; i++){ 
  Book_InfoShow( &Book_List[i] ); 
 } 
 getch(); 
} 

Related articles: