图书管理系统C++代码
时间: 2023-09-11 19:10:33 浏览: 71
以下是一个简单的图书管理系统的 C++ 代码,仅供参考:
```cpp
#include <iostream>
#include <cstring>
using namespace std;
const int MAX_BOOKS = 100; // 最大图书数量
// 图书类
class Book {
public:
Book(string name, string author, string publisher, int year, double price) {
this -> name = name;
this -> author = author;
this -> publisher = publisher;
this -> year = year;
this -> price = price;
}
void display() {
cout << "书名:" << name << endl;
cout << "作者:" << author << endl;
cout << "出版社:" << publisher << endl;
cout << "出版年份:" << year << endl;
cout << "价格:" << price << endl;
}
private:
string name; // 书名
string author; // 作者
string publisher; // 出版社
int year; // 出版年份
double price; // 价格
};
// 图书馆类
class Library {
public:
Library() {
book_count = 0;
}
// 添加图书
void add_book(Book book) {
if (book_count >= MAX_BOOKS) {
cout << "库存已满,无法添加" << endl;
return;
}
books[book_count++] = book;
cout << "添加成功" << endl;
}
// 显示所有图书
void display_all_books() {
if (book_count == 0) {
cout << "图书馆暂无藏书" << endl;
return;
}
for (int i = 0; i < book_count; i++) {
cout << "第" << i + 1 << "本图书:" << endl;
books[i].display();
}
}
// 查找图书
void search_book(string name) {
for (int i = 0; i < book_count; i++) {
if (books[i].name == name) {
cout << "找到了:" << endl;
books[i].display();
return;
}
}
cout << "未找到该书" << endl;
}
// 删除图书
void delete_book(string name) {
for (int i = 0; i < book_count; i++) {
if (books[i].name == name) {
for (int j = i; j < book_count - 1; j++) {
books[j] = books[j + 1];
}
book_count--;
cout << "删除成功" << endl;
return;
}
}
cout << "未找到该书" << endl;
}
private:
Book books[MAX_BOOKS]; // 图书数组
int book_count; // 图书数量
};
int main() {
Library library;
// 添加图书
Book book1("C++ Primer", "Stanley B. Lippman", "机械工业出版社", 2019, 99.0);
library.add_book(book1);
// 显示所有图书
library.display_all_books();
// 查找图书
library.search_book("C++ Primer");
// 删除图书
library.delete_book("C++ Primer");
// 再次显示所有图书
library.display_all_books();
return 0;
}
```
这个图书管理系统可以实现添加图书、显示所有图书、查找图书和删除图书的功能。