图书馆管理系统 C语言实现

//图书管理系统

//具体功能包括图书的添加 删除 搜索 ,采用会员制度包含 会员的身份记录

// 编译环境 visual studio 2008  语言:C语言
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#define HEADER1 "..............................BOOK.............................................\n"
#define HEADER2 "| number   |   name        |price|    author     |publishing company  |number |\n"
#define HEADER3 "|..........|...............|.....|...............|................... |.......|\n"
#define HEADER4 "..............................Member...................................\n"
#define HEADER5 "|  number  |     name    |       telephone   |\n"
#define HEADER6 "|..........|.............|...................|\n"
#define HEADER7 "|  mnumber |  member  name |    bnumber | book name |\n"
#define HEADER8 "|..........|...............|............|...........|\n"
#define HEADER9 "|.......................borrow book..................................\n"
#define END "..........................................................................\n"
#define FORMAT "|%-10s|%-15s|%5d|%-15s|%-20s|%7d|\n"
#define FORMAT1 "|%-10s|%-15s|%-15s|\n"
#define FORMAT2 "|%-10s|%-15s|%-10s|%-15s|\n"
#define DATA p->data.num,p->data.name,p->data.price,p->data.author,p->data.pub,p->data.number
#define DATA1 p->inf.mnum,p->inf.mname,p->inf.tel

typedef struct book
{
 char num[100];    /* 图书的编号   */
 char name[150];   /* 书名    */
 int price;        /*  定价   */
 char author[150];  /* 作者  */
 char pub[200];      /*出版社*/
 int number;          /*数量 */
};
typedef struct Member
{
 char mnum[100];     /*借书人编号 */
 char mname[150];    /* 姓名 */
 char tel[150];      /* 联系电话 */
};

typedef struct borrow
{
 char mnum[100];     /* 借书人编号*/
 char mname[150];     /* 借书人*/
 char num[100];        /* 书号*/
 char name[150];        /* 书名*/
};

typedef struct node        /* 定义图书信息链表的结点结构*/
{
 struct book data;   
 struct node *next;
}Node, *Link;

typedef struct mnode         /*定义借书人链表的结点结构 */
{
 struct Member inf;
 struct mnode *next;
}Mnode, *Mlink;

typedef struct bnode        /* 定义借书信息链表的结点结构*/
{
 struct borrow binf;
 struct bnode *next;
}Bnode,  *Blink;

void stringinput(char *t, int lens, char *notice)
{
 char n[50];
 do
 {

  printf(notice);
  scanf("%s", n);
  if(strlen(n)>lens)
   printf("\nexceed the required lenth!\n");
 }
   while(strlen(n)>lens);
 strcpy(t, n);
}
int numberinput(char *t)
{
 int n;
 printf(t);
 scanf_s("%d", &n);
 return n;
}
void Disp(Link l)  /* 显示链表中存储的图书记录*/
{
 Node *p;
 p = l->next;
 if (!p)             /*p=NULL 则说明暂无记录*/
 {
  printf("\n= = =>Not record!\n");
  getchar();
  return;
 }
 printf("\n\n");
 printf(HEADER1);
 printf(HEADER2);
 printf(HEADER3);
 while (p)           /*逐条输出链表中存储的图书信息*/
 {
  printf(FORMAT, DATA);
  p = p->next;
  printf(HEADER3);
 }
 getchar();
}
void menu()   /* 定义的主界面*/
{
 printf("\n*********************ADDRESS LIST********************\n");
 printf("\n* 1:Add book;         2: delete book;               *\n");
 printf("\n* 3:search book       4: modify record              *\n");
 printf("\n* 5:Add member        6:borrow book                 *\n");
 printf("\n* 7:savebook          8: save member                *\n");
 printf("\n* 9:save borrow      10:restart system              *\n");
 printf("\n* 0: quit system                                    *\n");
 printf("\n*****************************************************\n");
}
void Wrong()    /* 错误显示函数*/
{
 printf("wrong\n");
}
void Mdisp(Mlink l)   /*显示存储的借书人的记录*/
{
 Mnode *p;
 p= l->next;
 if (!p)           /*p=NULL则说明无记录*/
 {
  printf("\n= == = =>Not code!\n");
  getchar();
  return;
 }
 printf("\n\n");
 printf(HEADER4);
 printf(HEADER5);
 printf(HEADER6);

 while (p)       /*逐条输出链表中存储的借书人信息*/
 {
  printf(FORMAT1,DATA1);
  p = p->next;
  printf(HEADER6);
 }
 getchar();
}

