数据结构之图书管理系统

简介: 数据结构之图书管理系统

绪论:

   昨晚看了女友老师的数据结构题目要求,觉得你们的题还是很有意思的,晚上花了两小时纯写了一下代码,救济一下18级数媒的小伙伴。因为这道题你们在网上找不到答案!老师题目要求还是比较刁钻的。


令狐助教帮你们分析分析

   答案我会贴出来,但是看你们有没有能力把它组织起来了,放在项目里执行。但答案我肯定都会写在这篇文章里。我会按我写它时的思路来介绍这个答案,希望大家从答案里学到东西。


第一文件结构创建:

我们需要在源文件创建:

  • bookmis.cpp
  • bookmis.h
  • main.cpp
  • status.h

第二打通文件脉络:

首先从main.cpp开始:

#include <iomanip>
#include "string"
#include"bookmis.h"
#include"status.h"
#include<iostream>

接着从bookmis.cpp引入:

#include"bookmis.h"
#include<iostream>
using namespace std;
void LocateBook(BookList L)
{
  Book e;
  int i;
  char n = 0;
  while (1)
  {
    std::cout << "输入1按书号查找,输入2按书名查找,输入3按作者名查找,输入4按序号查找,输入#返回上一级:" << endl;
    std::cin >> n;
    if (n == '#')
      break;
    if (n == '1')
    {
      std::cout << "请输入要查找的书号:";
      std::cin >> e.isbn;
      for (i = 0; i < L.length; i++)
      {
        if (strcmp(L.elem[i].isbn, e.isbn) == 0)
        {
          std::cout << L.elem[i].isbn << "  " << L.elem[i].name << "  " << L.elem[i].author << endl;
          break;
        }
      }
      if (i >= L.length)
        std::cout << "查无此书!请查看输入是否正确" << endl;
    }
    if (n == '2')
    {
      std::cout << "请输入要查找的书名:";
      std::cin >> e.name;
      for (i = 0; i < L.length; i++)
      {
        if (strcmp(L.elem[i].name, e.name) == 0)
        {
          std::cout << L.elem[i].isbn << "  " << L.elem[i].name << "  " << L.elem[i].author << endl;
          break;
        }
      }
      if (i >= L.length)
        std::cout << "查无此书!请查看输入是否正确" << endl;
    }
    if (n == '3')
    {
      std::cout << "请输入要查找的作者:";
      std::cin >> e.author;
      for (i = 0; i < L.length; i++)
      {
        if (strcmp(L.elem[i].author, e.author) == 0)
        {
          std::cout << L.elem[i].isbn << "  " << L.elem[i].name << "  " << L.elem[i].author << endl;
          break;
        }
      }
      if (i >= L.length)
        std::cout << "查无此书!请查看输入是否正确" << endl;
    }
    if (n == '4')
    {
      std::cout << "请输入要查找的序号:";
      std::cin >> i;
      if (i <= L.length)
      {
        std::cout << L.elem[i - 1].isbn << "  " << L.elem[i - 1].name << "  " << L.elem[i - 1].author << endl;
      }
      if (i > L.length)
        std::cout << "查无此书!请查看输入是否正确" << endl;
    }
  }
}
void Deletebyname(BookList &L)
{
  Book e;
  int i, j;
  char n = 0;
  while (1)
  {
    std::cout << "请输入要删除的书名:";
    std::cin >> e.name;
    for (i = 0; i < L.length; i++)
    {
      if (strcmp(L.elem[i].name, e.name) == 0)
      {
        for (j = i + 1; j <= L.length - 1; j++)
          L.elem[j - 1] = L.elem[j];
        --L.length;
        Printf(L);
      }
    }
  }
}

跟着一步走,补充头文件bookmis.h引入:

我们将在这个头文件里完成图书管理系统的表结构建设,并分别声明三个函数

  1. void LocateBook(BookList L);
  2. void Deletebyname(BookList &L);
  3. void Printf(BookList &L);

三个函数相当于图书管理系统的三个支柱,后序的功能都是靠这三个支柱完成的。

typedef struct
{
  char isbn[20];
  char name[50];
  char author[20];
  int price[100];
}Book;
typedef struct
{
  Book *elem;
  int length;
}BookList; 
void LocateBook(BookList L);
void Deletebyname(BookList &L);
void Printf(BookList &L);

再来最后一步头文件status.h的建立和引入:

建立状态标识符:

#pragma once
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define MAXSIZE 10000

这个时候我们已经建立并打通了

  • bookmis.cpp
  • bookmis.h
  • main.cpp
  • status.h

四个文件的脉络!

第三开始修建房梁

