1 Star 0 Fork 0

Admin / text_cpp

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
Clone or Download
5_15_text.cpp 1.81 KB
Copy Edit Raw Blame History
Admin authored 2024-05-16 16:22 . 作业c++(3)
#include <iostream>
#include <string>
#include <vector>
using namespace std;
// 书籍结构体
struct Book {
string title;
string author;
int year;
};
// 图书管理类
class Library {
private:
vector<Book> books; // 书籍列表
public:
// 添加书籍
void addBook(const string& title, const string& author, int year) {
Book newBook = { title, author, year };
books.push_back(newBook);
cout << "书籍添加成功" << endl;
}
// 删除书籍
void removeBook(const string& title) {
for (auto it = books.begin(); it != books.end(); ++it) {
if (it->title == title) {
books.erase(it);
cout << "书籍删除成功" << endl;
return;
}
}
cout << "未找到该书籍" << endl;
}
// 查询书籍
void searchBook(const string& title) {
for (const auto& book : books) {
if (book.title == title) {
cout << "书名:" << book.title << ", 作者:" << book.author << ", 出版年份:" << book.year << endl;
return;
}
}
cout << "未找到该书籍" << endl;
}
// 输出所有书籍
void displayBooks() {
if (books.empty()) {
cout << "图书馆暂无书籍" << endl;
}
else {
cout << "图书馆中的书籍:" << endl;
for (const auto& book : books) {
cout << "书名:" << book.title << ", 作者:" << book.author << ", 出版年份:" << book.year << endl;
}
}
}
};
int main() {
Library library;
library.addBook("C++ Primer", "Stanley B. Lippman", 2003);
library.addBook("The C Programming Language", "Brian W. Kernighan", 1978);
library.displayBooks();
library.removeBook("C++ Primer");
library.searchBook("The C Programming Language");
return 0;
}
1
https://gitee.com/small-c_2_0/text_cpp.git
git@gitee.com:small-c_2_0/text_cpp.git
small-c_2_0
text_cpp
text_cpp
master

Search