1、前言
本系统采用C++编写,运用到了数据结构(链表)的知识,没有运用STL标准模板库。欢迎大家对本系统提供建议,联系QQ:2401937272,谢谢大家。
2、技术支持
C++基础语法
数据结构
编译器:Microsoft Visual Studio Community 2022 (64 位) – Current
3、需求分析
4、功能分析
根据前面的需求分析,本系统主要分为两个模块:读者和管理员模块。
读者模块主要包括:登录功能、注册功能、显示所有图书功能、查询特定图书功能、修改信息,显示读者信息、显示借阅记录、借书和还书功能。
管理员模块主要包括:登录功能、显示所有图书信息功能、添加图书功能、显示读者信息功能、查找读者信息功能、显示所有借阅记录功能。
5、结构设计
(1)Reader结构体设计
名称 | 意义 | 类型 |
name | 姓名 | string |
sex | 性别 | string |
code | 编号 | string |
password | 密码 | string |
next | 下一个Reader数据 | Reader* |
(2)Manager结构体设计
名称 | 意义 | 类型 |
code | 编号 | string |
password | 密码 | string |
(3)MyTime结构体设计
名称 | 意义 | 类型 |
year | 年 | int |
month | 月 | int |
day | 日 | int |
hour | 小时 | int |
minute | 分钟 | int |
second | 星期 | int |
(4)Book结构体设计
名称 | 意义 | 类型 |
code | 编号 | string |
name | 书名 | string |
author | 作者 | string |
price | 价格 | double |
sum | 总数量 | int |
number | 借走数量 | int |
next | 指向下一个Book数据 | Book* |
(5)Borrow结构体设计
名称 | 意义 | 类型 |
book_code | 图书编号 | string |
reader_code | 读者编号 | string |
mytime | 借书时间 | Mytine |
state | 借书状态(借阅中/已归还) | string |
note | 借阅备注 | string |
next | 指向下一个Borrow数据 | Borrow* |
6、代码实现
6.1、读者模块
(1)登录
Reader* readerFindByCodePassword(Reader* reader,string code,string password) {
Reader* r = reader->next;
while (r != NULL) {
if (r->code == code&&r->password==password) {
return r;
}
r = r->next;
}
cout << "查找失败!\n";
system ("pause");
system ("cls");
return NULL;
}
(2)遍历所有图书
void bookFindAll (Book *book) {
Book* b = book->next;
cout << "编号\t\t书名\t\t作者\t\t价格\t\t借走\t\t总数\n";
while (b != NULL) {
cout << b->code << "\t\t" << b->name << "\t\t" << b->author << "\t\t" << b->price << "\t\t" << b->number << "\t\t" << b->sum << "\n";
b = b->next;
}
system ("pause");
system ("cls");
}
(3)查找特定图书
void bookFindByCode (Book* book, string code) {
Book* b = book->next;
while (b != NULL) {
if (b->code == code) {
cout << "编号:" << b->code << endl;
cout << "书名:" << b->name << endl;
cout << "作者:" << b->author << endl;
cout << "价格:" << b->price << endl;
cout << "借走:" << b->number << endl;
cout << "总数:" << b->sum << endl;
system ("pause");
system ("cls");
return;
}
b = b->next;
}
cout << "未找到该图书!\n";
system ("pause");
system ("cls");
}
(4)借书功能
void borrowRegister (Borrow* borrow,Book *book,string reader_code) {
Borrow* b = new Borrow ();
cout << "图书编号:";
cin >> b->book_code;
if (!bookBorrow (book, b->book_code)) {
system ("pause");
system ("cls");
return;
}
b->reader_code = reader_code;
cout << "借阅备注:";
cin >> b->note;
if (b->note.size () == 0) {
b->note = "无";
}
b->mytime = myTimeFactory ();
b->state = "借阅中";
b->next = borrow->next;
borrow->next = b;
cout << "图书借阅成功!\n";
system ("pause");
system ("cls");
}
(5)还书功能
void borrowReturn (Borrow* borrow, string book_code, string reader_code) {
Borrow* b = borrow->next;
int i;
while (b != NULL) {
if (b->book_code == book_code && b->reader_code == reader_code&&b->state=="借阅中") {
cout << "是否归还图书:(1/0)";
cin >> i;
if (i == 1) {
b->state = "已归还";
cout << "归还成功!\n";
system ("pause");
system ("cls");
return;
} else {
system ("cls");
return;
}
}
b = b->next;
}
cout << "未找到该条借阅记录!\n请检查图书编号是否正确!\n";
system ("pause");
system ("cls");
}
(6)显示读者信息功能
Reader* readerFindByCode (Reader* reader, string code) {
Reader* r = reader->next;
while (r != NULL) {
if (r->code == code) {
return r;
}
r = r->next;
}
cout << "查找失败!\n";
system ("pause");
system ("cls");
return NULL;
}
(7)修改读者信息
void readerChange (Reader* reader) {
cout << "编号:";
cin >> reader->code;
cout << "姓名:";
cin >> reader->name;
cout << "性别:";
cin >> reader->sex;
if (reader->sex != "男") {
reader->sex = "女";
}
cout << "密码:";
cin >> reader->password;
cout << "修改成功!\n";
}
(8)注册功能
void readerRegist (Reader* reader) {
Reader* r = new Reader ();
cout << "编号:";
cin >> r->code;
cout << "姓名:";
cin >> r->name;
cout << "性别:";
cin >> r->sex;
if (r->code != "男") {
r->sex = "女";
}
cout << "密码:";
cin >> r->password;
r->next = reader->next;
reader->next = r;
cout << "读者注册成功!\n";
system ("pause");
system ("cls");
}
(9)查找借书记录
void borrowFindByReaderCode (Borrow* borrow, string reader_code) {
Borrow* b = borrow->next;
cout << "图书编号\t\t读者编号\t\t借书时间\t\t\t借阅状态\t\t借阅备注\n";
while (b!=NULL) {
if (b->reader_code == reader_code) {
cout << b->book_code << "\t\t\t";
cout << b->reader_code << "\t\t\t";
cout << b->mytime << "\t";
cout << b->state << "\t\t\t";
cout << b->note << "\n";
}
b = b->next;
}
system ("pause");
system ("cls");
}
6.2、管理员模块
(1)登录功能
void managerLogin (Manager *manager,Book *book,Reader* reader,Borrow *borrow) {
string code, password;
cout << "编号:";
cin >> code;
cout << "密码:";
cin >> password;
if (manager->code == code && manager->password==password) {
system ("cls");
managerMenu (book,reader,borrow);
}
}
(2)生成借阅时间功能
MyTime myTimeFactory () {
MyTime mt = MyTime ();
time_t tt;
time (&tt);
tm* t = new tm ();
localtime_s (t,&tt);
mt.year = (int)t->tm_year+1900;
mt.month = (int)t->tm_mon+1;
mt.day = (int)t->tm_mday;
mt.hour = (int)t->tm_hour;
mt.minute = (int)t->tm_min;
mt.second = (int)t->tm_sec;
return mt;
}
(3)显示所有借阅记录
void borrowFindAll (Borrow* borrow) {
Borrow* b = borrow->next;
cout << "图书编号\t\t读者编号\t\t借书时间\t\t\t借阅状态\t\t借阅备注\n";
while (b != NULL) {
cout << b->book_code << "\t\t\t";
cout << b->reader_code << "\t\t\t";
cout << b->mytime << "\t";
cout << b->state << "\t\t\t";
cout << b->note << "\n";
b = b->next;
}
system ("pause");
system ("cls");
}
(4)显示所有读者信息
void readerFindAll (Reader* reader) {
Reader* r = reader->next;
cout << "编号\t\t姓名\t\t性别\t\t密码\n";
while (r != NULL) {
cout << r->code << "\t\t" << r->name << "\t\t" << r->sex << "\t\t" << r->password << "\n";
r = r->next;
}
system ("pause");
system ("cls");
}
(5)查找读者并修改
void readerChangeByCode (Reader* reader, string code) {
Reader* r = reader->next;
int i;
while (r != NULL) {
if (r->code == code) {
cout << "编号:" << r->code << endl;
cout << "姓名:" << r->code << endl;
cout << "性别:" << r->code << endl;
cout << "密码:" << r->code << endl;
cout << "请选择是否修改信息:(1/0)\n";
cin >> i;
if (i == 1) {
cout << "编号:";
cin >> r->code;
cout << "姓名:";
cin >> r->name;
cout << "性别:";
cin >> r->sex;
if (r->sex != "男") {
r->sex = "女";
}
cout << "密码:";
cin >> r->password;
cout << "修改成功!\n";
system ("pause");
system ("cls");
return;
}
}
r = r->next;
}
cout << "未找到该读者信息!";
system ("pause");
system ("cls");
}
(6)添加图书信息
void bookAdd (Book* book) {
Book* b = new Book ();
cout << "编号:";
cin >> b->code;
cout << "书名:";
cin >> b->name;
cout << "作者:";
cin >> b->author;
cout << "价格:";
cin >> b->price;
cout << "总数:";
cin >> b->sum;
b->number = 0;
b->next = book->next;
book->next = b;
cout << "添加成功!\n";
system ("pause");
system ("cls");
}
(7)查找图书并修改修改图书信息
void bookChangeByCode (Book* book, string code){
Book* b = book->next;
int i;
while (b != NULL) {
if (b->code == code) {
cout << "编号:" << b->code << endl;
cout << "名称:" << b->name<< endl;
cout << "作者:" << b->author << endl;
cout << "价格:" << b->price << endl;
cout << "借阅:" << b->number << endl;
cout << "总数:" << b->sum << endl;
cout << "是否修改:(1/0)\n";
cin >> i;
if (i == 1) {
cout << "编号:";
cin >> b->code;
cout << "名称:";
cin >> b->name;
cout << "作者:";
cin >> b->author;
cout << "价格:";
cin >> b->price;
cout << "总数:";
i = b->sum;
cin >> b->sum;
if (b->sum < b->number) {
cout << "总数错误!修改失败!\n";
b->sum = i;
} else {
cout << "修改成功!\n";
}
system ("pause");
system ("cls");
return;
}
}
b = b->next;
}
cout << "未找到该图书!\n";
system ("pause");
system ("cls");
}
6.3、文件模块
这里以读者信息和借书记录进行简要的介绍。
(1)读者信息读取
因为读者信息显示的顺序对用户的使用体验无太大影响,所以读者信息的添加和读取都采用头插法。
void readerLoad (Reader* reader) {
ifstream is;
Reader* r = new Reader ();
is.open ("reader.txt");
while (is >> r->code) {
is >> r->name;
is >> r->sex;
is >> r->password;
r->next = reader->next;
reader->next = r;
r = new Reader ();
}
is.close ();
delete r;
}
(2) 读者信息保存
在保存操作中只需注意每个读者信息保存时的具体信息的顺序(这里读取和保存都是编号、姓名、性别、密码的顺序)不要弄错了。
void readerSave (Reader* reader) {
ofstream os;
Reader* r = reader->next;
os.open ("reader.txt");
if (r == NULL) {
os << "";
os.close ();
return;
}
while (r != NULL) {
os << r->code << endl;
os << r->name << endl;
os << r->sex << endl;
os << r->password << endl;
r = r->next;
}
os.close ();
}
(3)借阅信息的读取
因为借阅信息的添加是按照头插法添加的且保存也是从头开始的,所以读取的时候采用尾插法进行,从而时用户在显示借阅信息的时候最新的借阅信息始终在最上方,以保证用户的使用体验。
void borrowLoad (Borrow* borrow) {
ifstream is;
Borrow* p=borrow;
Borrow* b = new Borrow();
is.open ("borrow.txt");
while (is >> b->book_code) {
is >> b->reader_code;
is >> b->mytime;
is >> b->state;
is >> b->note;
b->next = NULL;
p->next = b;
p = b;
b = new Borrow ();
}
is.close ();
delete b;
}
(4)借阅信息的保存
这里的注意事项与读者信息的保存一样。
void borrowSave (Borrow* borrow) {
ofstream os;
Borrow* b = borrow->next;
os.open ("borrow.txt");
while (b != NULL) {
os << b->book_code << endl;
os << b->reader_code << endl;
os << b->mytime.year << " ";
os << b->mytime.month << " ";
os << b->mytime.day << " ";
os << b->mytime.hour << " ";
os << b->mytime.minute << " ";
os << b->mytime.second << endl;
os << b->state << endl;
os << b->note << endl;
b = b->next;
}
os.close ();
}
7、总结
本系统由C++编写,还存在着其他功能需求有待完成,例如管理员可以删除图书功能、管理员操作撤销等功能,这些功能会在2.0版本进行更新,同时优化本系统的代码结构。
8、致谢
本系统采用C++编写,运用到了数据结构(链表)的知识,没有运用STL标准模板库。欢迎大家对本系统提供建议,联系QQ:2401937272,谢谢大家。