数据结构项目—— 用顺序表制作图书管理系统

简介: 数据结构项目—— 用顺序表制作图书管理系统

《图书信息管理系统》的制作:



全部代码如下(各部分已注释):


#include "pch.h"
#include<string>
#include<fstream>
#include <iomanip>
#include <iostream>
using namespace std;
#define MAXSIZE  100
struct Book
{
  string id;
  string name;
  double price;
};
//顺序表结构体
struct SqList
{
  Book *elem;   //线性表初始位置
  int length;   //线性表长度
};
//初始化线性表
void initSqList(SqList &L)
{
  L.elem = new Book[MAXSIZE];
  if (!L.elem)
  {
    exit(0);
  }
  L.length = 0;
}
//线性表的取值
int GetElem(SqList &L, int i, Book &e)
{
  if (i<1||i>L.length)
  {
    return -1;
  }
  e=L.elem[i - 1];
  return 0;
} 
//线性表的查找
int LocateElem(SqList &L,string e)
{
  for (int i = 0; i < L.length; i++)
  {
    if (L.elem[i].id==e)
    {
      cout << "所查找书籍信息为:";
      cout << L.elem[i].id << "          ";
      cout << L.elem[i].name << "          ";
      cout << L.elem[i].price << endl;
      cout << "书籍查找成功!!" << endl;
      return i + 1;
    }
  }
  cout << "查无此书!!" << endl;
  return 0;
}
//线性表的插入
int InsertSqList(SqList &L, int i, Book e)
{
  //是否超出线性表的区间范围
  if (i<1||i>L.length+1)
  {
    return -1;
  }
  //当前元素超过线性表长度则无法插入
  if (L.length==MAXSIZE)
  {
    return -1;
  }
  //
  for (int j = L.length-1; j>=i-1; j--)
  {
    L.elem[j + 1] = L.elem[j];
  }
  L.elem[i - 1] = e;
  ++L.length;
  return 1;
}
//线性表的删除
int DeleteSqList(SqList &L, int i)
{
  //是否超出线性表的区间范围
  if (i<1 || i>L.length + 1)
  {
    return 0;
  }
  for (int j = i; j <=L.length; j++)
  {
    L.elem[j - 1] = L.elem[j];
  }
  --L.length;
  return 1;
}
int main()
{
  SqList L;
  int c;          //删除书籍位置
  int choice = -1;
  string str1, str2, str3;
  cout << "*****************************************" << endl;
  cout << "******          图书管理系统        *****" << endl;
  cout << "*****************************************" << endl;
  cout << "******      1.建立       2.录入     *****" << endl;
  cout << "******      3.取值       4.查找     *****" << endl;
  cout << "******      5.插入       6.删除     *****" << endl;
  cout << "******      7.输出       0.退出     *****" << endl;
  cout << "*****************************************" << endl;
  while (choice != 0)
  {
    cout << "请输入操作指令【0-7】" << endl;
    cin >> choice;
    int i = 0;
    switch (choice)
    {
    case 1:
    {
      initSqList(L);
      cout << "顺序表创建成功" << endl;
      cout << endl;
      cout << "*****************************************" << endl;
      cout << "******          图书管理系统        *****" << endl;
      cout << "*****************************************" << endl;
      cout << "******      1.建立       2.录入     *****" << endl;
      cout << "******      3.取值       4.查找     *****" << endl;
      cout << "******      5.插入       6.删除     *****" << endl;
      cout << "******      7.输出       0.退出     *****" << endl;
      cout << "*****************************************" << endl;
      break;
    }
    case 2:
    {
      L.elem = new Book[MAXSIZE];
      if (!L.elem)
      {
        exit(0);
      }
      L.length = 0;
      fstream file;
      file.open("book.txt");
      file >> str1 >> str2 >> str3;
      while (!file.eof())
      {
        file >> L.elem[i].id >> L.elem[i].name >> L.elem[i].price;
        i++;
      }
      cout << "book书库书籍信息导入成功" << endl;
      L.length = i;
      file.close();
      cout << endl;
      cout << "*****************************************" << endl;
      cout << "******          图书管理系统        *****" << endl;
      cout << "*****************************************" << endl;
      cout << "******      1.建立       2.录入     *****" << endl;
      cout << "******      3.取值       4.查找     *****" << endl;
      cout << "******      5.插入       6.删除     *****" << endl;
      cout << "******      7.输出       0.退出     *****" << endl;
      cout << "*****************************************" << endl;
      break;
    }
    case 3:
    {
      cout << "请输入取值图书位置:";
      cin >> i;
      Book em;
      GetElem(L,i,em);
      cout << em.id << "          ";
      cout << em.name << "          ";
      cout << em.price << endl;
      cout << "书籍取值成功!" << endl;
      cout << endl;
      cout << "*****************************************" << endl;
      cout << "******          图书管理系统        *****" << endl;
      cout << "*****************************************" << endl;
      cout << "******      1.建立       2.录入     *****" << endl;
      cout << "******      3.取值       4.查找     *****" << endl;
      cout << "******      5.插入       6.删除     *****" << endl;
      cout << "******      7.输出       0.退出     *****" << endl;
      cout << "*****************************************" << endl;
      break;
    }
    case 4:
    {
      Book em;  
      cout << "请输入查找图书编号:";
      cin >>em.id;
      LocateElem(L, em.id);
      cout << endl;
      cout << "*****************************************" << endl;
      cout << "******          图书管理系统        *****" << endl;
      cout << "*****************************************" << endl;
      cout << "******      1.建立       2.录入     *****" << endl;
      cout << "******      3.取值       4.查找     *****" << endl;
      cout << "******      5.插入       6.删除     *****" << endl;
      cout << "******      7.输出       0.退出     *****" << endl;
      cout << "*****************************************" << endl;
      break;
    }
    case 5:
    {
      cout << "请输入所要插入的位置:";
      cin >> i;
      Book em;
      cout << "请输入所要插入书籍的ID,书名,价格:";
      cin >> em.id >> em.name >> em.price;
      InsertSqList(L, i, em);
      cout << "书籍插入成功!" << endl;
      cout << endl;
      cout << "*****************************************" << endl;
      cout << "******          图书管理系统        *****" << endl;
      cout << "*****************************************" << endl;
      cout << "******      1.建立       2.录入     *****" << endl;
      cout << "******      3.取值       4.查找     *****" << endl;
      cout << "******      5.插入       6.删除     *****" << endl;
      cout << "******      7.输出       0.退出     *****" << endl;
      cout << "*****************************************" << endl;
      break;
    }
    case 6:
    {
      cout << "请输入要删除书籍位置:";
      cin >> c;
      if (DeleteSqList(L, c))
      {
        cout << "书籍删除成功!" << endl;
      }
      else
      {
        cout << "书籍删除失败!" << endl;
      }
      cout << endl;
      cout << "*****************************************" << endl;
      cout << "******          图书管理系统        *****" << endl;
      cout << "*****************************************" << endl;
      cout << "******      1.建立       2.录入     *****" << endl;
      cout << "******      3.取值       4.查找     *****" << endl;
      cout << "******      5.插入       6.删除     *****" << endl;
      cout << "******      7.输出       0.退出     *****" << endl;
      cout << "*****************************************" << endl;
      break;
    }
    case 7:
    {
      cout << "当前图书管理系统的所有图书信息如下:" << endl;
      for (int i = 0; i < L.length; i++)
      {
        cout << L.elem[i].id << setw(25);
        cout << L.elem[i].name << setw(15);
        cout << L.elem[i].price << endl;
      }
      cout << endl;
      cout << "*****************************************" << endl;
      cout << "******          图书管理系统        *****" << endl;
      cout << "*****************************************" << endl;
      cout << "******      1.建立       2.录入     *****" << endl;
      cout << "******      3.取值       4.查找     *****" << endl;
      cout << "******      5.插入       6.删除     *****" << endl;
      cout << "******      7.输出       0.退出     *****" << endl;
      cout << "*****************************************" << endl;
      break;
    }
    case 0:
    {
      break;
    }
    }
  }
  return 0;
}


