Простая система управления библиотеками на C++ с использованием массива: методы и пример кода

Я приведу пример простой системы управления библиотекой на C++ с использованием массива. Вот некоторые методы, обычно используемые в такой системе:

  1. AddBook(): этот метод позволяет пользователю добавить новую книгу в библиотеку.
void AddBook(Book library[], int& numBooks) {
    // Check if the library is already full
    if (numBooks >= MAX_LIBRARY_SIZE) {
        cout << "Library is full. Cannot add more books." << endl;
        return;
    }
// Prompt the user for book details
    cout << "Enter book title: ";
    cin.ignore();
    cin.getline(library[numBooks].title, MAX_TITLE_LENGTH);
    cout << "Enter author name: ";
    cin.getline(library[numBooks].author, MAX_AUTHOR_LENGTH);
    cout << "Enter publication year: ";
    cin >> library[numBooks].publicationYear;
    // Increment the number of books in the library
    numBooks++;
    cout << "Book added successfully." << endl;
}
  1. RemoveBook(): этот метод позволяет пользователю удалить книгу из библиотеки.
void RemoveBook(Book library[], int& numBooks, const char* title) {
    // Search for the book in the library
    int index = -1;
    for (int i = 0; i < numBooks; i++) {
        if (strcmp(library[i].title, title) == 0) {
            index = i;
            break;
        }
    }
// If the book is found, remove it
    if (index != -1) {
        // Shift the books after the removed book
        for (int i = index; i < numBooks - 1; i++) {
            library[i] = library[i + 1];
        }
// Decrement the number of books in the library
        numBooks--;
        cout << "Book removed successfully." << endl;
    } else {
        cout << "Book not found in the library." << endl;
    }
}
  1. DisplayBooks(): этот метод отображает все книги в библиотеке.
void DisplayBooks(const Book library[], int numBooks) {
    if (numBooks == 0) {
        cout << "Library is empty." << endl;
        return;
    }
    cout << "Books in the library:" << endl;
    for (int i = 0; i < numBooks; i++) {
        cout << "Title: " << library[i].title << endl;
        cout << "Author: " << library[i].author << endl;
        cout << "Publication Year: " << library[i].publicationYear << endl;
        cout << "-------------------------" << endl;
    }
}
void SearchBook(const Book library[], int numBooks, const char* title) {
    // Search for the book in the library
    bool found = false;
    for (int i = 0; i < numBooks; i++) {
        if (strcmp(library[i].title, title) == 0) {
            cout << "Book found in the library:" << endl;
            cout << "Title: " << library[i].title << endl;
            cout << "Author: " << library[i].author << endl;
            cout << "Publication Year: " << library[i].publicationYear << endl;
            found = true;
            break;
        }
    }
    if (!found) {
        cout << "Book not found in the library." << endl;
    }
}

Это всего лишь несколько методов, которые можно реализовать в системе управления библиотекой с использованием массива. Вы можете расширить это и добавить дополнительные функции в соответствии с вашими требованиями.