顺序表的实现:Book_Manage_System

打印 上一主题 下一主题

主题 984|帖子 984|积分 2952

一、问题引入

图书信息管理系统:
出版社有一些图书数据保存在一个文本文件book.txt 中,为简单起见,在此假设每种图书只包括三部分信息:ISBN (书号)、书名和价格,文件中的部分数据如图2.1 所示。现要求实现一个图书信息管理系统,包括以下6个具体功能。
(1) 查找:根据指定的ISBN 或书名查找相应图书的有关信息, 并返回该图书在表中的位置序号。
(2) 插入:插入一种新的图书信息。
(3) 删除:删除一种图书信息。
(4) 修改:根据指定的ISBN, 修改该图书的价格。
(5) 排序:将图书按照价格由低到高进行排序。
(6) 计数:统计图书表中的图书数量
具体实现:
图书数据由用户输入,功能(5)暂不实现
二、解决过程

2-1 数据格式定义


  • 顺序表的定义
  1. #define OK        0
  2. #define ERROR    -1
  3. #define OVERFLOW -2
  4. #define MAXSIZE 10000 //顺序表的最大长度
  5. typedef struct
  6. {
  7.         char   name[50]; // 书名
  8.         double price;    // 单价
  9.         char   isbn[20]; // ISBN码
  10. }Book_T;
  11. typedef Book_T ElemType;
  12. typedef struct
  13. {
  14.         ElemType *base; // 存储空间的基地址
  15.         int length;     // 当前长度
  16. }SqList_T;          // 顺序表的结构类型为 Sqlist_T
复制代码
2-2 功能实现


  • 顺序表的初始化
  1. int list_init(SqList_T *sq_list_pt)
  2. {
  3.         sq_list_pt->base = (ElemType *)malloc(MAXSIZE * sizeof(ElemType));
  4.         if (sq_list_pt == NULL)
  5.                 exit(OVERFLOW);
  6.         memset(sq_list_pt->base, 0, MAXSIZE * sizeof(ElemType));
  7.         sq_list_pt->length = 0;
  8.         return OK;
  9. }
复制代码

  • 顺序表的销毁
  1. void list_destory(SqList_T *sq_list_pt)
  2. {
  3.         if (sq_list_pt->base != NULL)
  4.                 free(sq_list_pt->base);
  5.         sq_list_pt->base = NULL;
  6.         sq_list_pt->length = 0;
  7. }
复制代码

  • 顺序表的查找
  1. ElemType * element_locate(SqList_T sq_list_t, const char *isbn, int *idx)
  2. {
  3.         ElemType *p_elem = NULL;
  4.         *idx = -1;
  5.         for (int i = 0; i < sq_list_t.length; i++)
  6.         {
  7.                 if (0 == strcmp(sq_list_t.base[i].isbn, isbn))
  8.                 {
  9.                         p_elem = &sq_list_t.base[i];
  10.                         *idx = i+1;
  11.                         break;
  12.                 }
  13.         }
  14.         return p_elem;
  15. }
复制代码

  • 顺序表的插入
  1. int list_insert(SqList_T *sq_list_pt, ElemType elem)
  2. {
  3.         // 判断顺序表是否到达最大长度
  4.         if (sq_list_pt->length == MAXSIZE)
  5.                 return ERROR;
  6.         int idx = sq_list_pt->length;
  7.         sq_list_pt->base[idx] = elem;
  8.         (sq_list_pt->length)++;
  9.         return OK;
  10. }
复制代码

  • 顺序表的删除
  1. int list_delete(SqList_T *sq_list_pt, const char *isbn)
  2. {
  3.         // 判断顺序表是否为空
  4.         if (sq_list_pt->length == 0)
  5.                 return ERROR;
  6.         int idx = 0; // idx 从1开始
  7.         if (element_locate(*sq_list_pt, isbn, &idx) != NULL)
  8.         {
  9.                 for (int i = idx; i <= sq_list_pt->length; i++)
  10.                 {
  11.                         sq_list_pt->base[i-1] = sq_list_pt->base[i];
  12.                 }
  13.         }
  14.         (sq_list_pt->length)--;
  15.         return OK;
  16. }