我们的主干main.cpp的房梁主要由以下几个结构构成:

int InitList(BookList &L);
int GetBook(BookList L, int i, Book &e);
int InsertBook(BookList &L, int i);
void Update(BookList &L);
void Create(BookList &L, int n);
void Printf(BookList &L);

如果说你看不懂这几个函数的功能:

那请你购买邓玉洁版的《算法与数据结构》

-------->点击购买

这是基础之基础,表建立的功能。

对应函数功能补充:

using namespace std;
void Printf(BookList &L);
int InitList(BookList &L)
{
  L.elem = new Book[MAXSIZE];
  if (!L.elem) exit(OVERFLOW);
  L.length = 0;
  return OK;
}
int GetBook(BookList L, int i, Book &e) 
{
  if (i<1 || i>L.length) return ERROR;
  e = L.elem[i - 1];
  return OK;
}
int InsertBook(BookList &L, int i) 
{
  int j = 0;
  if ((i<1) || (i > L.length + 1)) return ERROR;
  if (L.length == MAXSIZE) return ERROR;
  for (j = L.length - 1; j >= i - 1; j--)
    L.elem[j + 1] = L.elem[j];
  std::cout << "请输入书号、书名、作者" << endl;
  std::cin >> L.elem[i].isbn >> L.elem[i].name >> L.elem[i].author;
  ++L.length;
  Printf(L);
  return OK;
}
void Update(BookList &L)
{
  Book e;
  int i;
  char n;
  while (1)
  {
    std::cout << "请输入要修改的书的序号 ,输入0返回上一级:";
    std::cin >> i;
    if (i == 0) break;
    else if ((i<1) || (i>L.length)) std::cout << "输入的序号不正确" << endl;
    else
    {
      std::cout << L.elem[i - 1].isbn << "  " << L.elem[i - 1].name << "  " << L.elem[i - 1].author << endl;
      std::cout << "请选择要修改的对象,1:书号,2:书名,3:作者 ,输入#返回上一级" << endl;
      std::cin >> n;
      if (n == '#')
        break;
      switch (n)
      {
      case '1':std::cout << "把其修改为:";
        std::cin >> L.elem[i - 1].isbn;
        std::cout << L.elem[i - 1].isbn << "  " << L.elem[i - 1].name << "  " << L.elem[i - 1].author << endl; break;
      case '2':std::cout << "把其修改为:";
        std::cin >> L.elem[i - 1].name;
        std::cout << L.elem[i - 1].isbn << "  " << L.elem[i - 1].name << "  " << L.elem[i - 1].author << endl; break;
      case '3':std::cout << "把其修改为:";
        std::cin >> L.elem[i - 1].author;
        std::cout << L.elem[i - 1].isbn << "  " << L.elem[i - 1].name << "  " << L.elem[i - 1].author << endl; break;
      default:break;
      }
    }
  }
}
void Create(BookList &L, int n) 
{
  int i;
  for (i = 0; i < n; i++)
  {
    std::cout << "请分别输入第" << i + 1 << "本书的书号、书名、作者" << endl;
    std::cin >> L.elem[i].isbn >> L.elem[i].name >> L.elem[i].author;
    L.length++;
  }
}
void Printf(BookList &L)
{
  int i;
  std::cout << "/---------------------  存在以下图书  ----------------------------------/" << endl;
  std::cout << "   " << setw(10) << left << "序号" << setw(10) << left << "书号" << setw(30) << left << "书名" << setw(10) << left << "作者" << endl << endl;
  for (i = 0; i<L.length; i++)
  {
    std::cout << "   " << setw(10) << left << i + 1 << setw(10) << left << L.elem[i].isbn << setw(30) << left << L.elem[i].name << setw(10) << left << L.elem[i].author << endl;
  }
  std::cout << "/-----------------------------------------------------------------------/" << endl;
}

第四主房梁main的创建

我们将在这一步实现函数的调用和菜单设计,把之前的功能进行串联。

void main()
{
  BookList L;
  Book B;
  char n[20];
  int m = 0;
  char s = 0;
  InitList(L);
  std::cout << "/------------------------欢迎进入图书管理系统---------------------------/" << endl;
  std::cout << "创建图书信息" << endl;
  std::cout << "请输入书本数目:";
  std::cin >> m;
  Create(L, m);
  Printf(L);
  while (1)
  {
    std::cout << "请选择要进行的操作 :1:查找  2: 插入  3: 删除  4:修改  0: 显示:  :" << endl;
    std::cin >> s;
    switch (s)
    {
    case '0': Printf(L); break;
    case '1': LocateBook(L); break;
    case '2': InsertBook(L, L.length); break;
    case '4': Update(L); break;
    case '3':Deletebyname(L); break;
    }
  }
  system("pause");
}

