⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bookms.c

📁 简单的实现了图书馆管理系统
💻 C
📖 第 1 页 / 共 4 页
字号:
/* ------------------------------------------------------------ */
/*     Name        : Books Management System v1.0");            */
/*     CopyRight   : (C) Lewsn 2006                             */
/*     Made by     : LiuBing  OUC  2006/12                      */
/*                                                              */
/* ------------------------------------------------------------ */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dos.h>

#include "Welcome.c"
#include "LendInf.c"

#define PAGESIZE 20   /* 每一页显示的记录数目 */
#define MaxTime 30   /* 借阅的最大天数 */
#define INFORSZ 6   /* 信息字的大小 */
#define TIMESZ 9    /* 时间字符数组的大小 */
#define BKIDSZ 6       /* BookNode结构体中各成员数组的大小 */
#define BKNAMESZ 21
#define WRITERSZ 16
#define PRESSSZ  16
#define PUBTSZ  9


struct BookNode
{
   char bkID[BKIDSZ];         /*书号,约定书号为5位*/
   char bkName[BKNAMESZ];      /*书名 */
   char writer[WRITERSZ];      /*作者 */
   char Press[PRESSSZ];      /*出版社 */
   char pubTime[PUBTSZ];     /*出版时间 */
   char curStatus;       /*0表示在馆,1表示借出(约定1位) */
   char rdID[RDIDSZ];      /* 指向借阅的读者 */
};


const char ch5[] = "*****";
const char ch8[] = "********";
const char ch15[] = "***************";
struct BookNode *cur_book_ptr = NULL;
static int book_record_Total = 0;  /*记录文件中总的记录数   */
static int book_newID_Position = 0;  /*记录下次插入记录时的起始搜索未知 */
static int book_delete_total = 0;   /* 记录记录中无效记录的个数 */
static const BKRECSIZE = sizeof(struct BookNode);



/******************************************/
/*        funtions declarations           */

/* initial and menu functions */
void Initial();
void Handle_Main_Choice(char);
void Books_Management();
void Handle_Books_Choice(char p);
void Readers_Management();
void Handle_Readers_Choice(char p);

/* functions to deal with the book datafile */
void Book_File_Init();
void Get_Book_Infor();
void Save_Book_Infor_Word();
int Load_Book_Record(int recordNum);
void Save_cur_Book_Node();

/* functions to add new book */
void Add_New_Book();
int Alloc_NewID();
int Search_Null_Record();
int Assure_Time_True(char *time);

/* functions to search a book by different ways */
void Search_Book();
void Switch_Search_Book(char c);
void Search_By_BookID();
void Search_By_Book_Fullname();
void Search_By_First_Letter();
void Search_By_Writer();
void Search_By_Press();
void Select_And_Display(int );

/* functions to display */
void Display_Book_List();
void Display_Record(struct BookNode* new_node);
void Display_curBook_Detail();
void Display_Timeout_Lending();
void Display_cur_Timeout_Lending(int p);

/* functions to deal with current book */
void Lend_Book();
void Calculate_Time(char *startTime, char *endTime);
int Days_Per_Month(int year, int mon);
void Return_Book();
void Delete_Book();

/* functions to deal with a reader record,  */
/* maybe they should be in the ReaderMS file*/
void Add_New_Reader_Lend();
void Search_Reader();
void Switch_Search_Reader(char c);
void Search_By_ReaderID();
void Search_By_Reader_Fullname();
void Display_Reader_Detail();
void Delete_Reader();

/* functions to get data from the user */
int Get_Reader_ID();
int Get_Book_ID();
int Assure_ReaderID_True(char *str);
int Assure_BookID_True(char *str);
int Delet_Verify();



/********************************/
/*    Functions' realizations   */

int main()
{
      char choice;

      Display_Welcome();  /*显示欢迎界面*/
      clrscr();
      Initial();  /* 各文件的初始化和信息字的读取*/
      clrscr();

      do
      {
          clrscr();
          printf("            MAIN MENU      \n");
          printf("  1 - Books management\n");
          printf("  2 - Readers management\n");
          printf("  3 - Lend out a book\n");
          printf("  4 - Return a book\n");
          printf("  5 - Search a book\n");
          printf("  6 - Search a reader\n");
          printf("  7 - Display the book-list\n");
          printf("  8 - Display the reader-list\n");
          printf("  9 - Display the Timeout lending\n");
          printf("  ESC - Exit!\n");
          printf("\nPlease input your choice:");
          choice = getch();
          printf("%c", choice);
          printf("\n");
          if(choice!=27)
              Handle_Main_Choice(choice);
          else
          {
              printf("Press any key to exit!\n");
              getch();
          }
      }while(choice != 27);

    return 0;
}


