图书管理借阅系统(豪华版)
1:用户登录系统
-
用户角色区分(管理员/普通用户)
-
注册登录流程
-
基于角色的访问控制
-
用户数据加密存储(示例未加密,实际应加密)
-
2:借阅管理系统
-
完整的借阅/归还流程
-
借阅记录管理
-
库存自动更新
-
借阅期限管理
-
借阅记录查询
-
3:数据统计功能
-
图书总量统计
-
库存总量统计
-
最受欢迎图书分析
-
借阅趋势分析(需扩展时间处理)
-
用户借阅排行(需扩展用户系统)
3:使用说明:
-
首次使用需注册管理员账号(直接修改数据文件添加admin角色用户)
-
普通用户可进行图书查询和借阅操作
-
管理员具有完整系统管理权限
-
所有数据自动保存,记录包括:
-
books.txt(图书数据)
-
users.txt(用户数据)
-
records.txt(借阅记录)
4:扩展建议:
-
增加密码加密存储(使用加密算法)
-
完善日期处理(使用标准日期库)
-
添加借阅逾期罚款功能
-
实现图形界面(建议使用Qt框架)
-
增加图书分类和高级搜索功能
-
完善异常处理和数据验证
5:注意事项:
-
保持数据文件格式一致性
-
重要操作需要添加确认提示
-
敏感操作需要记录操作日志
-
定期进行数据备份
-
加强输入验证防止注入攻击
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<fstream>
#include<vector>
#include<string>
#include<map>
#include<algorithm>
#include<limits>
using namespace std;
struct Book {
string name;
string author;
string isbn;
double price;
int quantity;
};
struct User {
string username;
string password;
string role; //admin/user
};
struct BorrowRecord {
string username;
string isbn;
string borrowDate;
string dueDate;
bool returned;
};
vector<Book>books;
vector<User>users;
vector<BorrowRecord>borrowRecords;
User currentUser;
const string BOOK_FILE = "books.txt";
const string USER_FILE = "users.txt";
const string RECORD_FILE = "records.txt";
//文件操作函数
void loadBooks() {
//加载图书
ifstream bfile(BOOK_FILE);
if (bfile.is_open()) {
Book book;
while (bfile >> ws && getline(bfile, book.name) && getline(bfile, book.author) && getline(bfile, book.isbn) && bfile >> book.price >> book.quantity) {
books.push_back(book);
bfile.ignore();
}
bfile.close();
}
//加载用户
ifstream ufile(USER_FILE);
User u;
while (ufile >> ws && getline(ufile, u.username) && getline(ufile, u.password) && getline(ufile, u.role)) {
users.push_back(u);
}
//加载借阅记录
ifstream rfile(RECORD_FILE);
BorrowRecord r;
string returned;
while (rfile >> ws && getline(rfile, r.username) && getline(rfile, r.isbn) && getline(rfile, r.borrowDate) && getline(rfile, r.dueDate) && getline(rfile, returned)) {
r.returned = (returned == "1");
borrowRecords.push_back(r);
}
}
void saveData() {
//保存图书
ofstream bfile(BOOK_FILE);
if (bfile.is_open()) {
for (const auto& book : books) {
bfile << book.name << "\n"
<< book.author << "\n"
<< book.isbn << "\n"
<< book.price << " " << book.quantity << "\n";
}
bfile.close();
}
//保存用户
ofstream ufile(USER_FILE);
if (ufile.is_open()) {
for (const auto& u : users) {
ufile << u.username << "\n" << u.password << "\n" << u.role << "\n";
}
}
//保存借阅记录
ofstream rfile(RECORD_FILE);
if (rfile.is_open()) {
for (const auto& r : borrowRecords) {
rfile << r.username << "\n" << r.isbn << "\n" << r.borrowDate << "\n" << r.dueDate << "\n" << (r.returned ? 1 : 0) << "\n";
}
}
}
//用户系统
void registerUser() {
User newUser;
cout << "请输入用户名" << endl;
getline(cin, newUser.username);
if (any_of(users.begin(), users.end(), [&](const User& u) {return u.username == newUser.username; })) {
cout << "用户名已经存在!" << endl;
return;
}
cout << "请输入密码" << endl;
getline(cin, newUser.password);
newUser.role = "user";
users.push_back(newUser);
saveData();
cout << "注册成功" << endl;
}
void login() {
string username, password;
cout << "用户名:";
getline(cin, username);
cout << "密码:";
getline(cin, password);
auto it = find_if(users.begin(), users.end(), [&](const User& u) {return u.username == username && u.password == password; });
if (it != users.end()) {
currentUser = *it;
cout << "登录成功! 欢迎" <<currentUser.username<< endl;
}
else {
cout << "用户名或密码错误!\n";
}
}
//借阅管理
string getCurrentDate() {
time_t now = time(0);
tm* ltm = localtime(&now);
return to_string(1900 + ltm->tm_year) + "-" + to_string(1 + ltm->tm_mon) + "-" + to_string(ltm->tm_mday);
}
void borrowBook() {
string isbn;
cout << "请输入要借阅图书的ISBN:";
getline(cin, isbn);
auto bookIt = find_if(books.begin(), books.end(), [&](const Book& b) {return b.isbn == isbn; });
if (bookIt == books.end()) {
cout << "图书不存在" << endl;
return;
}
if (bookIt->quantity <= 0) {
cout << "该书已无库存!" << endl;
return;
}
BorrowRecord record;
record.username = currentUser.username;
record.isbn = isbn;
record.borrowDate = getCurrentDate();
record.dueDate = "2024-12-31";
record.returned = false;
borrowRecords.push_back(record);
bookIt->quantity--;
saveData();
cout << "借阅成功!" << endl;
}
void returnBook() {
string isbn;
cout << "输入归还图书的ISBN:";
getline(cin, isbn);
auto recordIt = find_if(borrowRecords.begin(), borrowRecords.end(), [&](const BorrowRecord& r) {return r.isbn == isbn&¤tUser.username==r.username&&!r.returned; });
if (recordIt == borrowRecords.end()) {
cout << "未找到借阅记录!" << endl;
}
else {
recordIt->returned = true;
auto bookIt = find_if(books.begin(), books.end(), [&](const Book& b) {return b.isbn == isbn; });
if (bookIt != books.end()) {
bookIt->quantity++;
saveData();
cout << "归还成功!" << endl;
}
}
}
void showMyBorrowRecord() {
for (const auto& x : borrowRecords) {
if (x.username == currentUser.username) {
cout << "图书的ISBN: " << x.isbn
<< "图书的借阅时间:" << x.borrowDate
<< "图书的归还时间:" << x.dueDate
<< "图书是否归还:" << x.returned;
}
cout << "==============";
}
}
//数据统计
void showStatistics() {
cout << "\n===== 数据统计 =====\n";
cout << "1. 图书总数:" << books.size() << endl;
int totalQuantity = 0;
for (const auto& b : books) {
totalQuantity += b.quantity;
}
cout << "2. 总库存:" << totalQuantity << endl;
//最受欢迎图书统计
map<string, int>borrowCount;
for (const auto& r : borrowRecords) {
borrowCount[r.isbn]++;
}
if (!borrowCount.empty()) {
auto maxIt = max_element(borrowCount.begin(), borrowCount.end(), [](const pair<string, int>& a, const pair<string, int>& b){ return a.second < b.second; });
auto bookIt = find_if(books.begin(), books.end(), [&](const Book& b) {return b.isbn == maxIt->first; });
cout << "3. 最受欢迎的图书:\n"
<< " 书名:" << bookIt->name << "\n"
<< " 借阅次数:" << maxIt->second << endl;
}
}
//菜单系统
void userMenu() {
cout << "\n===== 用户菜单 =====\n"
<< "1. 查询图书\n"
<<"2. 借阅图书\n"
<<"3. 归还图书\n"
<<"4. 查看我的借阅记录\n"
<< "5. 退出登录\n";
}
void adminMenu() {
cout<<"\n===== 管理员菜单 =====\n"
<<"1. 添加图书\n"
<<"2. 删除图书\n"
<<"3. 修改图书\n"
<<"4. 显示所有图书\n"
<<"5. 数据统计\n"
<< "6. 退出系统\n";
}
//核心功能函数
void addBook() {
Book newbook;
cout << "输入书名:";
getline(cin, newbook.name);
cout << "输入作者:";
getline(cin, newbook.author);
cout << "输入ISBN:";
getline(cin, newbook.isbn);
// 检查ISBN是否已存在
if (any_of(books.begin(), books.end(), [&](const Book& b) {
return b.isbn == newbook.isbn;
})) {
cout << "错误:ISBN已存在!\n";
return;
}
cout << "输入价格:";
cin >> newbook.price;
cout << "输入库存量:";
cin >> newbook.quantity;
cin.ignore();
}
void deleteBook() {
string isbn;
cout << "输入要删除的图书ISBN:";
getline(cin, isbn);
auto it = find_if(books.begin(), books.end(), [&](const Book& b) {return b.isbn == isbn; });
if (it != books.end()) {
books.erase(it);
cout << "图书删除成功!\n";
}
else {
cout << "未找到对应ISBN的图书!\n";
}
}
void searchBook() {
string keyword;
cout << "输入搜索关键词(书名/作者/ISBN):";
getline(cin, keyword);
vector<Book>results;
for (const Book& book : books) {
if (book.name.find(keyword) != string::npos || book.author.find(keyword) != string::npos || book.isbn.find(keyword) != string::npos)
{
results.push_back(book);
}
}
if (results.empty()) {
cout << "未找到相关图书!\n";
return;
}
cout << "\n搜索结果:\n";
for (const auto& book : results) {
cout << "书名:" << book.name << "\n"
<< "作者:" << book.author << "\n"
<< "ISBN:" << book.isbn << "\n"
<< "价格:" << book.price << "\n"
<< "库存:" << book.quantity << "\n\n";
}
}
void modifyBook() {
string isbn;
cout << "输入要修改的图书ISBN:";
getline(cin, isbn);
auto it = find_if(books.begin(), books.end(), [&](Book& b) {return b.isbn == isbn; });
if (it == books.end()) {
cout << "未找到对应ISBN的图书!\n";
return;
}
Book& book = *it;
cout << "当前信息:\n"
<< "书名:" << book.name << "\n"
<< "作者:" << book.author << "\n"
<< "价格:" << book.price << "\n"
<< "库存:" << book.quantity << "\n";
cout << "输入新书名(留空保持不变):";
string input;
getline(cin, input);
if (!input.empty()) book.name = input;
cout << "输入新作者(留空保持不变):";
getline(cin, input);
if (!input.empty()) book.author = input;
cout << "输入新价格(输入0保持不变):";
double newPrice;
cin >> newPrice;
if (newPrice > 0) book.price = newPrice;
cout << "输入新库存(输入-1保持不变):";
int newQty;
cin >> newQty;
if (newQty >= 0) book.quantity = newQty;
cin.ignore();
cout << "修改成功!\n";
}
void displayAll() {
if (books.empty()) {
cout << "当前没有图书记录!\n";
return;
}
cout << "\n所有图书信息:\n";
for (const auto& book : books) {
cout << "---------------\n"
<< "书名:" << book.name << "\n"
<< "作者:" << book.author << "\n"
<< "ISBN:" << book.isbn << "\n"
<< "价格:" << book.price << "\n"
<< "库存:" << book.quantity << "\n";
}
cout << "---------------\n";
}
int main() {
loadBooks();
while (true) {
if (currentUser.username.empty()) {
cout<<"\n===== 主界面 =====\n"
<<"1. 登录\n"
<<"2. 注册\n"
<<"3. 退出\n"
<< "请选择:";
int choice;
cin >> choice;
cin.ignore();
switch (choice) {
case 1:login(); break;
case 2:registerUser(); break;
case 3:saveData(); exit(0);
default:cout << "无效选择!" << endl;
}
}
else {
while (true) {
if (currentUser.role == "admin") {
adminMenu();
int choice;
cin >> choice;
cin.ignore();
switch (choice) {
case 1:addBook(); break;
case 2:deleteBook(); break;
case 3:modifyBook(); break;
case 4:displayAll(); break;
case 5:showStatistics(); break;
case 6:saveData(); cout << "谢谢使用!\n"; return 0;
default:cout << "无效输入,请重新输入!\n";
}
}
else {
userMenu();
int choice;
cin >> choice;
switch (choice) {
case 1:searchBook(); break;
case 2:borrowBook(); break;
case 3:returnBook; break;
case 4:showMyBorrowRecord(); break;
case 5:saveData(); cout << "谢谢使用!\n"; return 0;
default:cout << "无效输入,请重新输入!\n";
}
}
}
}
}
return 0;
}