第五main.cpp代码完整版

#include <iomanip>
#include "string"
#include"bookmis.h"
#include"status.h"
#include<iostream>
using namespace std;
void Printf(BookList &L);
int InitList(BookList &L)
{
  L.elem = new Book[MAXSIZE];
  if (!L.elem) exit(OVERFLOW);
  L.length = 0;
  return OK;
}
int GetBook(BookList L, int i, Book &e) 
{
  if (i<1 || i>L.length) return ERROR;
  e = L.elem[i - 1];
  return OK;
}
int InsertBook(BookList &L, int i) 
{
  int j = 0;
  if ((i<1) || (i > L.length + 1)) return ERROR;
  if (L.length == MAXSIZE) return ERROR;
  for (j = L.length - 1; j >= i - 1; j--)
    L.elem[j + 1] = L.elem[j];
  std::cout << "请输入书号、书名、作者" << endl;
  std::cin >> L.elem[i].isbn >> L.elem[i].name >> L.elem[i].author;
  ++L.length;
  Printf(L);
  return OK;
}
void Update(BookList &L)
{
  Book e;
  int i;
  char n;
  while (1)
  {
    std::cout << "请输入要修改的书的序号 ,输入0返回上一级:";
    std::cin >> i;
    if (i == 0) break;
    else if ((i<1) || (i>L.length)) std::cout << "输入的序号不正确" << endl;
    else
    {
      std::cout << L.elem[i - 1].isbn << "  " << L.elem[i - 1].name << "  " << L.elem[i - 1].author << endl;
      std::cout << "请选择要修改的对象,1:书号,2:书名,3:作者 ,输入#返回上一级" << endl;
      std::cin >> n;
      if (n == '#')
        break;
      switch (n)
      {
      case '1':std::cout << "把其修改为:";
        std::cin >> L.elem[i - 1].isbn;
        std::cout << L.elem[i - 1].isbn << "  " << L.elem[i - 1].name << "  " << L.elem[i - 1].author << endl; break;
      case '2':std::cout << "把其修改为:";
        std::cin >> L.elem[i - 1].name;
        std::cout << L.elem[i - 1].isbn << "  " << L.elem[i - 1].name << "  " << L.elem[i - 1].author << endl; break;
      case '3':std::cout << "把其修改为:";
        std::cin >> L.elem[i - 1].author;
        std::cout << L.elem[i - 1].isbn << "  " << L.elem[i - 1].name << "  " << L.elem[i - 1].author << endl; break;
      default:break;
      }
    }
  }
}
void Create(BookList &L, int n) 
{
  int i;
  for (i = 0; i < n; i++)
  {
    std::cout << "请分别输入第" << i + 1 << "本书的书号、书名、作者" << endl;
    std::cin >> L.elem[i].isbn >> L.elem[i].name >> L.elem[i].author;
    L.length++;
  }
}
void Printf(BookList &L)
{
  int i;
  std::cout << "/---------------------  存在以下图书  ----------------------------------/" << endl;
  std::cout << "   " << setw(10) << left << "序号" << setw(10) << left << "书号" << setw(30) << left << "书名" << setw(10) << left << "作者" << endl << endl;
  for (i = 0; i<L.length; i++)
  {
    std::cout << "   " << setw(10) << left << i + 1 << setw(10) << left << L.elem[i].isbn << setw(30) << left << L.elem[i].name << setw(10) << left << L.elem[i].author << endl;
  }
  std::cout << "/-----------------------------------------------------------------------/" << endl;
}
void main()
{
  BookList L;
  Book B;
  char n[20];
  int m = 0;
  char s = 0;
  InitList(L);
  std::cout << "/------------------------欢迎进入图书管理系统---------------------------/" << endl;
  std::cout << "创建图书信息" << endl;
  std::cout << "请输入书本数目:";
  std::cin >> m;
  Create(L, m);
  Printf(L);
  while (1)
  {
    std::cout << "请选择要进行的操作 :1:查找  2: 插入  3: 删除  4:修改  0: 显示:  :" << endl;
    std::cin >> s;
    switch (s)
    {
    case '0': Printf(L); break;
    case '1': LocateBook(L); break;
    case '2': InsertBook(L, L.length); break;
    case '4': Update(L); break;
    case '3':Deletebyname(L); break;
    }
  }
  system("pause");
}

第六执行结果