/*******************************************************/
/* when the system is start, to initialize the 3 files */

void Initial()
{
    /* 初始化book 文件 */
    Book_File_Init();
    Get_Book_Infor();   /*读取book文件信息字,获得书目记录总数和新插入记录位置 */
    printf("Press any key to continue!\n");
    getch();
    printf("\n");

    /* 初始化reader 文件*/
    Reader_File_Init();
    Get_Reader_Infor();  /*读取reader文件信息字,获得读者记录总数*/
    printf("Press any key to continue!\n");
    getch();
    printf("\n");

    /* 初始化lend 文件*/
    Lend_File_Init();
    printf("Press any key to continue!\n");
    getch();

    return;
}


/*************************************************/
/* to switch the Main-Menu in the main function  */

void Handle_Main_Choice(char p)
{
    switch(p)
    {
     case('1'):
         Books_Management();
         break;
     case('2'):
         Readers_Management();
         break;
     case('3'):
         Lend_Book();
         break;
     case('4'):
         Return_Book();
         break;
     case('5'):
         Search_Book();
         break;
     case('6'):
         Search_Reader();
         break;
     case('7'):
         Display_Book_List();
         getch();
         break;
     case('8'):
         Display_Reader_List();
         getch();
         break;
     case('9'):
         Display_Timeout_Lending();
         getch();
         break;
     default:
         printf("Invalid choice\n");
         printf("Press any key to continue!\n");
         getch();
         break;
    }

    return;
}


/****************************************/
/*   diaplay the Books-Management Menu  */

void Books_Management()
{
    char choice;
    do
    {
        clrscr();
        printf("       Books Management Menu \n");
        printf("  1 - Add a new book record\n");
        printf("  2 - Delete a book record\n");
        printf("  3 - Modify a book record\n");
        printf("  ESC - Return!\n");
        printf("\nPlease input your choice:");
        choice = getch();
        printf("%c\n", choice);
        if(choice!=27)
            Handle_Books_Choice(choice);
    }while(choice!=27);
    return;
}


/*****************************************/
/*  to switch the Books-Management Menu  */

void Handle_Books_Choice(char p)
{
    char ch;
    switch(p)
    {
     case('1'):
         do
         {
             Add_New_Book();
             printf("Press 'R' to add another book, else key to return!\n");
             ch = getch();
         }while((ch=='R') || (ch=='r'));
         break;
     case('2'):
         Delete_Book();
         break;
     case('3'):
         printf("Modify a book!\n");
         printf("Have not finished!\n");
         getch();
         break;
     default:
         printf("Invalid choice\n");
         printf("Press any key to continue!\n");
         getch();
         break;
    }
    return;
}


/****************************************/
/*   diaplay the Books-Management Menu  */

void Readers_Management()
{
    char choice;
    do
    {
        clrscr();
        printf("       Books Management Menu \n");
        printf("  1 - Add a new reader record\n");
        printf("  2 - Delete a reader record\n");
        printf("  ESC - Return!\n");
        printf("\nPlease input your choice:");
        choice = getch();
        printf("%c\n", choice);
        if(choice!=27)
            Handle_Readers_Choice(choice);
    }while(choice!=27);
}


/*****************************************/
/*  to switch the Books-Management Menu  */

void Handle_Readers_Choice(char p)
{
    char ch;
    switch(p)
    {
     case('1'):
         do
         {
             Add_New_Reader_Lend();
             printf("Press 'R' to add another reader, else key to return!\n");
             ch = getch();
         }while((ch=='r')||(ch=='R'));
         break;
     case('2'):
         Delete_Reader();
         break;
     default:
         printf("Invalid choice\n");
         printf("Press any key to continue!\n");
         getch();
         break;
    }
    return;
}


/**************************************************************/
/*    Initailize the book datafile, if the file is not exsit, */
/* to create a new file. If it is exsit and is a new file,    */
/* the InforWord is not exsit, to write the new InforWord to  */
/* the head of the file                                       */

