数据结构图书借阅管理系统c++
时间: 2023-07-06 07:25:54 浏览: 82
好的,我可以为您提供一个简单的数据结构图书借阅管理系统C++的代码示例。以下是一个基于控制台的实现:
```c++
#include <iostream>
#include <vector>
#include <string>
#include <ctime>
using namespace std;
class Book {
public:
string title;
string author;
string publisher;
string publishDate;
string ISBN;
double price;
bool borrowed; // 是否被借出
Book(string t, string a, string p, string pd, string i, double pr) {
title = t;
author = a;
publisher = p;
publishDate = pd;
ISBN = i;
price = pr;
borrowed = false; // 默认未借出
}
};
class Student {
public:
string id;
string name;
vector<Book*> borrowedBooks; // 借阅的图书列表
Student(string i, string n) {
id = i;
name = n;
}
void borrowBook(Book* book) {
borrowedBooks.push_back(book);
book->borrowed = true;
}
void returnBook(Book* book) {
for (auto it = borrowedBooks.begin(); it != borrowedBooks.end(); it++) {
if (*it == book) {
borrowedBooks.erase(it);
book->borrowed = false;
break;
}
}
}
};
class Library {
public:
vector<Book*> books; // 图书列表
vector<Student*> students; // 学生列表
vector<pair<Student*, Book*>> borrowedRecords; // 借阅记录列表
void addBook(Book* book) {
books.push_back(book);
}
void addStudent(Student* student) {
students.push_back(student);
}
void borrowBook(Student* student, Book* book) {
if (book->borrowed) { // 如果书已经被借出
cout << "Sorry, the book has been borrowed by another student." << endl;
} else {
student->borrowBook(book);
borrowedRecords.push_back(make_pair(student, book));
cout << "Borrow success." << endl;
}
}
void returnBook(Student* student, Book* book) {
student->returnBook(book);
borrowedRecords.erase(remove_if(borrowedRecords.begin(), borrowedRecords.end(),
[&](pair<Student*, Book*> record) -> bool {
return record.first == student && record.second == book;
}), borrowedRecords.end());
cout << "Return success." << endl;
}
void printAllBooks() {
cout << "----- All Books -----" << endl;
for (auto book : books) {
cout << "Title: " << book->title << endl;
cout << "Author: " << book->author << endl;
cout << "Publisher: " << book->publisher << endl;
cout << "Publish Date: " << book->publishDate << endl;
cout << "ISBN: " << book->ISBN << endl;
cout << "Price: " << book->price << endl;
cout << "Borrowed: " << (book->borrowed ? "Yes" : "No") << endl;
cout << endl;
}
}
void printAllStudents() {
cout << "----- All Students -----" << endl;
for (auto student : students) {
cout << "ID: " << student->id << endl;
cout << "Name: " << student->name << endl;
cout << "Borrowed Books:" << endl;
for (auto book : student->borrowedBooks) {
cout << "- " << book->title << endl;
}
cout << endl;
}
}
void printAllBorrowedRecords() {
cout << "----- All Borrowed Records -----" << endl;
for (auto record : borrowedRecords) {
cout << "Student: " << record.first->name << endl;
cout << "Book: " << record.second->title << endl;
cout << endl;
}
}
};
int main() {
// 初始化图书馆
Library library;
Book* book1 = new Book("Data Structures and Algorithms in C++", "Adam Drozdek", "Cengage Learning", "2012/7/1", "978-1133608424", 99.99);
Book* book2 = new Book("Introduction to Algorithms", "Thomas H. Cormen", "MIT Press", "2009/7/31", "978-0262033848", 72.99);
Book* book3 = new Book("C++ Primer", "Stanley B. Lippman", "Addison-Wesley Professional", "2012/8/16", "978-0321714114", 49.99);
library.addBook(book1);
library.addBook(book2);
library.addBook(book3);
Student* student1 = new Student("001", "Tom");
Student* student2 = new Student("002", "Jerry");
library.addStudent(student1);
library.addStudent(student2);
// 借阅和归还图书
library.borrowBook(student1, book1);
library.borrowBook(student2, book2);
library.printAllBooks();
library.printAllStudents();
library.printAllBorrowedRecords();
library.returnBook(student1, book1);
library.borrowBook(student2, book3);
library.printAllBooks();
library.printAllStudents();
library.printAllBorrowedRecords();
// 释放内存
delete book1;
delete book2;
delete book3;
delete student1;
delete student2;
return 0;
}
```
该示例中定义了三个类:Book(图书类)、Student(学生类)和Library(图书馆类)。其中,Book类和Student类分别表示图书和学生,Library类表示图书馆,它包含了所有的图书、学生和借阅记录。
在main函数中,我们首先初始化了一个图书馆对象library,然后向其中添加了三本图书和两个学生。接着,我们借阅和归还了一些图书,并通过printAllBooks、printAllStudents和printAllBorrowedRecords函数输出了所有图书、所有学生和所有借阅记录的信息。最后,我们释放了所有动态分配的内存。
希望这个示例可以帮助您更好地了解如何使用C++实现一个简单的数据结构图书借阅管理系统。