目录
相关文章
|
23天前
|
弹性计算 人工智能 架构师
阿里云携手Altair共拓云上工业仿真新机遇
2024年9月12日,「2024 Altair 技术大会杭州站」成功召开,阿里云弹性计算产品运营与生态负责人何川,与Altair中国技术总监赵阳在会上联合发布了最新的“云上CAE一体机”。
阿里云携手Altair共拓云上工业仿真新机遇
|
16天前
|
存储 关系型数据库 分布式数据库
GraphRAG:基于PolarDB+通义千问+LangChain的知识图谱+大模型最佳实践
本文介绍了如何使用PolarDB、通义千问和LangChain搭建GraphRAG系统,结合知识图谱和向量检索提升问答质量。通过实例展示了单独使用向量检索和图检索的局限性,并通过图+向量联合搜索增强了问答准确性。PolarDB支持AGE图引擎和pgvector插件,实现图数据和向量数据的统一存储与检索,提升了RAG系统的性能和效果。
|
20天前
|
机器学习/深度学习 算法 大数据
【BetterBench博士】2024 “华为杯”第二十一届中国研究生数学建模竞赛 选题分析
2024“华为杯”数学建模竞赛,对ABCDEF每个题进行详细的分析,涵盖风电场功率优化、WLAN网络吞吐量、磁性元件损耗建模、地理环境问题、高速公路应急车道启用和X射线脉冲星建模等多领域问题,解析了问题类型、专业和技能的需要。
2576 22
【BetterBench博士】2024 “华为杯”第二十一届中国研究生数学建模竞赛 选题分析
|
18天前
|
人工智能 IDE 程序员
期盼已久!通义灵码 AI 程序员开启邀测,全流程开发仅用几分钟
在云栖大会上,阿里云云原生应用平台负责人丁宇宣布,「通义灵码」完成全面升级,并正式发布 AI 程序员。
|
3天前
|
JSON 自然语言处理 数据管理
阿里云百炼产品月刊【2024年9月】
阿里云百炼产品月刊【2024年9月】,涵盖本月产品和功能发布、活动,应用实践等内容,帮助您快速了解阿里云百炼产品的最新动态。
阿里云百炼产品月刊【2024年9月】
|
2天前
|
存储 人工智能 搜索推荐
数据治理,是时候打破刻板印象了
瓴羊智能数据建设与治理产品Datapin全面升级,可演进扩展的数据架构体系为企业数据治理预留发展空间,推出敏捷版用以解决企业数据量不大但需构建数据的场景问题,基于大模型打造的DataAgent更是为企业用好数据资产提供了便利。
163 2
|
20天前
|
机器学习/深度学习 算法 数据可视化
【BetterBench博士】2024年中国研究生数学建模竞赛 C题:数据驱动下磁性元件的磁芯损耗建模 问题分析、数学模型、python 代码
2024年中国研究生数学建模竞赛C题聚焦磁性元件磁芯损耗建模。题目背景介绍了电能变换技术的发展与应用,强调磁性元件在功率变换器中的重要性。磁芯损耗受多种因素影响,现有模型难以精确预测。题目要求通过数据分析建立高精度磁芯损耗模型。具体任务包括励磁波形分类、修正斯坦麦茨方程、分析影响因素、构建预测模型及优化设计条件。涉及数据预处理、特征提取、机器学习及优化算法等技术。适合电气、材料、计算机等多个专业学生参与。
1576 16
【BetterBench博士】2024年中国研究生数学建模竞赛 C题:数据驱动下磁性元件的磁芯损耗建模 问题分析、数学模型、python 代码
|
22天前
|
编解码 JSON 自然语言处理
通义千问重磅开源Qwen2.5,性能超越Llama
击败Meta,阿里Qwen2.5再登全球开源大模型王座
972 14
|
3天前
|
Linux 虚拟化 开发者
一键将CentOs的yum源更换为国内阿里yum源
一键将CentOs的yum源更换为国内阿里yum源
218 2
|
17天前
|
人工智能 开发框架 Java
重磅发布!AI 驱动的 Java 开发框架:Spring AI Alibaba
随着生成式 AI 的快速发展,基于 AI 开发框架构建 AI 应用的诉求迅速增长,涌现出了包括 LangChain、LlamaIndex 等开发框架,但大部分框架只提供了 Python 语言的实现。但这些开发框架对于国内习惯了 Spring 开发范式的 Java 开发者而言,并非十分友好和丝滑。因此,我们基于 Spring AI 发布并快速演进 Spring AI Alibaba,通过提供一种方便的 API 抽象,帮助 Java 开发者简化 AI 应用的开发。同时,提供了完整的开源配套,包括可观测、网关、消息队列、配置中心等。
734 9