void Book_File_Init()
{
     char ch;
     FILE *fp;
     printf("Book File Initializing...\n");
     delay(8000);
     if((fp=fopen("BookData.txt","r"))==NULL)
     {
         printf("Open File Error!\n");
         printf("Press any key to create a new file!");
         getch();
         printf("\n");

         /*the file is not exist, create the file   */
         if((fp=fopen("BookData.txt","w+"))==NULL)
         {
             printf("Create File Error!\n");
             return;
         }
         printf("Create file successful!\n");
         printf("The file is BookData.txt!\n");
     }

     ch = fgetc(fp);
     fclose(fp);   /*打开文件读出首字符后关闭文件   */
                   /*因为后面的Save_Book_Infor_Word()将再次打开文件 */

     if(ch==EOF)  /*若文件为空时,调用函数Save_Book_Infor_Word()在文件头写入信息字段*/
     {
         book_record_Total = 0;
         book_newID_Position = 0;
         book_delete_total = 0;
         Save_Book_Infor_Word();
     }

     return;
}


/*****************************************************/
/* to get the book record total in the data file    */
/* and to get the posotion the newID will start form  */

void Get_Book_Infor()
{
   FILE *rfp;
   int num = 0;
   char ch;
   char str1[INFORSZ];
   char str2[INFORSZ];
   char str3[INFORSZ];

   if((rfp=fopen("BookData.txt","r")) != NULL)
   {
       fgets(str1, INFORSZ, rfp);
       book_record_Total = atoi(str1);

       fgets(str2, INFORSZ, rfp);
       book_newID_Position = atoi(str2);

       fgets(str3, INFORSZ, rfp);
       book_delete_total = atoi(str3);

       num = atoi(str1) - atoi(str3);
       printf("There are  %d book records in the book file.\n", num);
   }
   else
   {
       printf("Open File Error!\n");
       return;
   }

   fclose(rfp);
   return;
}


/*********************************************************/
/* if the infor_word has been modified, this function to  */
/* save the new infor_word to the data file      */

void Save_Book_Infor_Word()
{
    FILE *wfp;
    char str1[INFORSZ], str2[INFORSZ], str3[INFORSZ];
    sprintf(str1, "%-5d", book_record_Total);
    sprintf(str2, "%-5d", book_newID_Position);
    sprintf(str3, "%-5d", book_delete_total);

    if((wfp=fopen("BookData.txt","r+")) == NULL)
    {
    printf("Open File Error!\n");
    return;
    }
    fputs(str1, wfp);
    fputs(str2, wfp);
    fputs(str3, wfp);

    fclose(wfp);
    wfp = NULL;
    return;
}


/******************************************************************/
/* to load a record from the file to the pointer(cur_book_ptr)    */

int Load_Book_Record(int recordNum)
{
    FILE *rfp;
    char ch;
    int step = 0, loop;
    struct BookNode *new_Node_ptr;

    if(recordNum >= book_record_Total)
        return -1;

    new_Node_ptr =
         (struct BookNode*)malloc(BKRECSIZE);

    if((rfp=fopen("BookData.txt","r")) == NULL)
    {
     printf("Open File Error!\n");
     return 0;
    }

    step = recordNum * (BKRECSIZE+1);
    step += (INFORSZ-1)*3;
    fseek(rfp, step, 0);
    ch = fgetc(rfp);

    if(ch != '0')
    {
     fread(new_Node_ptr, BKRECSIZE, 1, rfp);

     cur_book_ptr = new_Node_ptr;
     new_Node_ptr = NULL;
     loop = 1;
    }
    else
    {
    // printf("The book has been deleted! \n");
     loop = -1;
    }

    fclose(rfp);
    return(loop);
}


/***********************************************************/
/* when add a new book, this function to save the new node  */
/* to the data file */

void Save_cur_Book_Node()
{
    int num, step;
    FILE *wfp;
    num = atoi(cur_book_ptr->bkID);
    
    /*找到记录插入位置  */
    step = num * (BKRECSIZE+1);
    step += (INFORSZ-1)*3;
    if((wfp=fopen("BookData.txt","r+")) == NULL)
    {
     printf("Open File Error!\n");
     return;
    }
    fseek(wfp, step, 0);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -