可以用c++写一个简单的图书管理系统吗
时间: 2023-12-03 15:03:12 浏览: 43
可以的,下面是一个简单的图书管理系统的C++代码示例:
```c++
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Book {
public:
Book(string title, string author, string ISBN, int quantity) {
this->title = title;
this->author = author;
this->ISBN = ISBN;
this->quantity = quantity;
}
string getTitle() {
return title;
}
string getAuthor() {
return author;
}
string getISBN() {
return ISBN;
}
int getQuantity() {
return quantity;
}
void setQuantity(int quantity) {
this->quantity = quantity;
}
private:
string title;
string author;
string ISBN;
int quantity;
};
class Library {
public:
void addBook(Book book) {
books.push_back(book);
}
void removeBook(string ISBN) {
for (int i = 0; i < books.size(); i++) {
if (books[i].getISBN() == ISBN) {
books.erase(books.begin() + i);
break;
}
}
}
void displayBooks() {
for (int i = 0; i < books.size(); i++) {
cout << "Title: " << books[i].getTitle() << ", Author: " << books[i].getAuthor() << ", ISBN: " << books[i].getISBN() << ", Quantity: " << books[i].getQuantity() << endl;
}
}
void borrowBook(string ISBN) {
for (int i = 0; i < books.size(); i++) {
if (books[i].getISBN() == ISBN) {
if (books[i].getQuantity() > 0) {
books[i].setQuantity(books[i].getQuantity() - 1);
cout << "Borrowed " << books[i].getTitle() << endl;
} else {
cout << "Sorry, " << books[i].getTitle() << " is out of stock" << endl;
}
break;
}
}
}
void returnBook(string ISBN) {
for (int i = 0; i < books.size(); i++) {
if (books[i].getISBN() == ISBN) {
books[i].setQuantity(books[i].getQuantity() + 1);
cout << "Returned " << books[i].getTitle() << endl;
break;
}
}
}
private:
vector<Book> books;
};
int main() {
Library library;
Book book1("The Great Gatsby", "F. Scott Fitzgerald", "978-0743273565", 5);
Book book2("To Kill a Mockingbird", "Harper Lee", "978-0446310789", 3);
Book book3("1984", "George Orwell", "978-0451524935", 2);
library.addBook(book1);
library.addBook(book2);
library.addBook(book3);
library.displayBooks();
library.borrowBook("978-0743273565");
library.borrowBook("978-0743273565");
library.borrowBook("978-0743273565");
library.borrowBook("978-0743273565");
library.borrowBook("978-0743273565");
library.returnBook("978-0743273565");
library.displayBooks();
return 0;
}
```
这个简单的图书管理系统包含了Book类和Library类。Book类是一个简单的类,包含了书的标题、作者、ISBN和数量。Library类是一个管理图书的类,包含了添加书籍、删除书籍、展示书籍、借书和还书等功能。在这个示例中,我们添加了三本书,展示了所有书,借了五本第一本书,然后还了一本第一本书,最后展示了书籍库存情况。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)