c++图书馆管理系统源代码
时间: 2024-04-07 12:26:35 浏览: 247
c++管理系统图书馆管理系统代码.zip
C++图书馆管理系统是一个常见的项目,它可以帮助图书馆管理图书的借阅、归还、查询等功能。下面是一个简单的C++图书馆管理系统的源代码示例:
```cpp
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Book {
private:
string title;
string author;
bool available;
public:
Book(string title, string author) {
this->title = title;
this->author = author;
this->available = true;
}
string getTitle() {
return title;
}
string getAuthor() {
return author;
}
bool isAvailable() {
return available;
}
void setAvailable(bool available) {
this->available = available;
}
};
class Library {
private:
vector<Book> books;
public:
void addBook(Book book) {
books.push_back(book);
}
void displayBooks() {
cout << "Books in the library:" << endl;
for (int i = 0; i < books.size(); i++) {
cout << "Title: " << books[i].getTitle() << ", Author: " << books[i].getAuthor();
if (books[i].isAvailable()) {
cout << " (Available)" << endl;
} else {
cout << " (Not Available)" << endl;
}
}
}
void borrowBook(string title) {
for (int i = 0; i < books.size(); i++) {
if (books[i].getTitle() == title && books[i].isAvailable()) {
books[i].setAvailable(false);
cout << "Successfully borrowed the book: " << title << endl;
return;
}
}
cout << "The book " << title << " is not available for borrowing." << endl;
}
void returnBook(string title) {
for (int i = 0; i < books.size(); i++) {
if (books[i].getTitle() == title && !books[i].isAvailable()) {
books[i].setAvailable(true);
cout << "Successfully returned the book: " << title << endl;
return;
}
}
cout << "The book " << title << " is not valid or already returned." << endl;
}
};
int main() {
Library library;
// 添加书籍
Book book1("Book 1", "Author 1");
Book book2("Book 2", "Author 2");
Book book3("Book 3", "Author 3");
library.addBook(book1);
library.addBook(book2);
library.addBook(book3);
// 显示所有书籍
library.displayBooks();
// 借阅书籍
library.borrowBook("Book 1");
// 再次显示所有书籍
library.displayBooks();
// 归还书籍
library.returnBook("Book 1");
// 再次显示所有书籍
library.displayBooks();
return 0;
}
```
这个示例代码实现了一个简单的图书馆管理系统,包括添加书籍、显示所有书籍、借阅书籍和归还书籍等功能。你可以根据自己的需求进行修改和扩展。
阅读全文