|
| 1 | +#include <stdio.h> |
| 2 | +#include <string.h> |
| 3 | + |
| 4 | +#define MAX_BOOKS 100 |
| 5 | +#define MAX_USERS 50 |
| 6 | + |
| 7 | +// Structure to represent a book |
| 8 | +struct Book { |
| 9 | + int bookId; |
| 10 | + char title[100]; |
| 11 | + char author[50]; |
| 12 | + int availableCopies; |
| 13 | +}; |
| 14 | + |
| 15 | +// Structure to represent a user |
| 16 | +struct User { |
| 17 | + int userId; |
| 18 | + char name[50]; |
| 19 | + int booksBorrowed; |
| 20 | + int borrowedBookIds[5]; // Assuming a user can borrow up to 5 books |
| 21 | +}; |
| 22 | + |
| 23 | +// Function to display book details |
| 24 | +void displayBook(struct Book book) { |
| 25 | + printf("Book ID: %d\nTitle: %s\nAuthor: %s\nAvailable Copies: %d\n\n", |
| 26 | + book.bookId, book.title, book.author, book.availableCopies); |
| 27 | +} |
| 28 | + |
| 29 | +// Function to display user details |
| 30 | +void displayUser(struct User user) { |
| 31 | + printf("User ID: %d\nName: %s\nBooks Borrowed: %d\n", |
| 32 | + user.userId, user.name, user.booksBorrowed); |
| 33 | + |
| 34 | + printf("Borrowed Book IDs: "); |
| 35 | + for (int i = 0; i < user.booksBorrowed; ++i) { |
| 36 | + printf("%d ", user.borrowedBookIds[i]); |
| 37 | + } |
| 38 | + printf("\n\n"); |
| 39 | +} |
| 40 | + |
| 41 | +// Function to search for a book by title |
| 42 | +int searchBookByTitle(struct Book books[], int numBooks, char searchTitle[]) { |
| 43 | + for (int i = 0; i < numBooks; ++i) { |
| 44 | + if (strcmp(books[i].title, searchTitle) == 0) { |
| 45 | + return i; // Book found, return its index |
| 46 | + } |
| 47 | + } |
| 48 | + return -1; // Book not found |
| 49 | +} |
| 50 | + |
| 51 | +// Function to borrow a book |
| 52 | +void borrowBook(struct Book books[], int numBooks, struct User *user, int bookId) { |
| 53 | + int index = bookId - 1; // Assuming book IDs start from 1 |
| 54 | + |
| 55 | + if (index >= 0 && index < numBooks && books[index].availableCopies > 0) { |
| 56 | + // Book is available, borrow it |
| 57 | + user->borrowedBookIds[user->booksBorrowed] = bookId; |
| 58 | + user->booksBorrowed++; |
| 59 | + books[index].availableCopies--; |
| 60 | + |
| 61 | + printf("Book borrowed successfully.\n"); |
| 62 | + } else { |
| 63 | + printf("Book not available for borrowing.\n"); |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +// Function to return a book |
| 68 | +void returnBook(struct Book books[], int numBooks, struct User *user, int bookId) { |
| 69 | + int index = bookId - 1; |
| 70 | + |
| 71 | + if (index >= 0 && index < numBooks && user->booksBorrowed > 0) { |
| 72 | + // User has borrowed this book, return it |
| 73 | + for (int i = 0; i < user->booksBorrowed; ++i) { |
| 74 | + if (user->borrowedBookIds[i] == bookId) { |
| 75 | + user->booksBorrowed--; |
| 76 | + |
| 77 | + // Increase available copies of the book |
| 78 | + books[index].availableCopies++; |
| 79 | + |
| 80 | + // Remove the book ID from the user's borrowed list |
| 81 | + for (int j = i; j < user->booksBorrowed; ++j) { |
| 82 | + user->borrowedBookIds[j] = user->borrowedBookIds[j + 1]; |
| 83 | + } |
| 84 | + |
| 85 | + printf("Book returned successfully.\n"); |
| 86 | + return; |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + printf("Book not found in user's borrowed list.\n"); |
| 92 | +} |
| 93 | + |
| 94 | +int main() { |
| 95 | + struct Book library[MAX_BOOKS] = { |
| 96 | + {1, "The Catcher in the Rye", "J.D. Salinger", 5}, |
| 97 | + {2, "To Kill a Mockingbird", "Harper Lee", 3}, |
| 98 | + // Add more books... |
| 99 | + }; |
| 100 | + |
| 101 | + struct User users[MAX_USERS] = { |
| 102 | + {1, "John Doe", 0, {}}, |
| 103 | + {2, "Jane Smith", 0, {}}, |
| 104 | + // Add more users... |
| 105 | + }; |
| 106 | + |
| 107 | + // Example usage |
| 108 | + int userIndex = 0; // Index of the user borrowing/returning books |
| 109 | + |
| 110 | + // Borrow a book |
| 111 | + borrowBook(library, MAX_BOOKS, &users[userIndex], 1); |
| 112 | + |
| 113 | + // Display user details |
| 114 | + displayUser(users[userIndex]); |
| 115 | + |
| 116 | + // Return a book |
| 117 | + returnBook(library, MAX_BOOKS, &users[userIndex], 1); |
| 118 | + |
| 119 | + // Display book details |
| 120 | + displayBook(library[0]); |
| 121 | + |
| 122 | + return 0; |
| 123 | +} |
| 124 | +Library management using c |
0 commit comments