写一个C++图书管理系统,管理员可以添加,删除,修改,显示全部图书,分为头文件和cpp文件
时间: 2023-06-25 17:08:11 浏览: 106
头文件 Book.h:
```C++
#ifndef BOOK_H
#define BOOK_H
#include <string>
class Book {
public:
Book(); // 默认构造函数
Book(const std::string& bookName, const std::string& author, int publishYear, float price); // 带参构造函数
~Book(); // 析构函数
// 获取图书名、作者、出版年份和价格
std::string getBookName() const;
std::string getAuthor() const;
int getPublishYear() const;
float getPrice() const;
// 设置图书名、作者、出版年份和价格
void setBookName(const std::string& bookName);
void setAuthor(const std::string& author);
void setPublishYear(int publishYear);
void setPrice(float price);
private:
std::string bookName_; // 图书名
std::string author_; // 作者
int publishYear_; // 出版年份
float price_; // 价格
};
#endif // BOOK_H
```
cpp文件 Book.cpp:
```C++
#include "Book.h"
// 默认构造函数
Book::Book()
: bookName_(""), author_(""), publishYear_(0), price_(0.0) {
}
// 带参构造函数
Book::Book(const std::string& bookName, const std::string& author, int publishYear, float price)
: bookName_(bookName), author_(author), publishYear_(publishYear), price_(price) {
}
// 析构函数
Book::~Book() {
}
// 获取图书名
std::string Book::getBookName() const {
return bookName_;
}
// 获取作者
std::string Book::getAuthor() const {
return author_;
}
// 获取出版年份
int Book::getPublishYear() const {
return publishYear_;
}
// 获取价格
float Book::getPrice() const {
return price_;
}
// 设置图书名
void Book::setBookName(const std::string& bookName) {
bookName_ = bookName;
}
// 设置作者
void Book::setAuthor(const std::string& author) {
author_ = author;
}
// 设置出版年份
void Book::setPublishYear(int publishYear) {
publishYear_ = publishYear;
}
// 设置价格
void Book::setPrice(float price) {
price_ = price;
}
```
头文件 BookList.h:
```C++
#ifndef BOOKLIST_H
#define BOOKLIST_H
#include "Book.h"
#include <vector>
class BookList {
public:
BookList(); // 默认构造函数
~BookList(); // 析构函数
// 添加图书
void addBook(const Book& book);
// 删除图书
void deleteBook(int index);
// 修改图书
void modifyBook(int index, const Book& book);
// 显示全部图书
void showAllBooks();
private:
std::vector<Book> bookList_; // 图书列表
};
#endif // BOOKLIST_H
```
cpp文件 BookList.cpp:
```C++
#include "BookList.h"
#include <iostream>
// 默认构造函数
BookList::BookList() {
}
// 析构函数
BookList::~BookList() {
}
// 添加图书
void BookList::addBook(const Book& book) {
bookList_.push_back(book);
std::cout << "添加成功!" << std::endl;
}
// 删除图书
void BookList::deleteBook(int index) {
if (index >= 0 && index < bookList_.size()) {
bookList_.erase(bookList_.begin() + index);
std::cout << "删除成功!" << std::endl;
} else {
std::cout << "删除失败,索引越界!" << std::endl;
}
}
// 修改图书
void BookList::modifyBook(int index, const Book& book) {
if (index >= 0 && index < bookList_.size()) {
bookList_[index] = book;
std::cout << "修改成功!" << std::endl;
} else {
std::cout << "修改失败,索引越界!" << std::endl;
}
}
// 显示全部图书
void BookList::showAllBooks() {
for (int i = 0; i < bookList_.size(); i++) {
std::cout << "索引:" << i << std::endl;
std::cout << "图书名:" << bookList_[i].getBookName() << std::endl;
std::cout << "作者:" << bookList_[i].getAuthor() << std::endl;
std::cout << "出版年份:" << bookList_[i].getPublishYear() << std::endl;
std::cout << "价格:" << bookList_[i].getPrice() << std::endl;
std::cout << std::endl;
}
}
```
主函数 main.cpp:
```C++
#include "BookList.h"
#include <iostream>
#include <string>
int main() {
BookList bookList; // 创建图书列表对象
while (true) {
std::cout << "请选择操作:" << std::endl;
std::cout << "1. 添加图书" << std::endl;
std::cout << "2. 删除图书" << std::endl;
std::cout << "3. 修改图书" << std::endl;
std::cout << "4. 显示全部图书" << std::endl;
std::cout << "0. 退出程序" << std::endl;
int choice = 0;
std::cin >> choice;
switch (choice) {
case 1: { // 添加图书
std::string bookName;
std::string author;
int publishYear;
float price;
std::cout << "请输入图书名:" << std::endl;
std::cin >> bookName;
std::cout << "请输入作者:" << std::endl;
std::cin >> author;
std::cout << "请输入出版年份:" << std::endl;
std::cin >> publishYear;
std::cout << "请输入价格:" << std::endl;
std::cin >> price;
Book book(bookName, author, publishYear, price);
bookList.addBook(book);
break;
}
case 2: { // 删除图书
int index;
std::cout << "请输入要删除的图书索引:" << std::endl;
std::cin >> index;
bookList.deleteBook(index);
break;
}
case 3: { // 修改图书
int index;
std::cout << "请输入要修改的图书索引:" << std::endl;
std::cin >> index;
std::string bookName;
std::string author;
int publishYear;
float price;
std::cout << "请输入新的图书名:" << std::endl;
std::cin >> bookName;
std::cout << "请输入新的作者:" << std::endl;
std::cin >> author;
std::cout << "请输入新的出版年份:" << std::endl;
std::cin >> publishYear;
std::cout << "请输入新的价格:" << std::endl;
std::cin >> price;
Book book(bookName, author, publishYear, price);
bookList.modifyBook(index, book);
break;
}
case 4: { // 显示全部图书
bookList.showAllBooks();
break;
}
case 0: { // 退出程序
return 0;
}
default: {
std::cout << "输入有误,请重新输入!" << std::endl;
break;
}
}
}
return 0;
}
```