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

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

BookNode *head = NULL;

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

char line[200];
while (fgets(line, sizeof(line), file)) {
    BookNode *newNode = (BookNode *)malloc(sizeof(BookNode));
    sscanf(line, '%s', newNode->bookCode);
    fgets(line, sizeof(line), file);
    sscanf(line, '%s', newNode->bookName);
    fgets(line, sizeof(line), file);
    sscanf(line, '%s', newNode->author);
    fgets(line, sizeof(line), file);
    sscanf(line, '%s', newNode->publisher);
    fgets(line, sizeof(line), file);
    sscanf(line, '%s', newNode->bookkind);
    fgets(line, sizeof(line), file);
    sscanf(line, '%s', newNode->bookkinds);
    fgets(line, sizeof(line), file);
    sscanf(line, '%f', &newNode->price);
    fgets(line, sizeof(line), file);
    sscanf(line, '%d', &newNode->quantity);
    fgets(line, sizeof(line), file);
    sscanf(line, '%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);

} 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, '%.2f\n', current->price);
    fprintf(file, '%d\n', current->quantity);
    fprintf(file, '%d\n', current->borrowed);

    current = current->next;
}

fclose(file);

}

void addBook() { BookNode *newNode = (BookNode *)malloc(sizeof(BookNode)); printf('请输入图书编号:'); scanf('%s', newNode->bookCode); printf('请输入书名:'); scanf('%s', newNode->bookName); printf('请输入作者:'); scanf('%s', newNode->author); printf('请输入出版社:'); scanf('%s', newNode->publisher); printf('请输入图书种类:'); scanf('%s', newNode->bookkind); printf('请输入图书种类编号:'); scanf('%s', newNode->bookkinds); printf('请输入价格:'); scanf('%f', &newNode->price); printf('请输入数量:'); scanf('%d', &newNode->quantity); newNode->borrowed = 0; newNode->next = NULL;

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

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

}

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

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

if (strcmp(head->bookCode, bookCode) == 0) {
    BookNode *temp = head;
    head = head->next;
    free(temp);
    printf('删除成功!\n');
    return;
}

BookNode *current = head;
while (current->next != NULL) {
    if (strcmp(current->next->bookCode, bookCode) == 0) {
        BookNode *temp = current->next;
        current->next = temp->next;
        free(temp);
        printf('删除成功!\n');
        return;
    }
    current = current->next;
}

printf('没有找到相关图书!\n');

}

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

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

BookNode *current = head;
while (current != NULL) {
    if (strcmp(current->bookCode, bookCode) == 0) {
        printf('请输入新的书名:');
        scanf('%s', current->bookName);
        printf('请输入新的作者:');
        scanf('%s', current->author);
        printf('请输入新的出版社:');
        scanf('%s', current->publisher);
        printf('请输入新的图书种类:');
        scanf('%s', current->bookkind);
        printf('请输入新的图书种类编号:');
        scanf('%s', current->bookkinds);
        printf('请输入新的价格:');
        scanf('%f', &current->price);
        printf('请输入新的数量:');
        scanf('%d', &current->quantity);
        printf('修改成功!\n');
        return;
    }
    current = current->next;
}

printf('没有找到相关图书!\n');

}

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

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

BookNode *current = head;
int found = 0;
while (current != NULL) {
    if (strcmp(current->bookName, bookName) == 0) {
        printf('图书编号:%s\n', current->bookCode);
        printf('书名:%s\n', current->bookName);
        printf('作者:%s\n', current->author);
        printf('出版社:%s\n', current->publisher);
        printf('图书种类:%s\n', current->bookkind);
        printf('图书种类编号:%s\n', current->bookkinds);
        printf('价格:%.2f\n', current->price);
        printf('数量:%d\n', current->quantity);
        printf('借阅状态:%s\n', current->borrowed ? '已借出' : '未借出');
        found = 1;
    }
    current = current->next;
}

if (!found) {
    printf('没有找到相关图书!\n');
}

}

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

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

BookNode *current = head;
while (current != NULL) {
    if (strcmp(current->bookCode, bookCode) == 0) {
        if (current->borrowed) {
            printf('该图书已借出!\n');
        } else {
            current->borrowed = 1;
            printf('借阅成功!\n');
        }
        return;
    }
    current = current->next;
}

printf('没有找到相关图书!\n');

}

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

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

BookNode *current = head;
while (current != NULL) {
    if (strcmp(current->bookCode, bookCode) == 0) {
        if (current->borrowed) {
            current->borrowed = 0;
            printf('归还成功!\n');
        } else {
            printf('该图书未借出!\n');
        }
        return;
    }
    current = current->next;
}

printf('没有找到相关图书!\n');

}

void displayAllBooks() { if (head == NULL) { printf('没有图书信息!\n'); return; }

BookNode *current = head;
while (current != NULL) {
    printf('图书编号:%s\n', current->bookCode);
    printf('书名:%s\n', current->bookName);
    printf('作者:%s\n', current->author);
    printf('出版社:%s\n', current->publisher);
    printf('图书种类:%s\n', current->bookkind);
    printf('图书种类编号:%s\n', current->bookkinds);
    printf('价格:%.2f\n', current->price);
    printf('数量:%d\n', current->quantity);
    printf('借阅状态:%s\n', current->borrowed ? '已借出' : '未借出');
    printf('\n');
    current = current->next;
}

} int main() { loadBookData();

int choice;
while (1) {
    printf('\n');
    printf('1. 添加图书\n');
    printf('2. 删除图书\n');
    printf('3. 修改图书信息\n');
    printf('4. 查找图书\n');
    printf('5. 借阅图书\n');
    printf('6. 归还图书\n');
    printf('7. 显示所有图书\n');
    printf('8. 退出\n');
    printf('请选择操作:');
    scanf('%d', &choice);

    switch (choice) {
        case 1:
            addBook();
            saveBookData();
            break;
        case 2:
            deleteBook();
            saveBookData();
            break;
        case 3:
            modifyBook();
            saveBookData();
            break;
        case 4:
            searchBook();
            break;
        case 5:
            borrowBook();
            saveBookData();
            break;
        case 6:
            returnBook();
            saveBookData();
            break;
        case 7:
            displayAllBooks();
            break;
        case 8:
            saveBookData();
            exit(0);
        default:
            printf('无效的选择!\n');
            break;
    }
}

return 0;
C语言图书管理系统:添加、删除、修改、查找、借阅、归还、显示所有图书

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

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