change folder

pull/9/head
yangtengze 2 years ago
parent 6bd52249f8
commit 291d41d647

@ -1,233 +1,233 @@
#ifndef DATA_H_INCLUDED #ifndef DATA_H_INCLUDED
#define DATA_H_INCLUDED #define DATA_H_INCLUDED
#include <stdio.h> #include <stdio.h>
#include "sqlist.h" #include "sqlist.h"
struct Book { struct Book {
int id; // 编号 int id; // 编号
string title; // 标题 string title; // 标题
int number; // 数量 int number; // 数量
}; };
SqList <Book> books; SqList <Book> books;
// 函数声明 // 函数声明
bool bookempty(); // 书籍库判空 bool bookempty(); // 书籍库判空
void input(Book& b); // 输入一本书的信息 void input(Book& b); // 输入一本书的信息
void print(const Book& b);// 打印一本书的信息 void print(const Book& b);// 打印一本书的信息
int find(int id); // 根据 id 查找图书 || 若找到,返回元素的位序,找不到时返回 0 int find(int id); // 根据 id 查找图书 || 若找到,返回元素的位序,找不到时返回 0
void DoAddBook(); // 添加图书 void DoAddBook(); // 添加图书
void DoFindBook(); // 查找图书 void DoFindBook(); // 查找图书
void DoDeleteBook(); // 删除图书 void DoDeleteBook(); // 删除图书
void DoPrintBook(); // 打印所有图书 void DoPrintBook(); // 打印所有图书
void DoRepaidBook(); // 归还图书 void DoRepaidBook(); // 归还图书
void DoBoorowBook(); // 借用图书 void DoBoorowBook(); // 借用图书
void DoReviseBook(); // 修改图书 void DoReviseBook(); // 修改图书
void DoSortBook(); // 排序图书 void DoSortBook(); // 排序图书
// 输入一本书的信息 // 输入一本书的信息
void input(Book& b) void input(Book& b)
{ {
cout << "ID : "; cout << "ID : ";
cin >> b.id; cin >> b.id;
cin.ignore(1024, '\n'); // skip rest chars of the line cin.ignore(1024, '\n'); // skip rest chars of the line
cout << "TITLE : "; cout << "TITLE : ";
getline(cin, b.title); getline(cin, b.title);
cout << "Number : "; cout << "Number : ";
while (1){ while (1){
cin >> b.number; cin >> b.number;
if (b.number>0) break; if (b.number>0) break;
cout << "Number ERROR" << endl; cout << "Number ERROR" << endl;
cout << "Input again" << endl; cout << "Input again" << endl;
} }
} }
// 打印一本书的信息 // 打印一本书的信息
void print(const Book& b) void print(const Book& b)
{ {
cout << "Book " << endl << endl cout << "Book " << endl << endl
<<"**************** id: " << b.id << " ****************" << endl <<"**************** id: " << b.id << " ****************" << endl
<< "**** title: 《 " << b.title << " 》****" << endl << "**** title: 《 " << b.title << " 》****" << endl
<< "************** Number: " << b.number << " **************" << endl << endl; << "************** Number: " << b.number << " **************" << endl << endl;
} }
// 根据 id 查找图书 // 根据 id 查找图书
// 若找到,返回元素的位序,找不到时返回 0 // 若找到,返回元素的位序,找不到时返回 0
int find(int id) int find(int id)
{ {
for (int i = 1; i <= books.length; i++) for (int i = 1; i <= books.length; i++)
{ {
if (id == books.elem[i].id){ if (id == books.elem[i].id){
return i; return i;
} }
} }
return 0; // not found return 0; // not found
} }
// 书籍库判空 // 书籍库判空
bool bookempty() bool bookempty()
{ {
int a=0; int a=0;
for (int i=1;i<=books.length;i++) for (int i=1;i<=books.length;i++)
{ {
if (find(i)==0) a++; if (find(i)==0) a++;
} }
if (a==books.length) return true; if (a==books.length) return true;
else return false; else return false;
} }
// 添加图书 // 添加图书
void DoAddBook() void DoAddBook()
{ {
cout << endl << "Add Book"<< endl<<endl; cout << endl << "Add Book"<< endl<<endl;
Book book; Book book;
input(book); //输入图书 input(book); //输入图书
books.elem[books.length + 1] = book; books.elem[books.length + 1] = book;
books.length++; books.length++;
print(book); print(book);
cout << "*******Added********" << endl; cout << "*******Added********" << endl;
} }
// 查找图书 // 查找图书
void DoFindBook() void DoFindBook()
{ {
cout << endl << "Find Book" << endl <<endl; cout << endl << "Find Book" << endl <<endl;
int id; int id;
cout << "Enter book ID: "; cout << "Enter book ID: ";
cin >> id; cin >> id;
Book book; Book book;
if (find(id) == 0) if (find(id) == 0)
cout << "Not found" << endl; cout << "Not found" << endl;
else else
{ {
book = books.elem[find(id)]; book = books.elem[find(id)];
cout << "Found" << endl; cout << "Found" << endl;
cout << "************" << endl; cout << "************" << endl;
print(book); print(book);
} }
} }
// 删除图书 // 删除图书
void DoDeleteBook() void DoDeleteBook()
{ {
cout << endl << "Delete Book" << endl << endl; cout << endl << "Delete Book" << endl << endl;
int id; int id;
cout << "Which book you wanna delelet?"<<endl; cout << "Which book you wanna delelet?"<<endl;
cout << "Enter book ID: "; cout << "Enter book ID: ";
cin >> id; cin >> id;
Book e; Book e;
if (find(id) == 0) if (find(id) == 0)
cout << "Not found" << endl; cout << "Not found" << endl;
else else
{ {
e = books.elem[find(id)]; e = books.elem[find(id)];
print(e); print(e);
for (int i = find(id); i < books.length; i++) for (int i = find(id); i < books.length; i++)
{ {
books.elem[i] = books.elem[i + 1]; books.elem[i] = books.elem[i + 1];
} }
--books.length; --books.length;
cout << "************************" << endl; cout << "************************" << endl;
cout << "Deleted" << endl; cout << "Deleted" << endl;
} }
} }
// 打印所有图书 // 打印所有图书
void DoPrintBook() void DoPrintBook()
{ {
cout << endl << "Print All Books" << endl <<endl; cout << endl << "Print All Books" << endl <<endl;
int flag = 0; int flag = 0;
for (int i = 0; i < books.length; i++) for (int i = 0; i < books.length; i++)
{ {
Book e; Book e;
e = books.elem[i + 1]; e = books.elem[i + 1];
print(e); print(e);
flag++; flag++;
} }
cout << "Total: "; cout << "Total: ";
cout << flag; cout << flag;
cout << " books" << endl; cout << " books" << endl;
} }
// 归还图书 // 归还图书
void DoRepaidBook() void DoRepaidBook()
{ {
cout << endl << "Repaid Book" << endl<<endl; cout << endl << "Repaid Book" << endl<<endl;
int id; int id;
cout << "Which book you wanna repaid ? " << endl; cout << "Which book you wanna repaid ? " << endl;
cout << "Enter book ID :" << endl; cout << "Enter book ID :" << endl;
cin >> id; cin >> id;
cout << endl; cout << endl;
if (find(id)){ if (find(id)){
Book e; Book e;
e = books.elem[find(id)]; e = books.elem[find(id)];
++books.elem[find(id)].number; ++books.elem[find(id)].number;
}else{ }else{
DoAddBook(); DoAddBook();
} }
} }
// 借用图书 // 借用图书
void DoBoorowBook() void DoBoorowBook()
{ {
cout << endl << "Boorow Book" << endl <<endl; cout << endl << "Boorow Book" << endl <<endl;
int id; int id;
cout << "Which book you wanna boorow ? " << endl; cout << "Which book you wanna boorow ? " << endl;
cout << "Enter book ID :" << endl; cout << "Enter book ID :" << endl;
cin >> id; cin >> id;
cout << endl; cout << endl;
if (find(id)){ if (find(id)){
if (books.elem[find(id)].number>0) { if (books.elem[find(id)].number>0) {
Book e; Book e;
e = books.elem[find(id)]; e = books.elem[find(id)];
cout<< "*********"<< endl << "OK" << endl << "*********"<<endl; cout<< "*********"<< endl << "OK" << endl << "*********"<<endl;
--books.elem[find(id)].number; --books.elem[find(id)].number;
}else cout << "***************************"<< endl }else cout << "***************************"<< endl
<< "Book empty" << endl << "***************************" << endl; << "Book empty" << endl << "***************************" << endl;
}else cout<< "NOT FOUND THIS BOOK"<<endl; }else cout<< "NOT FOUND THIS BOOK"<<endl;
} }
// 修改图书 // 修改图书
void DoReviseBook() void DoReviseBook()
{ {
cout <<endl<< "Revise Book" <<endl <<endl; cout <<endl<< "Revise Book" <<endl <<endl;
int id; int id;
printf( "Which book you wanna revise ?\n" ); printf( "Which book you wanna revise ?\n" );
cin >> id; cin >> id;
if ( find(id)!=0 ) { if ( find(id)!=0 ) {
printf ( "Original book number : %d\n", books.elem[id].number ); printf ( "Original book number : %d\n", books.elem[id].number );
printf ( "Revised number :" ); printf ( "Revised number :" );
int xiugai; int xiugai;
scanf ( "%d", &xiugai ); scanf ( "%d", &xiugai );
books.elem[id].number+=xiugai; books.elem[id].number+=xiugai;
cout << "**********************" << endl; cout << "**********************" << endl;
printf ( "Revise success \n" ); printf ( "Revise success \n" );
} }
else printf ( "No such book\n"); else printf ( "No such book\n");
} }
// 排序图书 // 排序图书
void DoSortBook() void DoSortBook()
{ {
cout << endl << "Sort Books" << endl << endl; cout << endl << "Sort Books" << endl << endl;
int a=0; int a=0;
if (bookempty()) cout << "Book data empty"<< endl; if (bookempty()) cout << "Book data empty"<< endl;
else { else {
for (int i=1;i<books.length;i++) for (int i=1;i<books.length;i++)
for (int j=books.length;j>0;j--) for (int j=books.length;j>0;j--)
{ {
if (books.elem[i].id>books.elem[j].id) { if (books.elem[i].id>books.elem[j].id) {
Book temp=books.elem[i];books.elem[i]=books.elem[j];books.elem[j]=temp;} Book temp=books.elem[i];books.elem[i]=books.elem[j];books.elem[j]=temp;}
} }
} }
cout << "*****************************" <<endl; cout << "*****************************" <<endl;
cout << "Sort success"<<endl; cout << "Sort success"<<endl;
} }
#endif // DATA_H_INCLUDED #endif // DATA_H_INCLUDED

