图书管理系统C++代码
时间: 2023-09-10 14:06:06 浏览: 106
C++图书管理系统代码
5星 · 资源好评率100%
下面是一个简单的图书管理系统的C++代码示例:
```cpp
#include <iostream>
#include <vector>
#include <string>
using namespace std;
struct Book {
string title;
string author;
int year;
};
vector<Book> library;
void addBook() {
Book newBook;
cout << "Enter book title: ";
getline(cin, newBook.title);
cout << "Enter book author: ";
getline(cin, newBook.author);
cout << "Enter publication year: ";
cin >> newBook.year;
cin.ignore(); // 清除输入缓冲区中的换行符
library.push_back(newBook);
cout << "Book added successfully!" << endl;
}
void displayBooks() {
if (library.empty()) {
cout << "Library is empty." << endl;
return;
}
cout << "Books in the library:" << endl;
for (const Book& book : library) {
cout << "Title: " << book.title << endl;
cout << "Author: " << book.author << endl;
cout << "Publication Year: " << book.year << endl;
cout << "-------------------------" << endl;
}
}
int main() {
int choice;
do {
cout << endl;
cout << "========= Library Management System =========" << endl;
cout << "1. Add Book" << endl;
cout << "2. Display Books" << endl;
cout << "3. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
cin.ignore(); // 清除输入缓冲区中的换行符
switch (choice) {
case 1:
addBook();
break;
case 2:
displayBooks();
break;
case 3:
cout << "Exiting the program. Goodbye!" << endl;
break;
default:
cout << "Invalid choice. Please try again." << endl;
}
} while (choice != 3);
return 0;
}
```
这是一个简单的图书管理系统,它可以让用户添加书籍和显示已添加的书籍。用户可以通过在控制台中输入选项来执行不同的操作。
阅读全文