以下是使用链表实现的图书管理系统的示例代码:

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

// 图书结构体
typedef struct Book {
    int id;
    char title[50];
    char author[50];
    int borrowed;
    struct Book* next;
} Book;

// 全局变量,指向链表头节点
Book* head = NULL;

// 创建新的图书节点
Book* createBook(int id, char title[], char author[]) {
    Book* newBook = (Book*)malloc(sizeof(Book));
    newBook->id = id;
    strcpy(newBook->title, title);
    strcpy(newBook->author, author);
    newBook->borrowed = 0;
    newBook->next = NULL;
    return newBook;
}

// 添加图书到链表
void addBook(int id, char title[], char author[]) {
    Book* newBook = createBook(id, title, author);
    if (head == NULL) {
        head = newBook;
    } else {
        Book* current = head;
        while (current->next != NULL) {
            current = current->next;
        }
        current->next = newBook;
    }
    printf("图书添加成功!\n");
}

// 输出所有图书信息
void displayBooks() {
    if (head == NULL) {
        printf("暂无图书信息!\n");
        return;
    }
    Book* current = head;
    printf("图书信息:\n");
    while (current != NULL) {
        printf("ID:%d\n", current->id);
        printf("书名:%s\n", current->title);
        printf("作者:%s\n", current->author);
        printf("状态:%s\n\n", current->borrowed ? "已借出" : "未借出");
        current = current->next;
    }
}

// 根据ID查找图书
Book* findBookById(int id) {
    if (head == NULL) {
        return NULL;
    }
    Book* current = head;
    while (current != NULL) {
        if (current->id == id) {
            return current;
        }
        current = current->next;
    }
    return NULL;
}

// 借阅图书
void borrowBook(int id) {
    Book* book = findBookById(id);
    if (book == NULL) {
        printf("未找到该图书!\n");
        return;
    }
    if (book->borrowed) {
        printf("该图书已借出!\n");
    } else {
        book->borrowed = 1;
        printf("借阅成功!\n");
    }
}

// 归还图书
void returnBook(int id) {
    Book* book = findBookById(id);
    if (book == NULL) {
        printf("未找到该图书!\n");
        return;
    }
    if (book->borrowed) {
        book->borrowed = 0;
        printf("归还成功!\n");
    } else {
        printf("该图书尚未借出!\n");
    }
}

// 释放链表内存
void freeBooks() {
    Book* current = head;
    while (current != NULL) {
        Book* temp = current;
        current = current->next;
        free(temp);
    }
    head = NULL;
}

int main() {
    int choice;
    int id;
    char title[50];
    char author[50];

    while (1) {
        printf("图书管理系统\n");
        printf("1. 录入图书信息\n");
        printf("2. 输出图书信息\n");
        printf("3. 查找图书信息\n");
        printf("4. 借阅图书\n");
        printf("5. 归还图书\n");
        printf("6. 退出程序\n");
        printf("请输入选项:");
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                printf("请输入图书ID:");
                scanf("%d", &id);
                printf("请输入图书标题:");
                scanf("%s", title);
                printf("请输入图书作者:");
                scanf("%s", author);
                addBook(id, title, author);
                break;
            case 2:
                displayBooks();
                break;
            case 3:
                printf("请输入图书ID:");
                scanf("%d", &id);
                Book* foundBook = findBookById(id);
                if (foundBook == NULL) {
                    printf("未找到该图书!\n");
                } else {
                    printf("ID:%d\n", foundBook->id);
                    printf("书名:%s\n", foundBook->title);
                    printf("作者:%s\n", foundBook->author);
                    printf("状态:%s\n", foundBook->borrowed ? "已借出" : "未借出");
                }
                break;
            case 4:
                printf("请输入图书ID:");
                scanf("%d", &id);
                borrowBook(id);
                break;
            case 5:
                printf("请输入图书ID:");
                scanf("%d", &id);
                returnBook(id);
                break;
            case 6:
                freeBooks();
                printf("程序已退出!\n");
                exit(0);
            default:
                printf("无效选项!\n");
        }
    }

    return 0;
}

该示例代码实现了基本的图书管理系统功能,可以通过菜单选项进行图书信息的录入、输出、查找、借阅和归还操作,并且能够从文件中读取图书信息。请按需修改和完善代码来满足实际需求

在C语言中我要用链表做图书管理系统功能有从文件中录入图书信息并输出图书信息查找图书信息借阅图书并记录归还图书退出程序

原文地址: https://www.cveoy.top/t/topic/hKAR 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录