天天看點

資料結構之圖書管理系統

文章目錄

  • ​​緒論:​​
  • ​​第一檔案結建構立:​​
  • ​​第二打通檔案脈絡:​​
  • ​​第三開始修建房梁​​
  • ​​第四主房梁`main`的建立​​
  • ​​第五main.cpp代碼完整版​​
  • ​​第六執行結果​​

緒論:

    昨晚看了女友老師的資料結構題目要求,覺得你們的題還是很有意思的,晚上花了兩小時純寫了一下代碼,救濟一下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
#define
#define
#define
#define      

這個時候我們已經建立并打通了

  • ​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");
}      

第六執行結果

資料結構之圖書管理系統