C语言图书管理系统:基于链表实现增删改查

这篇文章将介绍如何使用 C 语言和链表实现一个简单的图书管理系统。

1. 功能需求

该图书管理系统应满足以下功能需求:

  • 添加图书信息
  • 删除图书信息
  • 修改图书信息
  • 查找图书信息
  • 显示所有图书信息
  • 将图书信息保存到文件
  • 从文件加载图书信息

2. 数据结构设计

我们使用链表来存储图书信息。每个节点代表一本书,包含以下信息:

typedef struct Node {
    char bookCode[20];
    char bookName[50];
    char author[50];
    char publisher[50];
    char bookkind[50];
    char bookkinds[50];
    int quantity;
    int borrowed;
    struct Node *next;
} BookNode;

BookNode *head = NULL;

3. 代码实现

3.1 从文件加载图书数据

void loadBookData() {
    FILE *file = fopen('book_storage.txt', 'r');
    if (file == NULL) {
        return;
    }

    while (!feof(file)) {
        BookNode *newNode = (BookNode *)malloc(sizeof(BookNode));
        fscanf(file, '%s', newNode->bookCode);
        fscanf(file, '%s', newNode->bookName);
        fscanf(file, '%s', newNode->author);
        fscanf(file, '%s', newNode->publisher);
        fscanf(file, '%s', newNode->bookkind);
        fscanf(file, '%s', newNode->bookkinds);
        fscanf(file, '%d', &newNode->quantity);
        fscanf(file, '%d', &newNode->borrowed);
        newNode->next = NULL;

        if (head == NULL) {
            head = newNode;
        } else {
            BookNode *current = head;
            while (current->next != NULL) {
                current = current->next;
            }
            current->next = newNode;
        }
    }

    fclose(file);
}

3.2 保存图书数据到文件

void saveBookData() {
    FILE *file = fopen('book_storage.txt', 'w');
    if (file == NULL) {
        printf('无法保存图书信息!\n');
        return;
    }

    BookNode *current = head;
    while (current != NULL) {
        fprintf(file, '%s\n', current->bookCode);
        fprintf(file, '%s\n', current->bookName);
        fprintf(file, '%s\n', current->author);
        fprintf(file, '%s\n', current->publisher);
        fprintf(file, '%s\n', current->bookkind);
        fprintf(file, '%s\n', current->bookkinds);
        fprintf(file, '%d\n', current->quantity);
        fprintf(file, '%d\n', current->borrowed);

        current = current->next;
    }

    fclose(file);
}

3.3 添加图书信息

void addBook() {
    BookNode *newNode = (BookNode *)malloc(sizeof(BookNode));
    printf('请输入图书编号:');
    scanf('%s', newNode->bookCode);
    printf('请输入书名:');
    scanf('%s', newNode->bookName);
    // ... 获取其他图书信息

    newNode->next = NULL;

    if (head == NULL) {
        head = newNode;
    } else {
        // ... 将新节点添加到链表末尾
    }

    printf('添加成功!\n');
}

3.4 删除图书信息

void deleteBook() {
    char bookCode[20];
    printf('请输入要删除的图书编号:');
    scanf('%s', bookCode);

    if (head == NULL) {
        printf('没有找到相关图书!\n');
        return;
    }

    // ... 在链表中查找并删除对应图书
}

3.5 修改图书信息

void modifyBook() {
    char bookCode[20];
    printf('请输入要修改的图书编号:');
    scanf('%s', bookCode);

    if (head == NULL) {
        printf('没有找到相关图书!\n');
        return;
    }

    // ... 在链表中查找对应图书并修改信息
}

3.6 查找图书信息

void searchBook() {
    char bookName[50];
    printf('请输入要查找的书名:');
    scanf('%s', bookName);

    if (head == NULL) {
        printf('没有找到相关图书!\n');
        return;
    }

    // ... 在链表中查找对应图书并显示信息
}

3.7 显示所有图书信息

void displayBook() {
    if (head == NULL) {
        printf('图书列表为空!\n');
        return;
    }

    // ... 遍历链表并打印每个节点的信息
}

3.8 释放链表空间

void freeBookList() {
    BookNode *current = head;
    while (current != NULL) {
        BookNode *temp = current;
        current = current->next;
        free(temp);
    }
    head = NULL;
}

3.9 主函数

int main() {
    loadBookData();

    // ... 处理用户输入和调用相应功能函数

    saveBookData();
    freeBookList();
    return 0;
}

4. 总结

本文介绍了如何使用 C 语言和链表实现一个简单的图书管理系统。你可以根据自己的需求扩展功能,例如实现借阅和归还图书等功能。


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

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