void Add(Link l)      /* 增加图书的记录*/
{
 system("color b2");
 Node *p, *r, *s;
 char ch, flag = 0, num[10];
 r = l;
 s = l->next;
 system("cls");
 Disp(l);                   /*先输出已有的图书信息*/
 while (r->next != NULL)
  r = r->next;
 while (l)
 {
  while (l)
  {
   stringinput(num, 10, "input number(press '0' return menu):");
                                                       /* 输入书号*/
   flag = 0;
   if (strcmp(num, "0") == 0)        /*输入0退出操作,返回主界面*/
   {
    return;
   }
   s = l->next;
   while (s)
   {                                            /*查询输入的书号是否已经存在*/
    if (strcmp(s->data.num, num) == 0)
    {
     flag = 1;
     break;
    }
    s = s->next;
   }
  if (flag == 1)                     /* 提示用户是否重新输入*/
  {
   getchar();
   printf("=====>The number %s is existing,try again?(y/n):", num);
   scanf_s("%c", &ch);
   if (ch == 'y' || ch == 'Y')
    continue;
   else
    return;
  }
  else
  {
   break;
  }
 }
 p = (Node*)malloc(sizeof(Node));   /* 申请内存空间*/
 if (!p)
 {
  printf("\n allocate memory failure");     /*如果没有申请到,输出提示信息*/
  return;                            /*返回主界面*/
 }
 strcpy(p->data.num,num);                 /*将字符串拷贝到p->data.num中*/
 stringinput(p->data.name, 100, "Name");  /* 输入图书名称到p->data.name中*/
 p->data.price = numberinput("price:");    /*输入定价到p->data.price中*/
 stringinput(p->data.author, 100, "author"); /* 输入作者的名称 p->data.author */
 stringinput(p->data.pub, 100, "publish complay:"); /*输入出版社名到 p->data.pub中*/
 p->data.number = numberinput("number:");   /*输入图书数量到 p->data.number*/
 p->next = NULL;
 r->next = p;          /*将新结点插入链表中*/
 r = p;
}
return;
}