@ -1,177 +1,177 @@
/////////////////////////////////////// ///////////////////////////////////////
/// file: sqlist.h /// file: sqlist.h
/// Sequential list /// Sequential list
/////////////////////////////////////// ///////////////////////////////////////
#pragma once #pragma once
#include <stdexcept> // for std::out_of_range #include <stdexcept> // for std::out_of_range
using std::out_of_range; // 导入名称 using std::out_of_range; // 导入名称
using std::length_error; using std::length_error;
/////////////////////////////////////// ///////////////////////////////////////
/// 存储结构 /// 存储结构
/// ///
/// 线性表的顺序存储结构 /// 线性表的顺序存储结构
/// ///
template <typename E, int MAXSIZE = 256> template <typename E, int MAXSIZE = 256>
struct SqList struct SqList
{ {
E elem[MAXSIZE]; E elem[MAXSIZE];
int length; int length;
}; };
/////////////////////////////////////// ///////////////////////////////////////
/// 基本操作 /// 基本操作
/// ///
/// 构造空的顺序表 L /// 构造空的顺序表 L
/// ///
template <typename E, int M> template <typename E, int M>
void InitList(SqList<E, M> &L) void InitList(SqList<E, M> &L)
{ {
L.length = 0; // 空表长度为 0 L.length = 0; // 空表长度为 0
} }
/// ///
/// 销毁顺序表 L /// 销毁顺序表 L
/// ///
template <typename E, int M> template <typename E, int M>
void DestroyList(SqList<E, M> &L) void DestroyList(SqList<E, M> &L)
{ {
// do nothing // do nothing
} }
/// ///
/// 将顺序表 L 置为空表 /// 将顺序表 L 置为空表
/// ///
template <typename E, int M> template <typename E, int M>
void ClearList(SqList<E, M> &L) void ClearList(SqList<E, M> &L)
{ {
L.length = 0; L.length = 0;
} }
/// ///
/// 若 L 为空表,则返回 true否则返回 false /// 若 L 为空表,则返回 true否则返回 false
/// ///
template <typename E, int M> template <typename E, int M>
bool ListEmpty(const SqList<E, M> &L) bool ListEmpty(const SqList<E, M> &L)
{ {
return L.length == 0; return L.length == 0;
} }
/// ///
/// 返回顺序表 L 中数据元素个数 /// 返回顺序表 L 中数据元素个数
/// ///
template <typename E, int M> template <typename E, int M>
int ListLength(const SqList<E, M> &L) int ListLength(const SqList<E, M> &L)
{ {
return L.length; return L.length;
} }
/// ///
/// 用 e 返回顺序表 L 中第 i 个数据元素1<=i<=length /// 用 e 返回顺序表 L 中第 i 个数据元素1<=i<=length
/// ///
template <typename E, int M> template <typename E, int M>
bool GetElem(const SqList<E, M> &L, int i, E &e) bool GetElem(const SqList<E, M> &L, int i, E &e)
{ {
// 若 i 值不合法,则返回 false // 若 i 值不合法,则返回 false
if (i < 1 || i > L.length) if (i < 1 || i > L.length)
return false; return false;
// 取第 i 个元素 // 取第 i 个元素
e = L.elem[i - 1]; e = L.elem[i - 1];
// 返回 true 表示操作成功 // 返回 true 表示操作成功
return true; return true;
} }
/// ///
/// 取顺序表 L 中第 i 个元素 /// 取顺序表 L 中第 i 个元素
/// ///
template <typename E, int M> template <typename E, int M>
const E &GetElem(const SqList<E, M> &L, int i) const E &GetElem(const SqList<E, M> &L, int i)
{ {
// 若 i 值不合法,不能取元素 // 若 i 值不合法,不能取元素
if (i < 1 || i > L.length) if (i < 1 || i > L.length)
throw out_of_range("i out of range"); throw out_of_range("i out of range");
// 返回第 i 个元素 // 返回第 i 个元素
return L.elem[i - 1]; return L.elem[i - 1];
} }
/// ///
/// 在顺序表 L 中第 i 个位置之前插入新的数据元素 e /// 在顺序表 L 中第 i 个位置之前插入新的数据元素 e
/// ///
template <typename E, int MAXSIZE> template <typename E, int MAXSIZE>
void ListInsert(SqList<E, MAXSIZE> &L, int i, E e) void ListInsert(SqList<E, MAXSIZE> &L, int i, E e)
{ {
// 若表满,则不能插入 // 若表满,则不能插入
if (L.length == MAXSIZE) if (L.length == MAXSIZE)
throw length_error("L is full"); throw length_error("L is full");
// 若 i 值不合法,则不能插入 // 若 i 值不合法,则不能插入
if (i < 1 || i > L.length + 1) if (i < 1 || i > L.length + 1)
throw out_of_range("i out of range"); throw out_of_range("i out of range");
// 插入位置及之后的元素后移 // 插入位置及之后的元素后移
for (int j = L.length - 1; j >= i - 1; j--) for (int j = L.length - 1; j >= i - 1; j--)
{ {
L.elem[j + 1] = L.elem[j]; L.elem[j + 1] = L.elem[j];
} }
// 插入元素 // 插入元素
L.elem[i - 1] = e; L.elem[i - 1] = e;
// 表长增 1 // 表长增 1
L.length++; L.length++;
} }
/// ///
/// 在顺序表 L 中删除第 i 个元素,用 e 返回 /// 在顺序表 L 中删除第 i 个元素,用 e 返回
/// ///
template <typename E, int M> template <typename E, int M>
void ListDelete(SqList<E, M> &L, int i, E &e) void ListDelete(SqList<E, M> &L, int i, E &e)
{ {
// 若 i 值不合法,则不能删除 // 若 i 值不合法,则不能删除
if (i < 1 || i > L.length) if (i < 1 || i > L.length)
throw out_of_range("i out of range"); throw out_of_range("i out of range");
// 取出被删除元素 // 取出被删除元素
e = L.elem[i - 1]; e = L.elem[i - 1];
// 被删除元素之后的元素前移 // 被删除元素之后的元素前移
for (int j = i; j < L.length; j++) for (int j = i; j < L.length; j++)
{ {
L.elem[j - 1] = L.elem[j]; L.elem[j - 1] = L.elem[j];
} }
// 表长减 1 // 表长减 1
L.length--; L.length--;
} }
/// ///
/// 返回顺序表 L 中第一个与 e 满足关系 compare 的数据元素的位序 /// 返回顺序表 L 中第一个与 e 满足关系 compare 的数据元素的位序
/// 若这样的数据元素不存在,则返回 0。 /// 若这样的数据元素不存在,则返回 0。
/// ///
template <typename E, int M, typename Cmp> template <typename E, int M, typename Cmp>
int LocateElem(const SqList<E, M> &L, const E &e, Cmp compare) int LocateElem(const SqList<E, M> &L, const E &e, Cmp compare)
{ {
// 逐个取出元素与 e 比较 // 逐个取出元素与 e 比较
for (int i = 0; i < L.length; i++) for (int i = 0; i < L.length; i++)
{ {
// 若满足条件,则返回位序 // 若满足条件,则返回位序
if (compare(L.elem[i], e)) if (compare(L.elem[i], e))
return i + 1; return i + 1;
} }
return 0; // 不存在 return 0; // 不存在
} }
/// ///
/// 遍历顺序表,依次对 L 中的每个数据元素调用函数 visit /// 遍历顺序表,依次对 L 中的每个数据元素调用函数 visit
/// ///
template <typename E, int M, typename Func> template <typename E, int M, typename Func>
void ListTraverse(const SqList<E, M> &L, Func visit) void ListTraverse(const SqList<E, M> &L, Func visit)
{ {
for (int i = 0; i < L.length; i++) for (int i = 0; i < L.length; i++)
{ {
visit(L.elem[i]); visit(L.elem[i]);
} }
} }