结果为:


相关文章
|
1月前
|
机器学习/深度学习 存储 C++
【C++数据结构——线性表】顺序表的基本运算(头歌实践教学平台习题)【合集】
本文档介绍了线性表的基本运算任务,涵盖顺序表和链表的初始化、销毁、判定是否为空、求长度、输出、查找元素、插入和删除元素等内容。通过C++代码示例详细展示了每一步骤的具体实现方法,并提供了测试说明和通关代码。 主要内容包括: - **任务描述**:实现顺序表的基本运算。 - **相关知识**:介绍线性表的基本概念及操作,如初始化、销毁、判定是否为空表等。 - **具体操作**:详述顺序表和链表的初始化、求长度、输出、查找、插入和删除元素的方法,并附有代码示例。 - **测试说明**:提供测试输入和预期输出,确保代码正确性。 - **通关代码**:给出完整的C++代码实现,帮助完成任务。 文档
41 5
|
2月前
|
数据库
数据结构中二叉树,哈希表,顺序表,链表的比较补充
二叉搜索树,哈希表,顺序表,链表的特点的比较
数据结构中二叉树,哈希表,顺序表,链表的比较补充
|
3月前
|
算法 安全 NoSQL
2024重生之回溯数据结构与算法系列学习之顺序表习题精讲【无论是王道考研人还真爱粉都能包会的;不然别给我家鸽鸽丢脸好嘛?】
顺序表的定义和基本操作之插入;删除;按值查找;按位查找习题精讲等具体详解步骤以及举例说明
|
3月前
|
存储 算法 安全
2024重生之回溯数据结构与算法系列学习之顺序表【无论是王道考研人还真爱粉都能包会的;不然别给我家鸽鸽丢脸好嘛?】
顺序表的定义和基本操作之插入;删除;按值查找;按位查找等具体详解步骤以及举例说明
|
3月前
|
存储 C语言
【数据结构】顺序表(c语言实现)(附源码)
本文介绍了线性表和顺序表的基本概念及其实现。线性表是一种有限序列,常见的线性表有顺序表、链表、栈、队列等。顺序表是一种基于连续内存地址存储数据的数据结构,其底层逻辑是数组。文章详细讲解了静态顺序表和动态顺序表的区别,并重点介绍了动态顺序表的实现,包括初始化、销毁、打印、增删查改等操作。最后,文章总结了顺序表的时间复杂度和局限性,并预告了后续关于链表的内容。
110 3
|
4月前
|
存储
数据结构(顺序表)
数据结构(顺序表)
36 0
|
4月前
|
存储 算法
【数据结构】新篇章 -- 顺序表
【数据结构】新篇章 -- 顺序表
32 0
|
4月前
|
存储 测试技术
探索数据结构:顺序表的实现与应用
探索数据结构:顺序表的实现与应用
|
4月前
|
存储 编译器 C语言
数据结构-顺序表详解(看这篇就足够了,哈哈哈)
数据结构-顺序表详解(看这篇就足够了,哈哈哈)
84 2
|
4月前
|
存储 Java
数据结构第二篇【关于java线性表(顺序表)的基本操作】
数据结构第二篇【关于java线性表(顺序表)的基本操作】
67 6

热门文章

最新文章