void Addmember(Mlink l)   /*增加借书人的记录*/
{
 system("color f3");
 Mnode *p, *r, *s;
 char ch, flag = 0, mnum[10];
 r = l;
 s= l->next;
 system("cls");
 Mdisp(l);          /*先输出已有的借书人的信息*/
 while (r->next != NULL)
  r = r->next;      /* 将指针移至链表的最末尾,准备添加纪录*/
 while (l)      /* 可输出多条记录,输入0时退出添加的操作*/
 {
  while (l)
  {
   stringinput(mnum, 10, "input number(press '0' return menu):");
                                                               /* 输入借书人的编号*/
   flag = 0;
   if (strcmp(mnum, "0") == 0)
                        /* 输入0退出操作,返回主界面*/
   {
    return;
   }
   s = l->next;
   while (s)
                 /*查询输入的借书人的号码是否已经存在*/
   {
    if (strcmp(s->inf.mnum, mnum) == 0)
    {
     flag = 1;
     break;
    }
    s = s->next;
   }
   if (flag == 1)
                      /* 提示用户是否重新输入*/
   {
    getchar();
    printf("=====>The number %s is existing,try again?(y/n):", mnum);
    scanf_s("%c", &ch);
    if (ch == 'y' || ch == 'Y')
     continue;
    else
     return;
   }
   else
   {
    break;
   }   
  }
  p = (Mnode*)malloc(sizeof(Mnode));  /*申请内存空间*/
  if (!p)
  {
   printf("\n allocate memory failure");   /* 如果没有申请到,输出提示信息*/
   return;                                   /* 返回主界面*/
  }
  strcpy_s(p->inf.mnum,sizeof(p->inf.mnum), mnum);  /*将字符串拷贝到p->inf.mnum中*/
  stringinput(p->inf.mname, 10, "Mname");     /*将借书人的名字拷贝到 p->inf.mname中*/
  stringinput(p->inf.tel, 15, "telephone");  /* 将借书人的号码拷贝到 p->inf.tel中*/
  p->next = NULL;
  r->next = p;  /*将新结点插入链表*/
  r = p;
 }
 return;
}
Node *Locate(Link l, char *s, char*t)
{
 printf(t);
 Link p;
 p = l;
 while (p != NULL)
 {
  if (strcmp(p->data.num, s)==0)
  return p;
  p=p->next;
 }
 
}
void printdata(Node *s)
{
 Node *p;
 p = s;
 printf(FORMAT, DATA);
}
void search(Link l)    /* 图书查询 */
{
 system("color 91");
 int select;
 char searchinput[20];
 Node *p;
 if (!l->next)
                  /* 若链表为空*/
 {
  system("cls");
  printf("\n=========>No record!\n");
  getchar();
  return;
 }
 system("lcs");
 printf("\n\t1 search by number \n\t2search by name \n");
 printf("   please choice[1,2]:");
 scanf_s("%d", &select);
 if (select == 1)   /* 按书号查询*/
 {
  stringinput(searchinput, 10, "input the extsiting student number:");
  p = Locate(l, searchinput, "num");
  if (p)       /* p!=NULL*/
  {
   printdata(p);
   printf(END);
   printf("press any key to return");
   getchar();
  }
  else
   printf("No find\n");
  getchar();
 }
 else if (select == 2)
                     /*按书号查询*/
 {
  stringinput(searchinput, 15, "input the existing book name:");
  p = Locate(l, searchinput, "name");
  if (p)
  {
   printdata(p);
   printf(END);
   printf("press any key to return");
   getchar();
  }
  else
   printf("No find\n");
  getchar();
 }
 else
  printf("wrong");
 getchar();
}
void Del(Link l)    /* 删除指定的图书记录*/
{
 system("color 70");
 int sel;
 Node *p, *r;
 char findmess[20];
 if (!l->next)
 {
  system("cls");
  printf("\n====>No record!\n");
  getchar();
  return;
 }
 system("cls");
 Disp(l);
 printf("\n     ======>1 Delete by number  ======>2Delete by name:");
 printf("Please choice [1,2]");
 scanf_s("%d", &sel);
 if (sel == 1)
 {
  stringinput(findmess, 10, "input the existing student number:");
  p = Locate(l, findmess, "num");
  if (p)
  {
   r = l;
   while (r->next != p)
    r = r->next;
   r->next = p->next;
   free(p);       /*将p所指的结点从链表中去除,释放内存*/
   printf("\n == == = >delete success!\n");
   getchar();
  }
  else
   printf("No find");
  getchar();
 }
 else if (sel == 2)    /* 按书号查询*/
 {
  stringinput(findmess, 15, "input the exsting book name");
  p = Locate(l, findmess, "name");
  if (p)
  {
   r = l;
   while (r->next != p)
    r = r->next;
   r->next = p->next;
   free(p);
   printf("\n====>delete success!\n");
   getchar();
  }
  else
   printf("No find\n");
  getchar();
 }
 else
  printf("wrong\n");
 getchar();
}

void Modify(Link l)    /* 修改图书信息*/
{
 Node *p;
 char findmess[20];
 if (!l->next)
 {
  system("cls");
  printf("\n======> No book record!\n");
  getchar();
  return;
 }
 system("cls");
 printf("modify book record!\n");
 Disp(l);
 stringinput(findmess, 10, "input the exsting book record:"); /*查询到该结点*/
 p = Locate(l, findmess, "num");
 if (p)                         /* 若p!=NULL,表明已经找到该节点*/
 {
  printf("Number:%s,\n", p->data.num);
  printf("Name:%s,", p->data.name);
  stringinput(p->data.name, 15, "input book name:");
  printf("price:%d,", p->data.price);
  p->data.price = numberinput("the price of book");
  printf("Author:%s,", p->data.author);
  stringinput(p->data.author, 15, "Author:");
  printf("Publishing company:%s", p->data.pub);
  stringinput(p->data.pub, 15, "Publishing company:");
  printf("number:%d,", p->data.number);
  p->data.number = numberinput("the number of book:");
  printf("\n======>modify success!\n");
  Disp(l);
 }
 else
  printf("No find");
 getchar();
}