复制代码

  • 顺序表的遍历
  1. int list_traverse(SqList_T sq_list_t)
  2. {
  3.         printf("ISBN                          "
  4.                    "书名                          "
  5.                    "单价                           \n");
  6.         if (sq_list_t.length == 0)
  7.         {
  8.                 printf("图书馆书籍为空\n");
  9.                 return ERROR;
  10.         }
  11.         for (int i = 0; i < sq_list_t.length; i++)
  12.         {
  13.                 printf("%-30s%-30s%-30.2f\n", sq_list_t.base[i].isbn,
  14.                         sq_list_t.base[i].name, sq_list_t.base[i].price);
  15.         }
  16.         return OK;
  17. }
复制代码

  • 顺序表的修改
  1. int list_update(SqList_T *sq_list_pt, const char *isbn, double price)
  2. {
  3.         // 判断顺序表是否为空
  4.         if (sq_list_pt->length == 0)
  5.                 return ERROR;
  6.         ElemType *p_elem = NULL;
  7.         int idx = 0; // idx 从1开始
  8.         p_elem = element_locate(*sq_list_pt, isbn, &idx);
  9.         if (p_elem == NULL)
  10.                 return ERROR;
  11.         p_elem->price = price;
  12.         return OK;
  13. }
复制代码
2-3 main()
  1. int main(void)
  2. {
  3.         int num_of_book = 0;
  4.         Book_T book_arr[MAXSIZE] = {0};
  5.         SqList_T sq_list_t = {0};
  6.         list_init(&sq_list_t);
  7.         printf("请输入书籍数量:");
  8.         scanf("%d", &num_of_book);
  9.         printf("\n");
  10.         for (int i = 0; i < num_of_book; i++)
  11.         {
  12.                 printf("请输入第%d本的信息\n", i+1);
  13.                 printf("书籍名称:");
  14.                 scanf("%50s", book_arr[i].name);
  15.                 printf("书籍单价:");
  16.                 scanf("%lf", &book_arr[i].price);
  17.                 printf("书籍ISBN:");
  18.                 scanf("%20s", book_arr[i].isbn);
  19.                 printf("\n");
  20.         }
  21.         // 插入
  22.         for (int i = 0; i < num_of_book; i++)
  23.         {
  24.                 list_insert(&sq_list_t, book_arr[i]);
  25.         }
  26.         // 遍历打印所有书籍
  27.         printf("打印图书馆所有书籍清单:\n");
  28.         list_traverse(sq_list_t);
  29.         printf("\n");
  30.         // 删除
  31.         char isbn[20] = {0};
  32.         printf("请输入删除书籍的ISBN码:");
  33.         scanf("%20s", isbn);
  34.         list_delete(&sq_list_t, isbn);
  35.         printf("打印图书馆所有书籍清单:\n");
  36.         list_traverse(sq_list_t);
  37.         printf("\n");
  38.         // 查找
  39.         ElemType *p_elem = NULL;
  40.         char book_info[300] = {0};
  41.         int idx = 0;
  42.         printf("请输入查找数据的ISBN码:");
  43.         scanf("%20s", isbn);
  44.         p_elem = element_locate(sq_list_t, isbn, &idx);
  45.         if (NULL != p_elem)
  46.         {
  47.                 sprintf(book_info, "{"name":"%s","isbn":"%s","price":%.2f}",
  48.                         p_elem->name, p_elem->isbn, p_elem->price);
  49.                 printf("%s\n", book_info);
  50.         }
  51.         printf("\n");
  52.         // 修改
  53.         double price = 0;
  54.         printf("请输入书籍的ISBN码:");
  55.         scanf("%20s", isbn);
  56.         printf("请输入书籍现在的价格:");
  57.         scanf("%lf", &price);
  58.         if (ERROR == list_update(&sq_list_t, isbn, price))
  59.         {
  60.                 printf("价格修改失败\n");
  61.         }
  62.         printf("打印图书馆所有书籍清单:\n");
  63.         list_traverse(sq_list_t);
  64.         printf("\n");
  65.         // 释放内存空间
  66.         list_destory(&sq_list_t);
  67.         return 0;
  68. }
复制代码

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

慢吞云雾缓吐愁

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表