@ -1,126 +1,126 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <iostream> #include <iostream>
using namespace std; using namespace std;
#include "data.h" #include "data.h"
#include "sqlist.h" #include "sqlist.h"
enum { enum {
CMD_QUIT, // 退出程序 CMD_QUIT, // 退出程序
CMD_QUERY, // 查书(数量) CMD_QUERY, // 查书(数量)
CMD_BOOROW, // 借用书籍 CMD_BOOROW, // 借用书籍
CMD_REPAID, // 归还书籍 CMD_REPAID, // 归还书籍
CMD_PRINT, // 打印书籍 CMD_PRINT, // 打印书籍
CMD_INSERT, // 添加书籍 CMD_INSERT, // 添加书籍
CMD_DELETE, // 删除书籍 CMD_DELETE, // 删除书籍
CMD_UPDATE, // 修改书籍 CMD_UPDATE, // 修改书籍
CMD_SORT, // 排序书籍 CMD_SORT, // 排序书籍
}; };
// 函数声明 // 函数声明
void init(void); // 程序启动 void init(void); // 程序启动
void quit(void); // 程序退出 void quit(void); // 程序退出
void display_menu(void); // 显示命令菜单 void display_menu(void); // 显示命令菜单
int make_choice(void); // 选择命令 int make_choice(void); // 选择命令
int confirm(const char* msg); // 确认 int confirm(const char* msg); // 确认
// 主函数 // 主函数
int main(void) int main(void)
{ {
init(); // 程序启动 init(); // 程序启动
InitList(books); InitList(books);
for(;;) { for(;;) {
display_menu(); // display_menu(); //
int c = make_choice(); int c = make_choice();
switch(c) { switch(c) {
case CMD_REPAID:// 还书 case CMD_REPAID:// 还书
{DoRepaidBook(); {DoRepaidBook();
break;} break;}
case CMD_BOOROW:// 借书 case CMD_BOOROW:// 借书
{DoBoorowBook(); {DoBoorowBook();
break;} break;}
case CMD_PRINT:// 打印所有书籍 case CMD_PRINT:// 打印所有书籍
{DoPrintBook(); {DoPrintBook();
break;} break;}
case CMD_QUERY:// 查找书籍 case CMD_QUERY:// 查找书籍
{DoFindBook(); {DoFindBook();
break;} break;}
case CMD_INSERT:// 添加书籍 case CMD_INSERT:// 添加书籍
{DoAddBook(); {DoAddBook();
break;} break;}
case CMD_DELETE:// 删除书籍 case CMD_DELETE:// 删除书籍
{DoDeleteBook(); {DoDeleteBook();
break;} break;}
case CMD_UPDATE:// 修改书籍 case CMD_UPDATE:// 修改书籍
{DoReviseBook(); {DoReviseBook();
break;} break;}
case CMD_SORT: // 排序书籍 case CMD_SORT: // 排序书籍
{DoSortBook(); {DoSortBook();
break;} break;}
case CMD_QUIT:// 退出程序 case CMD_QUIT:// 退出程序
{if (confirm("确定要退出吗?")) quit(); // 仅在确认后退出 {if (confirm("确定要退出吗?")) quit(); // 仅在确认后退出
break;} break;}
default: default:
puts("命令错误,请重新选择"); puts("命令错误,请重新选择");
break;} break;}
} }
quit(); // 程序退出 quit(); // 程序退出
DestroyList(books); DestroyList(books);
return 0; return 0;
} }
void init(void) void init(void)
{ {
puts("*******图书管理系统启动*******"); puts("*******图书管理系统启动*******");
} }
void quit(void) void quit(void)
{ {
puts("系统退出"); puts("系统退出");
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }
void display_menu(void) void display_menu(void)
{ {
cout << "————————————————————————————————————————————————————————————————"<<endl; cout << "————————————————————————————————————————————————————————————————"<<endl;
printf("| %d 查书 | %d 借书 | %d 还书 | %d 打印 | %d 添加 |\n| %d 删除 | %d 修改 | %d 排序 | %d 退出 |\n", printf("| %d 查书 | %d 借书 | %d 还书 | %d 打印 | %d 添加 |\n| %d 删除 | %d 修改 | %d 排序 | %d 退出 |\n",
CMD_QUERY,CMD_BOOROW,CMD_REPAID,CMD_PRINT,CMD_INSERT,CMD_DELETE,CMD_UPDATE,CMD_SORT,CMD_QUIT); CMD_QUERY,CMD_BOOROW,CMD_REPAID,CMD_PRINT,CMD_INSERT,CMD_DELETE,CMD_UPDATE,CMD_SORT,CMD_QUIT);
cout << "————————————————————————————————————————————————————————————————"<<endl; cout << "————————————————————————————————————————————————————————————————"<<endl;
} }
int make_choice(void) int make_choice(void)
{ {
int c; // 用户输入 int c; // 用户输入
int n = 0; // 正确读入的数据项个数 int n = 0; // 正确读入的数据项个数
while (n == 0) { while (n == 0) {
printf("请选择:"); printf("请选择:");
n = scanf("%d", &c); // 尝试读入整数 c n = scanf("%d", &c); // 尝试读入整数 c
scanf("%*[^\n]"); // 跳过一行中剩余的字符 scanf("%*[^\n]"); // 跳过一行中剩余的字符
} }
return c; return c;
} }
int confirm(const char* msg) int confirm(const char* msg)
{ {
char c = 'n'; // 默认选择是 no char c = 'n'; // 默认选择是 no
printf("%s(Y/N): ", msg); // 提示输入 yes/no 进行确认s printf("%s(Y/N): ", msg); // 提示输入 yes/no 进行确认s
scanf(" %c%*[^\n]", &c); // 读取第一个字符,忽略剩余字符 scanf(" %c%*[^\n]", &c); // 读取第一个字符,忽略剩余字符
return c == 'y' || c == 'Y'; // 返回确认结果 return c == 'y' || c == 'Y'; // 返回确认结果
} }
Loading…
Cancel
Save