void Save(Link l)    /* 将图书信息数据存盘 */
{
 FILE *fp;
 Node *p;
 int count = 0;
 fp = fopen("d:\\book", "wb");   /* 以只写方式打开二进制文件 */
 if (fp == NULL)
 {                                 /* 打开文件失败 */
  printf("\n=====>open file error!\n");
  getchar();
  return;
 }
 p = l->next;

 while (p)
 {
  if (fwrite(p, sizeof(Node), 1, fp) == 1)
  {                                    /* 写记录到磁盘文件中 */
   p = p->next;
   count++;
  }
  else
  {
   break;
  }
 }
 if (count > 0)
 {
  getchar();
  printf("\n\n\n\t save file complete,total saved's record number is:%d\n", count);
  getchar();
 }
 else
 {
  system("cls");
  printf("the current link is empty,no student record is saved!\n");
  getchar();
 }
 fclose(fp);
}

void Savemember(Mlink l)   /* 将借书人的数据存盘*/
{
 FILE *fp;
 Mnode *p;
 int count = 0;
 fp = fopen("d:\\member", "wb");   /* 以只写方式打开二进制文件*/
 if (fp == NULL)
 {                                  /* 打开文件失败 */
  printf("\n=====>open file error!\n");
  getchar();
  return;
 }
 p = l->next;

 while (p)
 {
  if (fwrite(p, sizeof(Mnode), 1, fp) == 1)
                                  /* 写记录到磁盘文件 */
  {
   p = p->next;
   count++;
  }
  else
  {
   break;
  }
 }
 if (count > 0)
 {
  getchar();
  printf("\n\n\n\t save file complete,total saved's record number is:%d\n", count);
  getchar();
 }
 else
 {
  system("cls");
  printf("the current link is empty,no member record is saved!\n");
  getchar();
 }
 fclose(fp);
}

void Saveoi(Blink l)   /* 将借书信息存盘 */
{
 FILE *fp;
 Bnode *p;
 int count = 0;
 fp = fopen("d:\\jieshu", "wb");   /*只写方式打开二进制文件 */
 if (fp == NULL)
 {                                /*打开文件失败*/
  printf("\n=====>open file error!\n");
  getchar();
  return;
 }
 p = l->next;

 while (p)
 {
  if (fwrite(p, sizeof(Bnode), 1, fp) == 1)  /*写记录到磁盘文件中*/
  {
   p = p->next;
   count++;
  }
  else
  {
   break;
  }
 }
 if (count > 0)
 {
  getchar();
  printf("\n\n\n\t save file complete,total saved's record number is:%d\n", count);
  getchar();
 }
 else
 {
  system("cls");
  printf("the current link is empty,no student record is saved!\n");
  getchar();
 }
 fclose(fp);
}
void borrow(Link l, Mlink m, Blink b)
{
 Mnode *p;
 Node *q;
 Bnode *t, *s, *k;
 char number[10], booknum[10];
 system("cls");
 t = b->next;

 if (!t)
 {
  printf("\n=====>Not record!\n");
 }
 printf("\n\n");
 printf(HEADER9);
 printf(HEADER7);
 printf(HEADER8);

 while (t)
 {
  printf(FORMAT2, t->binf.mnum, t->binf.mname, t->binf.num, t->binf.name);
  t = t->next;
  printf(HEADER8);
 }
 while (l)
 {
  s = b;
  p = m->next;
  q = l->next;
  while (s->next != NULL)
   s = s->next;
  stringinput(number, 10, "please input the number of member:");
                      /* 输入借书人的编号*/
  if (strcmp(number, "0") == 0)
   break;
  do
  {
   if (strcmp(p->inf.mnum, number) == 0) /* 查看会员号是否存在*/
    break;
   else
    p = p->next;
  }
  while (p != NULL);
  stringinput(booknum, 10, "please input the number of book:");  /*输入书号*/

  do
  {
   if (strcmp(q->data.num, booknum) == 0)  /* 查看书号是否存在 */
    break;
   else
    q = q->next;
  }
  while (q != NULL);
  if (p == NULL)
  {
   printf("you are not a member!");
   return;
  }
  else
   if (q == NULL)
   {
   printf("the book is not exist!");
   return;
   }
   else
   {
    if (q->data.number != 0)
    {
     q->data.number--;       /* 申请内存空间*/
     k = (Bnode*)malloc(sizeof(Bnode));
     if (!k)
     {
      printf("\n allocate memory failure");
               /* 如没有申请到,输入提示信息 */
      return;
     }
     strcpy(k->binf.num, q->data.num);
     strcpy(k->binf.name, q->data.name);
     strcpy(k->binf.mnum, p->inf.mnum);
     strcpy(k->binf.mname, p->inf.mname);
     k->next = NULL;
     s->next = k;    /* 将新结点插入链表 */
     s = k;
    }
    else
     printf("no book");

   }
 }
 Saveoi(b);          /* 写记录到磁盘文件 */
 getch();
}

