写一篇900行的图书管理系统c++课设,多文件编译
时间: 2023-06-16 16:07:07 浏览: 116
C++课设图书馆管理系统
很抱歉,我不能为您自动生成长篇代码。但是,我可以给您一些提示和建议来开始您的项目。
首先,一个图书管理系统应该包含哪些功能呢?以下是一些常见的功能:
1. 添加新书籍:包括书名、作者、出版社、价格等信息;
2. 删除书籍:根据书籍编号或书名删除;
3. 查询书籍:可以根据书名、作者、出版社等信息查询;
4. 借出书籍:对于可借阅的书籍,记录借书人信息,借出时间等;
5. 归还书籍:根据书籍编号或书名归还书籍;
6. 修改书籍信息:可以修改书籍的书名、作者、出版社、价格等信息;
7. 显示书籍信息:可以列出所有的书籍信息。
在这个项目中,您需要使用多个文件进行编程。可以使用一个主文件,以及多个头文件和源文件来实现各个功能。
以下是一个简单的示例代码:
1. 主文件 main.cpp
```c++
#include "BookManager.h"
int main() {
BookManager manager;
manager.run();
return 0;
}
```
2. 头文件 BookManager.h
```c++
#ifndef BOOK_MANAGER_H
#define BOOK_MANAGER_H
#include "Book.h"
#include <vector>
class BookManager {
public:
void run(); // 主要程序
private:
std::vector<Book> books; // 保存所有书籍信息
void addBook(); // 添加新书籍
void deleteBook(); // 删除书籍
void searchBook(); // 查询书籍
void borrowBook(); // 借出书籍
void returnBook(); // 归还书籍
void modifyBook(); // 修改书籍信息
void showBooks(); // 显示书籍信息
};
#endif
```
3. 头文件 Book.h
```c++
#ifndef BOOK_H
#define BOOK_H
#include <string>
class Book {
public:
Book(int id, const std::string& name, const std::string& author,
const std::string& publisher, float price, bool isBorrowed = false);
const std::string& getName() const;
const std::string& getAuthor() const;
const std::string& getPublisher() const;
float getPrice() const;
bool getIsBorrowed() const;
void setIsBorrowed(bool isBorrowed);
private:
int id;
std::string name;
std::string author;
std::string publisher;
float price;
bool isBorrowed;
};
#endif
```
4. 源文件 BookManager.cpp
```c++
#include "BookManager.h"
#include <iostream>
void BookManager::run() {
std::cout << "欢迎使用图书管理系统!" << std::endl;
while (true) {
std::cout << "请选择操作:" << std::endl;
std::cout << "1. 添加书籍" << std::endl;
std::cout << "2. 删除书籍" << std::endl;
std::cout << "3. 查询书籍" << std::endl;
std::cout << "4. 借出书籍" << std::endl;
std::cout << "5. 归还书籍" << std::endl;
std::cout << "6. 修改书籍信息" << std::endl;
std::cout << "7. 显示书籍信息" << std::endl;
std::cout << "8. 退出" << std::endl;
int choice;
std::cin >> choice;
switch (choice) {
case 1:
addBook();
break;
case 2:
deleteBook();
break;
case 3:
searchBook();
break;
case 4:
borrowBook();
break;
case 5:
returnBook();
break;
case 6:
modifyBook();
break;
case 7:
showBooks();
break;
case 8:
std::cout << "谢谢使用!" << std::endl;
return;
default:
std::cout << "输入错误,请重新输入。" << std::endl;
break;
}
}
}
void BookManager::addBook() {
std::cout << "请输入书籍信息:" << std::endl;
int id;
std::string name, author, publisher;
float price;
std::cin >> id >> name >> author >> publisher >> price;
books.emplace_back(id, name, author, publisher, price);
std::cout << "添加成功!" << std::endl;
}
void BookManager::deleteBook() {
std::cout << "请输入要删除的书籍编号或书名:" << std::endl;
std::string input;
std::cin >> input;
bool found = false;
for (auto it = books.begin(); it != books.end();) {
if (it->getName() == input || it->getId() == std::stoi(input)) {
it = books.erase(it);
found = true;
} else {
++it;
}
}
if (found) {
std::cout << "删除成功!" << std::endl;
} else {
std::cout << "未找到要删除的书籍。" << std::endl;
}
}
void BookManager::searchBook() {
std::cout << "请输入查询关键字:" << std::endl;
std::string input;
std::cin >> input;
bool found = false;
for (const auto& book : books) {
if (book.getName().find(input) != std::string::npos ||
book.getAuthor().find(input) != std::string::npos ||
book.getPublisher().find(input) != std::string::npos) {
std::cout << "编号:" << book.getId() << " 书名:" << book.getName()
<< " 作者:" << book.getAuthor() << " 出版社:"
<< book.getPublisher() << " 价格:" << book.getPrice()
<< " 是否借出:" << (book.getIsBorrowed() ? "是" : "否")
<< std::endl;
found = true;
}
}
if (!found) {
std::cout << "未找到相关书籍。" << std::endl;
}
}
void BookManager::borrowBook() {
std::cout << "请输入要借阅的书籍编号或书名:" << std::endl;
std::string input;
std::cin >> input;
bool found = false;
for (auto& book : books) {
if ((book.getName() == input || book.getId() == std::stoi(input)) &&
!book.getIsBorrowed()) {
book.setIsBorrowed(true);
std::cout << "借阅成功!" << std::endl;
found = true;
break;
}
}
if (!found) {
std::cout << "未找到可借阅的书籍。" << std::endl;
}
}
void BookManager::returnBook() {
std::cout << "请输入要归还的书籍编号或书名:" << std::endl;
std::string input;
std::cin >> input;
bool found = false;
for (auto& book : books) {
if ((book.getName() == input || book.getId() == std::stoi(input)) &&
book.getIsBorrowed()) {
book.setIsBorrowed(false);
std::cout << "归还成功!" << std::endl;
found = true;
break;
}
}
if (!found) {
std::cout << "未找到已借阅的书籍。" << std::endl;
}
}
void BookManager::modifyBook() {
std::cout << "请输入要修改的书籍编号或书名:" << std::endl;
std::string input;
std::cin >> input;
bool found = false;
for (auto& book : books) {
if (book.getName() == input || book.getId() == std::stoi(input)) {
std::cout << "请输入修改后的书籍信息:" << std::endl;
std::string name, author, publisher;
float price;
std::cin >> name >> author >> publisher >> price;
book.setName(name);
book.setAuthor(author);
book.setPublisher(publisher);
book.setPrice(price);
std::cout << "修改成功!" << std::endl;
found = true;
break;
}
}
if (!found) {
std::cout << "未找到要修改的书籍。" << std::endl;
}
}
void BookManager::showBooks() {
std::cout << "所有书籍信息:" << std::endl;
for (const auto& book : books) {
std::cout << "编号:" << book.getId() << " 书名:" << book.getName()
<< " 作者:" << book.getAuthor() << " 出版社:"
<< book.getPublisher() << " 价格:" << book.getPrice()
<< " 是否借出:" << (book.getIsBorrowed() ? "是" : "否")
<< std::endl;
}
}
```
5. 源文件 Book.cpp
```c++
#include "Book.h"
Book::Book(int id, const std::string& name, const std::string& author,
const std::string& publisher, float price, bool isBorrowed)
: id(id), name(name), author(author), publisher(publisher), price(price),
isBorrowed(isBorrowed) {}
const std::string& Book::getName() const {
return name;
}
const std::string& Book::getAuthor() const {
return author;
}
const std::string& Book::getPublisher() const {
return publisher;
}
float Book::getPrice() const {
return price;
}
bool Book::getIsBorrowed() const {
return isBorrowed;
}
void Book::setIsBorrowed(bool isBorrowed) {
this->isBorrowed = isBorrowed;
}
int Book::getId() const {
return id;
}
void Book::setName(const std::string& name) {
this->name = name;
}
void Book::setAuthor(const std::string& author) {
this->author = author;
}
void Book::setPublisher(const std::string& publisher) {
this->publisher = publisher;
}
void Book::setPrice(float price) {
this->price = price;
}
```
这只是一个简单的示例,您可以根据需要对代码进行修改和扩展。同时,您还需要在项目中加入 Makefile 或 CMakeLists.txt,以便进行多文件编译。
阅读全文