有没有类似代码 推荐
时间: 2023-06-21 22:17:01 浏览: 53
这里为您提供一个简单的图书管理系统的C++代码作为参考:
```cpp
#include <iostream>
#include <string>
using namespace std;
class Book {
public:
string name; // 书名
string author; // 作者
string publisher; // 出版社
int publish_year; // 出版年份
bool borrowed; // 是否借出
string borrower; // 借阅人
Book(string n, string a, string p, int y):
name(n), author(a), publisher(p), publish_year(y), borrowed(false) {}
void info() {
cout << "书名:" << name << endl;
cout << "作者:" << author << endl;
cout << "出版社:" << publisher << endl;
cout << "出版年份:" << publish_year << endl;
if (borrowed) {
cout << "借阅人:" << borrower << endl;
} else {
cout << "未借出" << endl;
}
}
void borrow(string name) {
if (borrowed) {
cout << "此书已被借出" << endl;
} else {
borrowed = true;
borrower = name;
cout << "借阅成功" << endl;
}
}
void return_book() {
if (borrowed) {
borrowed = false;
borrower = "";
cout << "归还成功" << endl;
} else {
cout << "此书未被借出" << endl;
}
}
};
class Library {
public:
Book books[100]; // 最多管理100本书
int book_count; // 当前管理的书籍数量
Library(): book_count(0) {}
void add_book(Book book) {
if (book_count >= 100) {
cout << "图书馆已满,无法添加新书" << endl;
} else {
books[book_count] = book;
book_count++;
cout << "添加成功" << endl;
}
}
void remove_book(int index) {
if (index >= book_count) {
cout << "该书不存在" << endl;
} else {
for (int i = index; i < book_count - 1; i++) {
books[i] = books[i + 1];
}
book_count--;
cout << "删除成功" << endl;
}
}
void borrow_book(int index, string name) {
if (index >= book_count) {
cout << "该书不存在" << endl;
} else {
books[index].borrow(name);
}
}
void return_book(int index) {
if (index >= book_count) {
cout << "该书不存在" << endl;
} else {
books[index].return_book();
}
}
void list_books() {
cout << "当前图书馆有" << book_count << "本书" << endl;
for (int i = 0; i < book_count; i++) {
cout << "第" << i + 1 << "本书:" << endl;
books[i].info();
}
}
};
int main() {
Library library;
Book book1("C++ Primer", "Lippman", "机械工业出版社", 2013);
Book book2("Effective C++", "Scott Meyers", "机械工业出版社", 2005);
library.add_book(book1);
library.add_book(book2);
library.list_books();
library.borrow_book(0, "Tom");
library.borrow_book(0, "Jerry");
library.return_book(1);
library.list_books();
return 0;
}
```
以上代码仅作为参考,具体实现方式和细节可以根据自己的需求进行修改和完善。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![](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)