编写一个图书馆管理程序,可以实现学生借书、还书和图书查阅功能。 (1)借书证类型定义及初始化 学生一般通过本人所持的借书证借书,借书证应该有证号、学生姓名和已借书数量等基本信息。 (2)书籍结构体类型定义及初始化 每本书应该有相关信息的档案记录,比如:书号、书名、是否借出、借出日期等,其中借出日期又应该包含具体的年、月、日。 (3)设计一个菜单函数menu()来完成功能的选择,用户可以选择1~4来分别实现查询、借书、还书和退出功能. (4)各函数功能 1)search()查阅函数,根据书号进行查阅,并给出所查书目的各项信息。如果所输入的书号存在就查询该书的状态,如果是借出状态可以选择还书;如果是未借出状态则可以进一步选择借书。 2)returnbook()函数实现还书
时间: 2024-02-28 10:54:07 浏览: 88
图书馆管理程序有借书还书功能
4星 · 用户满意度95%
的功能,根据学生借书证号和书号进行还书操作。 3)borrowbook()函数实现借书的功能,根据学生借书证号和书号进行借书操作。 4)menu()函数实现用户菜单选择功能,根据用户选择的序号来执行相应的操作。
下面是一个简单的实现示例(仅供参考):
```
#include <iostream>
#include <string>
#include <vector>
#include <ctime>
using namespace std;
// 定义借书证类型
struct Card {
string id;
string name;
int borrowedCount;
};
// 定义书籍类型
struct Book {
string id;
string name;
bool borrowed;
time_t borrowedTime;
};
// 初始化借书证和书籍信息
vector<Card> cards = {
{"001", "张三", 0},
{"002", "李四", 0},
{"003", "王五", 0}
};
vector<Book> books = {
{"001", "红楼梦", false, 0},
{"002", "西游记", false, 0},
{"003", "水浒传", false, 0}
};
// 查找借书证信息
Card* findCard(string id) {
for (auto& card : cards) {
if (card.id == id) {
return &card;
}
}
return nullptr;
}
// 查找书籍信息
Book* findBook(string id) {
for (auto& book : books) {
if (book.id == id) {
return &book;
}
}
return nullptr;
}
// 查阅书籍
void search() {
string id;
cout << "请输入书号:";
cin >> id;
auto book = findBook(id);
if (book == nullptr) {
cout << "该书不存在!" << endl;
} else {
cout << "书号:" << book->id << endl;
cout << "书名:" << book->name << endl;
if (book->borrowed) {
cout << "状态:已借出" << endl;
cout << "借出时间:" << ctime(&book->borrowedTime) << endl;
cout << "是否归还?(Y/N):";
char choice;
cin >> choice;
if (choice == 'Y' || choice == 'y') {
book->borrowed = false;
book->borrowedTime = 0;
auto card = findCard(id);
card->borrowedCount--;
cout << "还书成功!" << endl;
}
} else {
cout << "状态:未借出" << endl;
cout << "是否借出?(Y/N):";
char choice;
cin >> choice;
if (choice == 'Y' || choice == 'y') {
book->borrowed = true;
book->borrowedTime = time(nullptr);
auto card = findCard(id);
card->borrowedCount++;
cout << "借书成功!" << endl;
}
}
}
}
// 还书
void returnbook() {
string cardId;
string bookId;
cout << "请输入借书证号:";
cin >> cardId;
auto card = findCard(cardId);
if (card == nullptr) {
cout << "该借书证不存在!" << endl;
} else {
cout << "请输入书号:";
cin >> bookId;
auto book = findBook(bookId);
if (book == nullptr) {
cout << "该书不存在!" << endl;
} else if (!book->borrowed) {
cout << "该书未被借出!" << endl;
} else {
book->borrowed = false;
book->borrowedTime = 0;
card->borrowedCount--;
cout << "还书成功!" << endl;
}
}
}
// 借书
void borrowbook() {
string cardId;
string bookId;
cout << "请输入借书证号:";
cin >> cardId;
auto card = findCard(cardId);
if (card == nullptr) {
cout << "该借书证不存在!" << endl;
} else {
cout << "请输入书号:";
cin >> bookId;
auto book = findBook(bookId);
if (book == nullptr) {
cout << "该书不存在!" << endl;
} else if (book->borrowed) {
cout << "该书已经被借出!" << endl;
} else {
book->borrowed = true;
book->borrowedTime = time(nullptr);
card->borrowedCount++;
cout << "借书成功!" << endl;
}
}
}
// 菜单函数
void menu() {
int choice;
do {
cout << "1.查询书籍信息" << endl;
cout << "2.借阅书籍" << endl;
cout << "3.归还书籍" << endl;
cout << "4.退出" << endl;
cout << "请选择操作:";
cin >> choice;
switch (choice) {
case 1:
search();
break;
case 2:
borrowbook();
break;
case 3:
returnbook();
break;
case 4:
cout << "再见!" << endl;
break;
default:
cout << "输入有误,请重新输入!" << endl;
break;
}
} while (choice != 4);
}
int main() {
menu();
return 0;
}
```
阅读全文