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

📄 readerms.c

📁 简单的实现了图书馆管理系统
💻 C
字号:
/**********************************************/
/*         ReaderMS.c                         */
/**********************************************/

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

#define PAGESIZE 20
#define RDIDSZ 6
#define RDNAMESZ 16
#define GRADESZ 5
#define MAJORSZ 16

struct ReaderInfor
{
    char rdID[RDIDSZ];
    char Name[RDNAMESZ];
    char Grade[GRADESZ];
    char Major[MAJORSZ];
};


struct ReaderInfor *cur_reader_ptr;
static const int RDRECSIZE = sizeof(struct ReaderInfor);
static int reader_total = 0;
static int reader_newID_Position = 0;  /*记录下次插入记录时的起始搜索未知 */
static int reader_delete_total = 0;


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

void Reader_File_Init();
void Get_Reader_Infor();
void Save_Reader_Infor_Word();
int  Load_Reader_Record(int recordNum);
void Save_Reader_Node();

int Add_New_Reader();
int reader_Alloc_NewID();
int reader_Search_Null_Record();
void Get_Grade(char *instr);
int Assure_Grade_True(char *str);

void Display_cur_Reader_Record();
void Display_Reader_List();





/**************************************************************/
/*    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 Reader_File_Init()
{
     char ch;
     char str[6];
     FILE *fp;
     printf("Reader File Initializing...\n");
     delay(3000);
     if((fp=fopen("ReaderData.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("ReaderData.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_Reader_Infor_Word()将再次打开文件 */

     if(ch==EOF)  /*若文件为空时,调用函数Save_Reader_Infor_Word()在文件头写入信息字段*/
     {
         reader_total = 0;
         Save_Reader_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_Reader_Infor()
{
   FILE *rfp;
   int num;
   char ch;
   char str1[6], str2[6], str3[6];

   if((rfp=fopen("ReaderData.txt","r")) != NULL)
   {
       fgets(str1, 6, rfp);
       reader_total = atoi(str1);

       fgets(str2, 6, rfp);
       reader_newID_Position = atoi(str2);

       fgets(str3, 6, rfp);
       reader_delete_total = atoi(str3);

       num = atoi(str1) - atoi(str3);
       printf("There are  %d reader records in the reader 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_Reader_Infor_Word()
{
    FILE *wfp;
    char str1[6],str2[6],str3[6];
    sprintf(str1, "%-5d", reader_total);
    sprintf(str2, "%-5d", reader_newID_Position);
    sprintf(str3, "%-5d", reader_delete_total);

    if((wfp=fopen("ReaderData.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_reader_ptr) */

int Load_Reader_Record(int recordNum)
{
    FILE *rfp;
    char ch;
    int step = 0, loop;
    struct ReaderInfor *new_Node_ptr;
    new_Node_ptr =
         (struct ReaderInfor*)malloc(RDRECSIZE);

    if(recordNum >= reader_total)
        return -1;

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

    step = recordNum * (RDRECSIZE+1);
    step += 15;
    fseek(rfp, step, 0);

    ch = fgetc(rfp);
    if(ch != '0')
    {
        fread(new_Node_ptr, RDRECSIZE, 1, rfp);
        cur_reader_ptr = new_Node_ptr;
        new_Node_ptr = NULL;
    }
    else
        return -1;

    fclose(rfp);
    return 1;
}


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

void Save_cur_Reader_Node()
{
    int num, step;
    FILE *wfp;
    num = atoi(cur_reader_ptr->rdID);
    
    /*找到记录插入位置  */
    step = num * (RDRECSIZE+1);
    step += 15;
    if((wfp=fopen("ReaderData.txt","r+")) == NULL)
    {
     printf("Open File Error!\n");
     return;
    }
    fseek(wfp, step, 0);

    fputc('1', wfp);  /*写入记录状态标志,1表示该记录有效*/
    fwrite(cur_reader_ptr, RDRECSIZE, 1, wfp);  /*用数据块写入方式 */

    printf("Add successfully!\n");
    fclose(wfp);   /*关闭文件*/
    return;
}


/*****************************/
/*    Add a new reader       */

int Add_New_Reader()   /* 返回的新添加读者号,为添加相应的借阅记录提供位置 */
{
    int num, i, rdID;
    char c;
    char newID[RDIDSZ], instr[RDNAMESZ], str[RDNAMESZ];
    struct ReaderInfor *new_node_ptr;
    new_node_ptr =
         (struct ReaderInfor*)malloc(RDRECSIZE);

    if(new_node_ptr==NULL)
    {
        printf("WARNING:Memory Error!\n");
        return -1;
    }

    /*分配读者证号*/
    num = reader_Alloc_NewID();
    rdID = num;
    for(i=RDIDSZ-2; i>=0; i--)
    {
         newID[i] = num%10 + 48;
         num = num/10;
    }
    newID[RDIDSZ-1] = '\0';
    printf("new ReaderID: %s\n", newID);
    strcpy(new_node_ptr->rdID, newID);
     
    /*获得读者姓名*/
    printf("Please Input Reader's name(1~15 chars):\n");
    gets(instr);
    while(strlen(instr) > RDNAMESZ)
    {
       printf("Wrong input, it is too long!\n");
       printf("Please Input Reader's name(1~15 chars):\n");
       gets(instr);
    }
    sprintf(str, "%-15s", instr);
    strcpy(new_node_ptr->Name, str);

    /*获得读者年级*/
    Get_Grade(instr);
    strcpy(new_node_ptr->Grade, instr);

    /*获得德读者专业*/
    printf("Please Input Reader's Major(1~15 chars):\n");
    gets(instr);
    while(strlen(instr) > MAJORSZ)
    {
       printf("Wrong input, it is too long!\n");
       printf("Please Input Reader's Major(1~15 chars):\n");
       gets(instr);
    }
    sprintf(str, "%-15s", instr);
    strcpy(new_node_ptr->Major, str);

    cur_reader_ptr = new_node_ptr;
    new_node_ptr = NULL;

    /*询问是否添加该新书 */
    printf("Add this reader? (y/n):");
    c = getch();
    printf("%c\n", c);
    if(c=='y'||c=='Y')
    {
        Save_cur_Reader_Node(); /*保存读者信息, 写入文件*/

        free(cur_reader_ptr);    /*释放指针 */
        cur_reader_ptr = NULL;
         
        if(rdID==reader_total)  /*若记录是在最后插入的,则记录总数应该加1*/
        {                       /*新插入点为最后*/
             reader_total++;
             reader_newID_Position = reader_total;
        }
        else
        {
             reader_newID_Position = rdID+1;
             reader_delete_total--; /* 在中间插入一个新纪录,则中间的无效记录数目减一 */
        }

        Save_Reader_Infor_Word();
    }
    else
    {
        printf("Adding cancled!\n");
    }

    free(cur_reader_ptr);
    cur_reader_ptr = NULL;
    
    return rdID;
}


/***********************************************************/
/*  to allocate a new ID to the new reader automatically   */

int reader_Alloc_NewID()
{
    int num;
    int result, i;

    if(reader_newID_Position==reader_total) /*前面没有空记录,从后面依次分配*/
         num = reader_newID_Position;

    /*存在空记录,调用函数Search_Null_Record()搜索book_newID_Position后的*/
    /*第一条空记录位置作为新数添加的位置  */
    else
    {
        result = reader_Search_Null_Record();
        if(result < 0)
        {
           printf("Allocate newID Failed!\n");
           return -1;
        }
        else
           num = result;
    }
    return(num);
}


/**********************************************************************/
/*start from the book_newID_Position to search the ineffective record */
int reader_Search_Null_Record()
{
    char ch;
    FILE *rfp;
    int  step;

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

    step = reader_newID_Position * (RDRECSIZE+1);
    step += 15;
    fseek(rfp, step, 0);
    ch = fgetc(rfp);     /*读有效标志 */
    while((ch=='1')&&(reader_newID_Position<reader_total))
    {
        reader_newID_Position++;
        fseek(rfp, RDRECSIZE, 1);   /*跳过一条记录*/
        ch = fgetc(rfp);   /*读有效标志  */
    }
    fclose(rfp);

    return(reader_newID_Position);
}


/***************************************************/
/*  get the grade of the new reader from the user  */

void Get_Grade(char *str)
{
    int loop;
    char instr[GRADESZ];

    do    /* 检验输入的年级格式是否正确*/
    {
       printf("Please Input Reader's Grade(4 chars,like:2004):\n");
       gets(instr);
       loop = Assure_Grade_True(instr);
       if(loop==-1)
            printf("Wrong input!\n");
    }while(loop==-1);

    strcpy(str, instr);
    return;
}


/****************************************/
/*  to judge the grade is legal or not  */

int Assure_Grade_True(char *str)
{
    int i;
    char ch;
    if(strlen(str)!= GRADESZ-1)
        return -1;
    for(i=0; i<GRADESZ-1; i++)
    {
        ch = *(str+i);
        if(ch<48 || ch>57)
            return -1;
    }
    return 1;
}


/******************************************************/
/*  display current reader record information         */
void Display_cur_Reader_Record()
{
   printf("\t %s |", cur_reader_ptr->rdID);
   printf("  %s |", cur_reader_ptr->Name);
   printf(" %s  |", cur_reader_ptr->Grade);
   printf("  %s", cur_reader_ptr->Major);
   printf("\n");
   return;
}


/***************************************/
/*    display a list of all readers    */

void Display_Reader_List()
{
    int i, dsp_total=0;
    int loop;

    clrscr();
    if(reader_total==0)
    {
        printf("There is no reader!\n");
        return;
    }
    
    for(i=0; i<reader_total; i++)
    {
        loop = Load_Reader_Record(i);
        if(loop==-1)
            continue;
        dsp_total++;
        if(dsp_total % PAGESIZE==1)
        {
            clrscr();
            printf("\t Number|      Name        | Grade |      Major       \n");
            printf("\t-----------------------------------------------------\n");
        }
        Display_cur_Reader_Record();

        /*************释放指针(释放内存空间是很重要的)*************** */
        free(cur_reader_ptr);
        cur_reader_ptr = NULL;

        if(dsp_total % PAGESIZE==0)  /* 分页显示 */
        {
            printf("\nPress any key to the next page!\n");
            getch();
        }

    }
    printf("\n   There are  %d  readers total.\n", reader_total - reader_delete_total);
   
    return;
}





⌨️ 快捷键说明

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