设计一个图书管理系统 C++
时间: 2023-09-12 10:11:18 浏览: 124
好的,下面是一个简单的图书管理系统的 C++ 代码实现:
```c++
#include<iostream>
#include<cstring>
using namespace std;
//定义一个图书类
class Book {
private:
string name; //书名
string author; //作者
string publisher; //出版社
double price; //价格
int stock; //库存
public:
Book() {}
Book(string name, string author, string publisher, double price, int stock) {
this->name = name;
this->author = author;
this->publisher = publisher;
this->price = price;
this->stock = stock;
}
//获取图书信息
string getName() {
return name;
}
string getAuthor() {
return author;
}
string getPublisher() {
return publisher;
}
double getPrice() {
return price;
}
int getStock() {
return stock;
}
//修改图书信息
void setName(string name) {
this->name = name;
}
void setAuthor(string author) {
this->author = author;
}
void setPublisher(string publisher) {
this->publisher = publisher;
}
void setPrice(double price) {
this->price = price;
}
void setStock(int stock) {
this->stock = stock;
}
};
//定义一个图书管理系统类
class BookManager {
private:
Book books[100]; //图书数组
int count; //图书数量
public:
BookManager() {
count = 0;
}
//添加图书
void addBook(Book book) {
books[count++] = book;
}
//删除图书
void deleteBook(string name) {
for (int i = 0; i < count; i++) {
if (books[i].getName() == name) {
for (int j = i; j < count - 1; j++) {
books[j] = books[j + 1];
}
count--;
break;
}
}
}
//修改图书信息
void modifyBook(string name, Book book) {
for (int i = 0; i < count; i++) {
if (books[i].getName() == name) {
books[i] = book;
break;
}
}
}
//查找图书
Book* findBook(string name) {
for (int i = 0; i < count; i++) {
if (books[i].getName() == name) {
return &books[i];
}
}
return NULL;
}
//展示图书信息
void showBooks() {
for (int i = 0; i < count; i++) {
cout << "书名:" << books[i].getName() << " 作者:" << books[i].getAuthor() << " 出版社:" << books[i].getPublisher() << " 价格:" << books[i].getPrice() << " 库存:" << books[i].getStock() << endl;
}
}
};
int main() {
BookManager bm;
bm.addBook(Book("C++程序设计", "谭浩强", "清华大学出版社", 68.0, 10));
bm.addBook(Book("Java编程思想", "Bruce Eckel", "机械工业出版社", 98.0, 5));
bm.showBooks();
bm.modifyBook("Java编程思想", Book("Java编程思想", "Bruce Eckel", "机械工业出版社", 108.0, 5));
bm.showBooks();
bm.deleteBook("C++程序设计");
bm.showBooks();
return 0;
}
```
这个系统实现了基本的添加、删除、修改、查找和展示图书信息等功能,你可以根据实际需求进行扩展。
阅读全文