diff --git a/Graph/系统模块图.drawio b/Graph/系统模块图.drawio new file mode 100644 index 0000000..3c0aa7e --- /dev/null +++ b/Graph/系统模块图.drawio @@ -0,0 +1,277 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/README.md b/README.md index 1b67efb..4e46874 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # 图书管理系统 -2023年4月22日 +2023年5月1日 杨腾泽,孙英皓,李聪颖,刘彩月 ## 项目简介 @@ -9,8 +9,8 @@ 项目开发过程中采用 Kanban(看板)进行任务管理和分工协作,并使用 Git 对程序代码和文档进行版本管理。任务分工情况如下: |任务 |设计 |开发 |测试 |文档 | |------ |-------|-------|--------|-------| -|Ca-Cb | | | | 杨腾泽 | -|C0-C2 | | | | 孙英皓 | +|Ca-Cb | | | | 孙英皓 | +|C0-C2 | | | | 杨腾泽 | |C3-C5 | | | | 刘彩月 | |C6-C8 | | | | 李聪颖 | @@ -22,6 +22,7 @@ ## 关于图书管理系统 设计一个图书管理系统,要求采用命令行菜单界面进行交互,具备查书、借书、还书、打印书籍 、添加、删除等基本功能,能够以表格和图表形式展示数据,采用 CSV 格式保存数据。 +\ 系统的功能性需求: - 数据的查书、借书、还书、打印书籍、添加、删除展示。 @@ -60,9 +61,70 @@ 确定要退出吗?(Y/N): n 程序退出 ``` +### C1: 查找图书 +选择菜单命令: 1 ,提示Find Book 和 Enter book ID: 字样,若本身书籍库为空,则提示Book data empty。继续输入图书编号。若没找到,提示Not found;若找到,提示Found 并 打印书籍信息。 +``` +请选择:1 + +Find Book + +Book data empty +``` +``` +请选择:1 + +Find Book + +Enter book ID: 1 +Not found +``` +``` +请选择:1 + +Find Book + +Enter book ID: 1 +Found +************ +Book + +**************** id: 1 **************** +**** title: 《 2 》**** +************** Number: 3 ************** +``` +### C2:借用图书 +选择菜单命令:2,提示 Boorow Book,若本身书籍库为空,则提示Book data empty。提示 Which book you wanna boorow ?,则继续输入图书编号。若该书本身不存在,则提示NOT FOUND THIS BOOK;如图书存在,则提示OK。 +``` +请选择:2 + +Boorow Book + +Book data empty +``` +``` +请选择:2 + +Boorow Book + +Which book you wanna boorow ? +2 +NOT FOUND THIS BOOK +``` +``` +请选择:2 + +Boorow Book + +Which book you wanna boorow ? +1 + +********* +OK +********* +``` ### C3:归还图书 -选择菜单命令:3 提示归还图书 和 你想要归还哪一本书?输入图书编号,若在系统中没有该图书,则添加图书并输入图书的编号,数量和数目,显示相关信息并归还成功,否则直接退出。 +选择菜单命令:3,提示归还图书 和 你想要归还哪一本书?输入图书编号,若在系统中没有该图书,则添加图书并输入图书的编号,数量和数目,显示相关信息并归还成功,否则直接退出。 ````` 请选择:3 @@ -95,7 +157,7 @@ Which book you wanna repaid ? ````` ### C4:打印书籍 -选择菜单命令:4 提示打印书籍并打印图书信息,若没有书,则输出Total: 0 books,若有书,则输出书籍信息 +选择菜单命令:4,提示打印书籍并打印图书信息,若没有书,则输出Total: 0 books,若有书,则输出书籍信息 ``` 请选择:4 @@ -188,6 +250,7 @@ Which book you wanna revise ? 2 No such book `````` + # 概要设计 系统主要分为用户界面和数据处理两大模块。 diff --git a/data.h b/code/data.h similarity index 95% rename from data.h rename to code/data.h index f05e2a5..813dc33 100644 --- a/data.h +++ b/code/data.h @@ -1,231 +1,235 @@ -#ifndef DATA_H_INCLUDED -#define DATA_H_INCLUDED - -#include -#include "sqlist.h" - - -struct Book { - int id; // 编号 - string title; // 标题 - int number; // 数量 - }; - -SqList books; - -// 函数声明 -bool bookempty(); // 书籍库判空 -void input(Book& b); // 输入一本书的信息 -void print(const Book& b);// 打印一本书的信息 -int find(int id); // 根据 id 查找图书 || 若找到,返回元素的位序,找不到时返回 0 - -void DoAddBook(); // 添加图书 -void DoFindBook(); // 查找图书 -void DoDeleteBook(); // 删除图书 -void DoPrintBook(); // 打印所有图书 -void DoRepaidBook(); // 归还图书 -void DoBoorowBook(); // 借用图书 -void DoReviseBook(); // 修改图书 -void DoSortBook(); // 排序图书 - - -// 输入一本书的信息 -void input(Book& b) -{ - cout << "ID : "; - cin >> b.id; - cin.ignore(1024, '\n'); // skip rest chars of the line - cout << "TITLE : "; - getline(cin, b.title); - cout << "Number : "; - while (1){ - cin >> b.number; - if (b.number>0) break; - cout << "Number ERROR" << endl; - cout << "Input again" << endl; - } -} - -// 打印一本书的信息 -void print(const Book& b) -{ - cout << "Book " << endl << endl - <<"**************** id: " << b.id << " ****************" << endl - << "**** title: 《 " << b.title << " 》****" << endl - << "************** Number: " << b.number << " **************" << endl << endl; -} - -// 根据 id 查找图书 -// 若找到,返回元素的位序,找不到时返回 0 -int find(int id) -{ - - for (int i = 1; i <= books.length; i++) - { - if (id == books.elem[i].id){ - return i; - } - } - return 0; // not found -} - -// 书籍库判空 -bool bookempty() -{ - int a=0; - for (int i=1;i<=books.length;i++) - { - if (find(i)==0) a++; - } - if (a==books.length) return true; - else return false; -} - -// 添加图书 -void DoAddBook() -{ - cout << endl << "Add Book"<< endl<> id; - - Book book; - if (find(id) == 0) - cout << "Not found" << endl; - else - { - book = books.elem[find(id)]; - cout << "Found" << endl; - cout << "************" << endl; - print(book); - } -} - -// 删除图书 -void DoDeleteBook() -{ - cout << endl << "Delete Book" << endl << endl; - int id; - cout << "Which book you wanna delelet?"<> id; - Book e; - if (find(id) == 0) - cout << "Not found" << endl; - else - { - e = books.elem[find(id)]; - print(e); - for (int i = find(id); i < books.length; i++) - { - books.elem[i] = books.elem[i + 1]; - } - --books.length; - cout << "************************" << endl; - cout << "Deleted" << endl; - } -} - -// 打印所有图书 -void DoPrintBook() -{ - cout << endl << "Print All Books" << endl <> id; - cout << endl; - if (find(id)){ - Book e; - e = books.elem[find(id)]; - ++books.elem[find(id)].number; - }else{ - DoAddBook(); - } -} - -// 借用图书 -void DoBoorowBook() -{ - cout << endl << "Boorow Book" << endl <> id; - cout << endl; - if (find(id)){ - if (books.elem[find(id)].number>0) { - Book e; - e = books.elem[find(id)]; - cout<< "*********"<< endl << "OK" << endl << "*********"<> id; - - if ( find(id)!=0 ) { - printf ( "Original book number : %d\n", books.elem[id].number ); - printf ( "Revised number :" ); - int xiugai; - scanf ( "%d", &xiugai ); - books.elem[id].number+=xiugai; - cout << "**********************" << endl; - printf ( "Revise success \n" ); - } - else printf ( "No such book\n"); -} - -// 排序图书 -void DoSortBook() -{ - cout << endl << "Sort Books" << endl << endl; - int a=0; - if (bookempty()) cout << "Book data empty"<< endl; - else { - for (int i=1;i0;j--) - { - if (books.elem[i].id>books.elem[j].id) { - Book temp=books.elem[i];books.elem[i]=books.elem[j];books.elem[j]=temp;} - } - } - cout << "*****************************" < +#include "sqlist.h" + + +struct Book { + int id; // 编号 + string title; // 标题 + int number; // 数量 + }; + +SqList books; + +// 函数声明 +bool bookempty(); // 书籍库判空 +void input(Book& b); // 输入一本书的信息 +void print(const Book& b);// 打印一本书的信息 +int find(int id); // 根据 id 查找图书 || 若找到,返回元素的位序,找不到时返回 0 + +void DoAddBook(); // 添加图书 +void DoFindBook(); // 查找图书 +void DoDeleteBook(); // 删除图书 +void DoPrintBook(); // 打印所有图书 +void DoRepaidBook(); // 归还图书 +void DoBoorowBook(); // 借用图书 +void DoReviseBook(); // 修改图书 +void DoSortBook(); // 排序图书 + + +// 输入一本书的信息 +void input(Book& b) +{ + cout << "ID : "; + cin >> b.id; + cin.ignore(1024, '\n'); // skip rest chars of the line + cout << "TITLE : "; + getline(cin, b.title); + cout << "Number : "; + while (1){ + cin >> b.number; + if (b.number>0) break; + cout << "Number ERROR" << endl; + cout << "Input again" << endl; + } +} + +// 打印一本书的信息 +void print(const Book& b) +{ + cout << "Book " << endl << endl + <<"**************** id: " << b.id << " ****************" << endl + << "**** title: 《 " << b.title << " 》****" << endl + << "************** Number: " << b.number << " **************" << endl << endl; +} + +// 根据 id 查找图书 +// 若找到,返回元素的位序,找不到时返回 0 +int find(int id) +{ + + for (int i = 1; i <= books.length; i++) + { + if (id == books.elem[i].id){ + return i; + } + } + return 0; // not found +} + +// 书籍库判空 +bool bookempty() +{ + int a=0; + for (int i=1;i<=books.length;i++) + { + if (find(i)==0) a++; + } + if (a==books.length) return true; + else return false; +} + +// 添加图书 +void DoAddBook() +{ + cout << endl << "Add Book"<< endl<> id; + + Book book; + if (find(id) == 0) + cout << "Not found" << endl; + else + { + book = books.elem[find(id)]; + cout << "Found" << endl; + cout << "************" << endl; + print(book); + } +} + +// 删除图书 +void DoDeleteBook() +{ + cout << endl << "Delete Book" << endl << endl; + int id; + cout << "Which book you wanna delelet?"<> id; + Book e; + if (find(id) == 0) + cout << "Not found" << endl; + else + { + e = books.elem[find(id)]; + print(e); + for (int i = find(id); i < books.length; i++) + { + books.elem[i] = books.elem[i + 1]; + } + --books.length; + cout << "************************" << endl; + cout << "Deleted" << endl; + } +} + +// 打印所有图书 +void DoPrintBook() +{ + cout << endl << "Print All Books" << endl <> id; + cout << endl; + if (find(id)){ + Book e; + e = books.elem[find(id)]; + ++books.elem[find(id)].number; + }else{ + DoAddBook(); + } +} + +// 借用图书 +void DoBoorowBook() +{ + cout << endl << "Boorow Book" << endl <> id; + cout << endl; + if (find(id)){ + if (books.elem[find(id)].number>0) { + Book e; + e = books.elem[find(id)]; + cout<< "*********"<< endl << "OK" << endl << "*********"<> id; + + if ( find(id)!=0 ) { + printf ( "Original book number : %d\n", books.elem[id].number ); + printf ( "Revised number :" ); + int xiugai; + scanf ( "%d", &xiugai ); + books.elem[id].number+=xiugai; + cout << "**********************" << endl; + printf ( "Revise success \n" ); + } + else printf ( "No such book\n"); +} + +// 排序图书 +void DoSortBook() +{ + cout << endl << "Sort Books" << endl << endl; + int a=0; + if (bookempty()) cout << "Book data empty"<< endl; + else { + for (int i=1;i0;j--) + { + if (books.elem[i].id>books.elem[j].id) { + Book temp=books.elem[i];books.elem[i]=books.elem[j];books.elem[j]=temp;} + } + } + cout << "*****************************" < // for std::out_of_range -using std::out_of_range; // 导入名称 -using std::length_error; - -/////////////////////////////////////// -/// 存储结构 - -/// -/// 线性表的顺序存储结构 -/// -template -struct SqList -{ - E elem[MAXSIZE]; - int length; -}; - -/////////////////////////////////////// -/// 基本操作 - -/// -/// 构造空的顺序表 L -/// -template -void InitList(SqList &L) -{ - L.length = 0; // 空表长度为 0 -} - -/// -/// 销毁顺序表 L -/// -template -void DestroyList(SqList &L) -{ - // do nothing -} - -/// -/// 将顺序表 L 置为空表 -/// -template -void ClearList(SqList &L) -{ - L.length = 0; -} - -/// -/// 若 L 为空表,则返回 true,否则返回 false -/// -template -bool ListEmpty(const SqList &L) -{ - return L.length == 0; -} - -/// -/// 返回顺序表 L 中数据元素个数 -/// -template -int ListLength(const SqList &L) -{ - return L.length; -} - -/// -/// 用 e 返回顺序表 L 中第 i 个数据元素,1<=i<=length -/// -template -bool GetElem(const SqList &L, int i, E &e) -{ - // 若 i 值不合法,则返回 false - if (i < 1 || i > L.length) - return false; - - // 取第 i 个元素 - e = L.elem[i - 1]; - - // 返回 true 表示操作成功 - return true; -} - -/// -/// 取顺序表 L 中第 i 个元素 -/// -template -const E &GetElem(const SqList &L, int i) -{ - // 若 i 值不合法,不能取元素 - if (i < 1 || i > L.length) - throw out_of_range("i out of range"); - - // 返回第 i 个元素 - return L.elem[i - 1]; -} - -/// -/// 在顺序表 L 中第 i 个位置之前插入新的数据元素 e -/// -template -void ListInsert(SqList &L, int i, E e) -{ - // 若表满,则不能插入 - if (L.length == MAXSIZE) - throw length_error("L is full"); - // 若 i 值不合法,则不能插入 - if (i < 1 || i > L.length + 1) - throw out_of_range("i out of range"); - - // 插入位置及之后的元素后移 - for (int j = L.length - 1; j >= i - 1; j--) - { - L.elem[j + 1] = L.elem[j]; - } - // 插入元素 - L.elem[i - 1] = e; - // 表长增 1 - L.length++; -} - -/// -/// 在顺序表 L 中删除第 i 个元素,用 e 返回 -/// -template -void ListDelete(SqList &L, int i, E &e) -{ - // 若 i 值不合法,则不能删除 - if (i < 1 || i > L.length) - throw out_of_range("i out of range"); - - // 取出被删除元素 - e = L.elem[i - 1]; - // 被删除元素之后的元素前移 - for (int j = i; j < L.length; j++) - { - L.elem[j - 1] = L.elem[j]; - } - // 表长减 1 - L.length--; -} - -/// -/// 返回顺序表 L 中第一个与 e 满足关系 compare 的数据元素的位序 -/// 若这样的数据元素不存在,则返回 0。 -/// -template -int LocateElem(const SqList &L, const E &e, Cmp compare) -{ - // 逐个取出元素与 e 比较 - for (int i = 0; i < L.length; i++) - { - // 若满足条件,则返回位序 - if (compare(L.elem[i], e)) - return i + 1; - } - - return 0; // 不存在 -} - -/// -/// 遍历顺序表,依次对 L 中的每个数据元素调用函数 visit -/// -template -void ListTraverse(const SqList &L, Func visit) -{ - for (int i = 0; i < L.length; i++) - { - visit(L.elem[i]); - } +/////////////////////////////////////// +/// file: sqlist.h +/// Sequential list +/////////////////////////////////////// + +#pragma once + +#include // for std::out_of_range +using std::out_of_range; // 导入名称 +using std::length_error; + +/////////////////////////////////////// +/// 存储结构 + +/// +/// 线性表的顺序存储结构 +/// +template +struct SqList +{ + E elem[MAXSIZE]; + int length; +}; + +/////////////////////////////////////// +/// 基本操作 + +/// +/// 构造空的顺序表 L +/// +template +void InitList(SqList &L) +{ + L.length = 0; // 空表长度为 0 +} + +/// +/// 销毁顺序表 L +/// +template +void DestroyList(SqList &L) +{ + // do nothing +} + +/// +/// 将顺序表 L 置为空表 +/// +template +void ClearList(SqList &L) +{ + L.length = 0; +} + +/// +/// 若 L 为空表,则返回 true,否则返回 false +/// +template +bool ListEmpty(const SqList &L) +{ + return L.length == 0; +} + +/// +/// 返回顺序表 L 中数据元素个数 +/// +template +int ListLength(const SqList &L) +{ + return L.length; +} + +/// +/// 用 e 返回顺序表 L 中第 i 个数据元素,1<=i<=length +/// +template +bool GetElem(const SqList &L, int i, E &e) +{ + // 若 i 值不合法,则返回 false + if (i < 1 || i > L.length) + return false; + + // 取第 i 个元素 + e = L.elem[i - 1]; + + // 返回 true 表示操作成功 + return true; +} + +/// +/// 取顺序表 L 中第 i 个元素 +/// +template +const E &GetElem(const SqList &L, int i) +{ + // 若 i 值不合法,不能取元素 + if (i < 1 || i > L.length) + throw out_of_range("i out of range"); + + // 返回第 i 个元素 + return L.elem[i - 1]; +} + +/// +/// 在顺序表 L 中第 i 个位置之前插入新的数据元素 e +/// +template +void ListInsert(SqList &L, int i, E e) +{ + // 若表满,则不能插入 + if (L.length == MAXSIZE) + throw length_error("L is full"); + // 若 i 值不合法,则不能插入 + if (i < 1 || i > L.length + 1) + throw out_of_range("i out of range"); + + // 插入位置及之后的元素后移 + for (int j = L.length - 1; j >= i - 1; j--) + { + L.elem[j + 1] = L.elem[j]; + } + // 插入元素 + L.elem[i - 1] = e; + // 表长增 1 + L.length++; +} + +/// +/// 在顺序表 L 中删除第 i 个元素,用 e 返回 +/// +template +void ListDelete(SqList &L, int i, E &e) +{ + // 若 i 值不合法,则不能删除 + if (i < 1 || i > L.length) + throw out_of_range("i out of range"); + + // 取出被删除元素 + e = L.elem[i - 1]; + // 被删除元素之后的元素前移 + for (int j = i; j < L.length; j++) + { + L.elem[j - 1] = L.elem[j]; + } + // 表长减 1 + L.length--; +} + +/// +/// 返回顺序表 L 中第一个与 e 满足关系 compare 的数据元素的位序 +/// 若这样的数据元素不存在,则返回 0。 +/// +template +int LocateElem(const SqList &L, const E &e, Cmp compare) +{ + // 逐个取出元素与 e 比较 + for (int i = 0; i < L.length; i++) + { + // 若满足条件,则返回位序 + if (compare(L.elem[i], e)) + return i + 1; + } + + return 0; // 不存在 +} + +/// +/// 遍历顺序表,依次对 L 中的每个数据元素调用函数 visit +/// +template +void ListTraverse(const SqList &L, Func visit) +{ + for (int i = 0; i < L.length; i++) + { + visit(L.elem[i]); + } } \ No newline at end of file diff --git a/title.cpp b/code/title.cpp similarity index 96% rename from title.cpp rename to code/title.cpp index bcff6b6..5822cb3 100644 --- a/title.cpp +++ b/code/title.cpp @@ -1,126 +1,126 @@ - #include - #include - #include - using namespace std; - - - #include "data.h" - #include "sqlist.h" - - enum { - CMD_QUIT, // 退出程序 - CMD_QUERY, // 查书(数量) - CMD_BOOROW, // 借用书籍 - CMD_REPAID, // 归还书籍 - CMD_PRINT, // 打印书籍 - CMD_INSERT, // 添加书籍 - CMD_DELETE, // 删除书籍 - CMD_UPDATE, // 修改书籍 - CMD_SORT, // 排序书籍 - }; - - - // 函数声明 - void init(void); // 程序启动 - void quit(void); // 程序退出 - void display_menu(void); // 显示命令菜单 - - int make_choice(void); // 选择命令 - int confirm(const char* msg); // 确认 - - - - // 主函数 - int main(void) - { - init(); // 程序启动 - - InitList(books); - - for(;;) { - display_menu(); // - int c = make_choice(); - switch(c) { - case CMD_REPAID:// 还书 - {DoRepaidBook(); - break;} - case CMD_BOOROW:// 借书 - {DoBoorowBook(); - break;} - case CMD_PRINT:// 打印所有书籍 - {DoPrintBook(); - break;} - case CMD_QUERY:// 查找书籍 - {DoFindBook(); - break;} - case CMD_INSERT:// 添加书籍 - {DoAddBook(); - break;} - case CMD_DELETE:// 删除书籍 - {DoDeleteBook(); - break;} - case CMD_UPDATE:// 修改书籍 - {DoReviseBook(); - break;} - case CMD_SORT: // 排序书籍 - {DoSortBook(); - break;} - case CMD_QUIT:// 退出程序 - {if (confirm("确定要退出吗?")) quit(); // 仅在确认后退出 - break;} - default: - puts("命令错误,请重新选择"); - break;} - } - quit(); // 程序退出 - - DestroyList(books); - return 0; - } - - - - - - - void init(void) - { - puts("*******图书管理系统启动*******"); - } - void quit(void) - { - puts("系统退出"); - exit(EXIT_SUCCESS); - } - - - - void display_menu(void) - { - cout << "————————————————————————————————————————————————————————————————"< + #include + #include + using namespace std; + + + #include "data.h" + #include "sqlist.h" + + enum { + CMD_QUIT, // 退出程序 + CMD_QUERY, // 查书(数量) + CMD_BOOROW, // 借用书籍 + CMD_REPAID, // 归还书籍 + CMD_PRINT, // 打印书籍 + CMD_INSERT, // 添加书籍 + CMD_DELETE, // 删除书籍 + CMD_UPDATE, // 修改书籍 + CMD_SORT, // 排序书籍 + }; + + + // 函数声明 + void init(void); // 程序启动 + void quit(void); // 程序退出 + void display_menu(void); // 显示命令菜单 + + int make_choice(void); // 选择命令 + int confirm(const char* msg); // 确认 + + + + // 主函数 + int main(void) + { + init(); // 程序启动 + + InitList(books); + + for(;;) { + display_menu(); // + int c = make_choice(); + switch(c) { + case CMD_REPAID:// 还书 + {DoRepaidBook(); + break;} + case CMD_BOOROW:// 借书 + {DoBoorowBook(); + break;} + case CMD_PRINT:// 打印所有书籍 + {DoPrintBook(); + break;} + case CMD_QUERY:// 查找书籍 + {DoFindBook(); + break;} + case CMD_INSERT:// 添加书籍 + {DoAddBook(); + break;} + case CMD_DELETE:// 删除书籍 + {DoDeleteBook(); + break;} + case CMD_UPDATE:// 修改书籍 + {DoReviseBook(); + break;} + case CMD_SORT: // 排序书籍 + {DoSortBook(); + break;} + case CMD_QUIT:// 退出程序 + {if (confirm("确定要退出吗?")) quit(); // 仅在确认后退出 + break;} + default: + puts("命令错误,请重新选择"); + break;} + } + quit(); // 程序退出 + + DestroyList(books); + return 0; + } + + + + + + + void init(void) + { + puts("*******图书管理系统启动*******"); + } + void quit(void) + { + puts("系统退出"); + exit(EXIT_SUCCESS); + } + + + + void display_menu(void) + { + cout << "————————————————————————————————————————————————————————————————"<