c++书管理系统源代码

以下是一个简单的C++图书管理系统的源代码示例:

#include <iostream>
#include <vector>
#include <string>

using namespace std;

class Book {
private:
    string title;
    string author;
    int year;

public:
    Book(string title, string author, int year) {
        this->title = title;
        this->author = author;
        this->year = year;
    }

    string getTitle() {
        return title;
    }

    string getAuthor() {
        return author;
    }

    int getYear() {
        return year;
    }
};

class Library {
private:
    vector<Book> books;

public:
    void addBook(Book book) {
        books.push_back(book);
    }

    void removeBook(string title) {
        for (int i = 0; i < books.size(); i++) {
            if (books[i].getTitle() == title) {
                books.erase(books.begin() + i);
                break;
            }
        }
    }

    void displayBooks() {
        for (int i = 0; i < books.size(); i++) {
            cout << "Title: " << books[i].getTitle() << endl;
            cout << "Author: " << books[i].getAuthor() << endl;
            cout << "Year: " << books[i].getYear() << endl;
            cout << endl;
        }
    }
};

int main() {
    Library library;

    // 添加书籍
    Book book1("Book 1", "Author 1", 2000);
    Book book2("Book 2", "Author 2", 2005);
    Book book3("Book 3", "Author 3", 2010);

    library.addBook(book1);
    library.addBook(book2);
    library.addBook(book3);

    // 显示所有书籍
    library.displayBooks();

    // 删除书籍
    library.removeBook("Book 2");

    // 显示更新后的书籍列表
    library.displayBooks();

    return 0;
}

这个示例代码实现了一个简单的图书管理系统,包括添加书籍、删除书籍和显示所有书籍的功能。图书类Book包含书籍的标题、作者和出版年份信息。图书馆类Library包含一个存储书籍的向量,并提供了添加、删除和显示书籍的方法。在main函数中,我们创建了几本书并添加到图书馆中,然后显示所有书籍,然后删除了一本书并再次显示更新后的书籍列表。

以下是一个简单的C++图书管理系统的源代码示例:

#include <iostream>
#include <vector>
#include <string>

using namespace std;

class Book {
private:
    string title;
    string author;
    int year;

public:
    Book(string title, string author, int year) {
        this->title = title;
        this->author = author;
        this->year = year;
    }

    string getTitle() {
        return title;
    }

    string getAuthor() {
        return author;
    }

    int getYear() {
        return year;
    }
};

class Library {
private:
    vector<Book> books;

public:
    void addBook(Book book) {
        books.push_back(book);
    }

    void removeBook(string title) {
        for (int i = 0; i < books.size(); i++) {
            if (books[i].getTitle() == title) {
                books.erase(books.begin() + i);
                break;
            }
        }
    }

    void displayBooks() {
        if (books.empty()) {
            cout << "No books in the library." << endl;
        } else {
            for (int i = 0; i < books.size(); i++) {
                cout << "Title: " << books[i].getTitle() << endl;
                cout << "Author: " << books[i].getAuthor() << endl;
                cout << "Year: " << books[i].getYear() << endl;
                cout << endl;
            }
        }
    }
};

int main() {
    Library library;

    while (true) {
        cout << "1. Add book" << endl;
        cout << "2. Remove book" << endl;
        cout << "3. Display books" << endl;
        cout << "4. Exit" << endl;
        cout << "Enter your choice: ";
        int choice;
        cin >> choice;

        if (choice == 1) {
            string title, author;
            int year;
            cout << "Enter book title: ";
            cin.ignore();
            getline(cin, title);
            cout << "Enter book author: ";
            getline(cin, author);
            cout << "Enter book year: ";
            cin >> year;
            Book book(title, author, year);
            library.addBook(book);
            cout << "Book added successfully." << endl;
        } else if (choice == 2) {
            string title;
            cout << "Enter book title: ";
            cin.ignore();
            getline(cin, title);
            library.removeBook(title);
            cout << "Book removed successfully." << endl;
        } else if (choice == 3) {
            library.displayBooks();
        } else if (choice == 4) {
            break;
        } else {
            cout << "Invalid choice. Please try again." << endl;
        }

        cout << endl;
    }

    return 0;
}

这个示例代码实现了一个简单的图书管理系统,包括添加书籍、删除书籍、显示所有书籍和退出程序的功能。在main函数中,我们使用一个无限循环来提供菜单选项,用户可以根据自己的选择执行相应的操作。用户可以输入书籍的标题、作者和出版年份来添加书籍,输入书籍的标题来删除书籍,显示所有书籍会列出图书馆中的所有书籍。用户可以选择退出程序来结束程序的运行。

请注意,这只是一个简单的示例,可能还有其他功能和错误处理需要添加。你可以根据自己的需求对代码进行修改和扩展。