diff --git a/book.cpp b/book.cpp deleted file mode 100644 index abb721f..0000000 --- a/book.cpp +++ /dev/null @@ -1,170 +0,0 @@ -#include -using namespace std; - -#include "sqlist.h" // 顺序表 - -// 图书信息 -struct Book { - int id; // 编号 - string title; // 标题 - string author; // 作者 - float price; // 价格 -}; - -// 所有书的列表 -SqList books; - -int get_command(); // 读取命令 -void DoAddBook(); // 添加图书 -void DoFindBook(); // 查找图书 -void DoDeleteBook(); // 删除图书 -void DoPrintBook(); // 打印图书 - -int main() -{ - bool finish = false; - - InitList(books); - - while (!finish) { - switch (get_command()) { - case 1: // Add book - DoAddBook(); - break; - case 2: // Find book - DoFindBook(); - break; - case 3: // Delete book - DoDeleteBook(); - break; - case 4: // Print - DoPrintBook(); - break; - case 0: // Exit - finish = true; - break; - default: // no command - cout << "Error command." << endl; - } - } - - DestroyList(books); - - return 0; -} - -// 读取命令 -int get_command() -{ - int cmd; - cout << "Commands: (1) Add, (2) Find, (3) Delete, (4) Print, (0) Exit\n"; - cout << "Enter your choice (0-4): "; - cin >> cmd; - return cmd; -} - -// 输入一本书的信息 -void read(Book& b) -{ - cout << "ID : "; - cin >> b.id; - cin.ignore(1024, '\n'); // skip rest chars of the line - cout << "TITLE : "; - getline(cin, b.title); - cout << "AUTHOR: "; - getline(cin, b.author); - cout << "PRICE : "; - cin >> b.price; -} - -// 打印一本书的信息 -void print(const Book& b) -{ - cout << "Book { id: " << b.id << "," - << " title:\"" << b.title << "\"," - << " author: \"" << b.author << "\"," - << " price: " << b.price << " }" - << 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 -} - -// 添加图书 -void DoAddBook() -{ - Book e; - read(e); - books.elem[books.length + 1] = e; - books.length++; - print(e); - cout << "Added" << endl; -} - -// 查找图书 -void DoFindBook() -{ - int id; - cout << "Enter book ID: "; - cin >> id; - Book e; - - if (find(id) == 0) - cout << "Not found" << endl; - else - { - e = books.elem[find(id)]; - print(e); - cout << "Found" << endl; - } -} - -// 删除图书 -void DoDeleteBook() -{ - int id; - cout << "Enter book ID: "; - cin >> id; - Book e; - int c = find(id); - if (find(id) == 0) - cout << "Not found" << endl; - else - { - e = books.elem[c]; - print(e); - for (int i = c; i < books.length; i++) - { - books.elem[i] = books.elem[i + 1]; - } - --books.length; - cout << "Deleted" << endl; - } -} - -// 打印图书 -void DoPrintBook() -{ - int flag = 0; - for (int i = 0; i < books.length; i++) - { - Book e; - e = books.elem[i + 1]; - print(e); - flag++; - } - cout << "Total: "; - cout << flag; - cout << " books" << endl; -} \ No newline at end of file diff --git a/data.h b/data.h index 24e086e..5f19df2 100644 --- a/data.h +++ b/data.h @@ -2,54 +2,211 @@ #define DATA_H_INCLUDED #include +#include "sqlist.h" + + // 函数声明 -void read_data(void); // 读取 -void save_data(void); // 保存 -void print_data(void); // 打印 -void query_data(void); // 查询 -void add_data(void); // 添加 -void update_data(void); // 修改 -void delete_data(void); // 删除 -void sort_data(void); // 排序 -void make_chart(void); // 图表 + +void print_book(void); // 打印 +void query_book(void); // 查询 +void add_book(void); // 添加 +void delete_book(void); // 删除 +void boorow_book(void); // 借书 +void repaid_book(void); // 还书 + + +void DoAddBook(); // 添加图书 +void DoFindBook(); // 查找图书 +void DoDeleteBook(); // 删除图书 +void DoPrintBook(); // 打印所有图书 +void DoRepaidBook(); // 归还图书 +void DoBoorowBook(); // 借用图书 -void read_data(void) + + +void repaid_book(void) // 还书 { - printf("读取\n"); + printf("还书\n"); } -void save_data(void) +void boorow_book(void) // 借书 { - printf("保存\n"); + printf("借书\n"); } -void print_data(void) +void query_book(void) // 查询 { - printf("打印\n"); + printf("查书\n"); } -void query_data(void) +void print_book(void) // 打印 { - printf("查询\n"); - } -void add_data(void) + printf("打印\n"); + } +void add_book(void) // 添加 { puts("添加"); } -void update_data(void) +void delete_book(void) // 删除 { - puts("修改"); + puts("删除"); } -void delete_data(void) + + + + + +struct Book { + int id; // 编号 + string title; // 标题 + int number; // 数量 + }; + +SqList books; + + // 输入一本书的信息 +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++) { - puts("删除"); + if (id == books.elem[i].id){ + return i; + } + } + return 0; // not found +} + + +// 添加图书 +void DoAddBook() +{ + Book book; + input(book); //输入图书 + books.elem[books.length + 1] = book; + books.length++; + print(book); + cout << "*******Added********" << endl; +} + +// 查找图书 +void DoFindBook() +{ + int id; + cout << "Enter book ID: "; + cin >> 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 sort_data(void) +} + +// 删除图书 +void DoDeleteBook() +{ + int id; + cout << "Which book you wanna delelet?"<> id; + Book e; + if (find(id) == 0) + cout << "Not found" << endl; + else { - puts("排序"); + 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 make_chart(void) +} + +// 打印所有图书 +void DoPrintBook() +{ + int flag = 0; + for (int i = 0; i < books.length; i++) { - puts("图表"); + Book e; + e = books.elem[i + 1]; + print(e); + flag++; + } + cout << "Total: "; + cout << flag; + cout << " books" << endl; +} + +// 归还图书 +void DoRepaidBook() +{ + int id; + cout << "Which book you wanna repaid ? " << endl; + cin >> id; + cout << endl; + if (find(id)){ + Book e; + e = books.elem[find(id)]; + ++books.elem[find(id)].number; + }else{ + DoAddBook(); } +} + +// 借用图书 +void DoBoorowBook() +{ + int id; + cout << "Which book you wanna boorow ? " << endl; + cin >> id; + cout << endl; + if (find(id)){ + if (books.elem[find(id)].number>0) { + Book e; + e = books.elem[find(id)]; + cout<< "*********"<< endl << "OK" << endl << "*********"< -#include "student.h" -using namespace std; - -struct student{ - long int id[10]; ///十位学号 - char name[15]; ///姓名 - long int phonenumber[11]; //手机号 - struct date; //日期 -}; - -struct date{ - int year; - int month; - int day; -}; - -int main() -{ - -} diff --git a/student.h b/student.h deleted file mode 100644 index 467bd93..0000000 --- a/student.h +++ /dev/null @@ -1,6 +0,0 @@ -/////////////////// -///student.cpp -/////////////////// - - - diff --git a/teammates b/teammates deleted file mode 100644 index ed9ecba..0000000 --- a/teammates +++ /dev/null @@ -1,4 +0,0 @@ -杨腾泽1 -孙英皓 -刘彩月 -李聪颖 diff --git a/title.cpp b/title.cpp index 6e04899..f501052 100644 --- a/title.cpp +++ b/title.cpp @@ -1,30 +1,20 @@ -///程序启动 - -///1 读取 | 2 保存 | 3 打印 | 4 查询 | 5 添加 -///6 修改 | 7 删除 | 8 排序 | 9 图表 | 0 退出 - -///请选择: 0 -///确定要退出吗?(Y/N): no - - - - - #include #include + #include + using namespace std; + + #include "data.h" + #include "sqlist.h" enum { - CMD_QUIT, // 退出 - CMD_READ, // 读取 - CMD_SAVE, // 保存 - CMD_PRINT, // 打印 - CMD_QUERY, // 查询 - CMD_INSERT, // 添加 - CMD_UPDATE, // 修改 - CMD_DELETE, // 删除 - CMD_SORT, // 排序 - CMD_CHART, // 图表 + CMD_QUIT, // 退出程序 + CMD_QUERY, // 查书(数量) + CMD_BOOROW, // 借书 + CMD_REPAID, // 还书 + CMD_PRINT, // 打印书籍 + CMD_INSERT, // 添加书籍 + CMD_DELETE, // 删除书籍 }; @@ -32,69 +22,83 @@ 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_READ: - read_data(); - break; - case CMD_SAVE: - save_data(); - break; - case CMD_PRINT: - print_data(); - break; - case CMD_QUERY: - query_data(); - break; - case CMD_INSERT: - add_data(); - break; - case CMD_UPDATE: - update_data(); - break; - case CMD_DELETE: - delete_data(); - break; - case CMD_SORT: - sort_data(); - break; - case CMD_CHART: - make_chart(); - break; - case CMD_QUIT: - if (confirm("确定要退出吗?")) quit(); // 仅在确认后退出 - break; + case CMD_REPAID:// 还书 + {repaid_book(); + DoRepaidBook(); + break;} + case CMD_BOOROW:// 借书 + {boorow_book(); + DoBoorowBook(); + break;} + case CMD_PRINT:// 打印所有书籍 + {print_book(); + DoPrintBook(); + break;} + case CMD_QUERY:// 查找书籍 + {query_book(); + DoFindBook(); + break;} + case CMD_INSERT:// 添加书籍 + {add_book(); + DoAddBook(); + break;} + case CMD_DELETE:// 删除书籍 + {delete_book(); + DoDeleteBook(); + break;} + case CMD_QUIT:// 退出程序 + {if (confirm("确定要退出吗?")) quit(); // 仅在确认后退出 + break;} default: puts("命令错误,请重新选择"); break;} } quit(); // 程序退出 + + DestroyList(books); return 0; } - // 函数定义 + + + + + + void init(void) { - puts("程序启动"); + puts("*******图书管理系统启动*******"); } void quit(void) { - puts("程序退出"); + puts("系统退出"); exit(EXIT_SUCCESS); } + + + void display_menu(void) { - printf("\n%d 读取 | %d 保存 | %d 打印 | %d 查询 | %d 添加\n%d 修改 | %d 删除 | %d 排序 | %d 图表 | %d 退出\n\n", - CMD_READ,CMD_SAVE,CMD_PRINT,CMD_QUERY,CMD_INSERT,CMD_UPDATE,CMD_DELETE,CMD_SORT,CMD_CHART,CMD_QUIT); + cout << "————————————————————————————————————————————————————————————————"<