C语言实现图书馆管理系统

该程序使用结构体`Book`来表示图书,其中包含书名、作者、出版商、出版年份和可用性信息。`bookList`数组存储所有图书,`numBooks`变量存储当前图书数量。

程序实现了以下功能:

- 添加书籍:向数组中添加一本书籍,输入书名、作者、出版商和出版年份;

- 搜索书籍:根据书名查找图书,并显示该书的详细信息;

- 显示所有书籍:显示所有图书的列表,包括书名、作者、出版商、出版年份和可用性信息;

- 借书:根据书名查找图书,并将其标记为不可用;

- 还书:根据书名查找图书,并将其标记为可用。

当添加的书籍超过数组大小时,程序将提示图书馆已满。当搜索到的书籍不存在或已经借出时,程序将相应提示。

该程序使用循环菜单,用户可以重复执行上述操作,直到选择退出。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_BOOKS 100

typedef struct Book {
    char title[50];
    char author[50];
    char publisher[50];
    int year;
    int available;
} Book;

Book bookList[MAX_BOOKS];
int numBooks = 0;

void displayMenu() {
    printf("Library Management Systemn");
    printf("=========================n");
    printf("1. Add a bookn");
    printf("2. Search for a bookn");
    printf("3. Display all booksn");
    printf("4. Borrow a bookn");
    printf("5. Return a bookn");
    printf("6. Exitn");
    printf("Enter your choice: ");
}

void addBook() {
    if (numBooks >= MAX_BOOKS) {
        printf("Library is full.n");
        return;
    }
    printf("Enter book title: ");
    scanf("%s", bookList[numBooks].title);
    printf("Enter book author: ");
    scanf("%s", bookList[numBooks].author);
    printf("Enter book publisher: ");
    scanf("%s", bookList[numBooks].publisher);
    printf("Enter book publication year: ");
    scanf("%d", &bookList[numBooks].year);
    bookList[numBooks].available = 1;
    numBooks++;
    printf("Book added successfully.n");
}

void searchBook() {
    char title[50];
    printf("Enter book title: ");
    scanf("%s", title);
    for (int i = 0; i < numBooks; i++) {
        if (strcmp(title, bookList[i].title) == 0) {
            printf("Title: %sn", bookList[i].title);
            printf("Author: %sn", bookList[i].author);
            printf("Publisher: %sn", bookList[i].publisher);
            printf("Publication Year: %dn", bookList[i].year);
            printf("Availability: %sn", bookList[i].available ? "Yes" : "No");
            return;
        }
    }
    printf("Book not found.n");
}

void displayBooks() {
    printf("TitletAuthortPublishertPublication YeartAvailabilityn");
    for (int i = 0; i < numBooks; i++) {
        printf("%st%st%st%dt%sn", bookList[i].title, bookList[i].author, bookList[i].publisher, bookList[i].year, bookList[i].available ? "Yes" : "No");
    }
}

void borrowBook() {
    char title[50];
    printf("Enter book title: ");
    scanf("%s", title);
    for (int i = 0; i < numBooks; i++) {
        if (strcmp(title, bookList[i].title) == 0) {
            if (bookList[i].available) {
                bookList[i].available = 0;
                printf("Book borrowed successfully.n");
            } else {
                printf("Book not available.n");
            }
            return;
        }
    }
    printf("Book not found.n");
}

void returnBook() {
    char title[50];
    printf("Enter book title: ");
    scanf("%s", title);
    for (int i = 0; i < numBooks; i++) {
        if (strcmp(title, bookList[i].title) == 0) {
            if (!bookList[i].available) {
                bookList[i].available = 1;
                printf("Book returned successfully.n");
            } else {
                printf("Book already available.n");
            }
            return;
        }
    }
    printf("Book not found.n");
}


int main() {
    int choice;
    
    do {
        displayMenu();
        scanf("%d", &choice);
        switch (choice) {
            case 1:
                addBook();
                break;
            case 2:
                searchBook();
                break;
            case 3:
                displayBooks();
                break;
            case 4:
                borrowBook();
                break;
            case 5:
                returnBook();
                break;
            case 6:
                printf("Goodbye.n");
                break;
            default:
                printf("Invalid choice.n");
                break;
            }
        } while (choice != 6);
    return 0;
}