void exit()  /* 退出系统 */
{
 menu();
}

void main()
{
  system("color a1");
 Link l;
 Mlink m;        /*定义链表*/
 Blink b;
 FILE *fp;        /* 文件指针 */
 int select;
 char ch;
 int count = 0;
 Node *p, *r;
 Mnode *q, *t;
 Bnode *s, *k;
 b =(Bnode*)malloc(sizeof(Bnode));
 if (!b)
 {
  printf("\n allocate memory faliure "); /*如果没有申请到,打印提示信息*/
  return;                /*返回主界面*/
 }
 b->next = NULL;
 k = b;

 fp = fopen("d:\\borrow","ab+");
 if (fp == NULL)
 {
  printf("\n =====>can not open file!\n");
  exit(0);
 }
 while (!feof(fp))
 {
  s = (Bnode*)malloc(sizeof(Bnode));
  if (!s)
  {
   printf("memory malloc failure!\n"); /*没有申请成功*/
   exit(0);
  }
  if (fread(s, sizeof(Bnode), 1, fp) == 1)  /*从文件中读取借书的信息记录*/
  {
   s->next = NULL;
   k->next = s;
   k = s;
  }
 }
 fclose(fp);
 m = (Mnode*)malloc(sizeof(Mnode));
 if (!m)
 {
  printf("\n allocate memory failure ");  /*如果没有申请到,输出提示信息*/
  return;                              /*返回主界面*/
 }
 m->next = NULL;
 t = m;

 fp = fopen("d:\\member", "ab+");
 if (fp == NULL)
 {
  printf("\n=======>can't open file!\n");
  exit(0);
 }

 while (!feof(fp))
 {
  q = (Mnode*)malloc(sizeof(Mnode));
  if (!q)
  {
   printf("memory malloc failure!\n");   /*没有申请成功*/
   exit(0);
  }
  if (fread(q, sizeof(Mnode), 1, fp) == 1)  /*从文件中读取借书人的信息记录*/
  {
   q->next = NULL;
   t->next = q;
   t = q;
  }
 }
 fclose(fp);
 l = (Node*)malloc(sizeof(Node));
 if (!l)
 {
  printf("\n allocate memory failure "); /* 如果没有申请到,打印提示信息*/
  return;                 /* 返回主界面*/
 }
 l->next = NULL;
 r = l;
 fp =fopen("d:\\book","ab+");
 if (fp == NULL)
 {
  printf("\n =====> can't open file!\n");
  exit(0);
 }

 while (!feof(fp))
 {
  p = (Node*)malloc(sizeof(Node));
  if (!p)
  {
   printf("memory malloc faliure!\n");   /* 没有申请成功 */
   exit(0);
  }
  if (fread(p, sizeof(Node), 1, fp) == 1)   /* 从文件中读取图书信息记录*/
  {
   p->next = NULL;
   r->next = p;
   r = p;
   count++;
  }
 }
 fclose(fp);
 printf("\n =====> open file sucess,the total records number is:%d.\n", count);
 menu();
 while (l)
 {
  p = r;
  printf("\n    Please Enter your choice(0~9):");
  scanf_s("%d", &select);

  switch (select)
  {
  case 0:  exit(); break;
   case 1:  Add(l);  break;
  case 2:  Del(l);  break;
  case 3:  search(l); break;
  case 4:  Modify(l); break;
  case 5:  Addmember(m); break;
  case 6:   borrow(l, m, b); break;
  case 7:  Save(l);  break;
  case 8:Savemember(m); break;
  case 9:Saveoi(b);  break;
  case 10: main(); break;
  default :
   Wrong();
   getchar();
   break;
  }
 }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值