C语言图书管理系统 (自用)

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
// 定义图书结构体
struct Book
{
    int id;              // 图书ID
    char title[100];     // 书名
    char author[100];    // 作者
    char type[20];       // 图书类型 (例如: "小说", "传记", ...)
    int available;       // 是否可借,0表示已借出,1表示可借
    char borrowDate[11]; // 借阅日期 (yyyy-mm-dd)
    char dueDate[11];    // 借阅到期日期 (yyyy-mm-dd)
};

// 函数声明
void addBook(struct Book books[], int *count);
void displayBooks(struct Book books[], int count);
void searchBooks(struct Book books[], int count);
void countBooksByType(struct Book books[], int count);
void borrowBook(struct Book books[], int count);
void returnBook(struct Book books[], int count);
void saveBooks(struct Book books[], int count);
void loadBooks(struct Book books[], int *count);

int main()
{
    struct Book books[100]; // 最多管理100本书
    int bookCount = 0;

    // 加载图书数据
    loadBooks(books, &bookCount);

    int choice;
    do
    {
        system("cls");
        printf("*******************************************************\n");
        printf("*                   欢迎使用图书管理系统                 *\n");
        printf("*******************************************************\n");
        printf("1. 添加图书\n");
        printf("2. 显示图书\n");
        printf("3. 查询图书\n");
        printf("4. 统计图书分类数量\n");
        printf("5. 借阅图书\n");
        printf("6. 归还图书\n");
        printf("7. 保存数据\n");
        printf("0. 退出\n");
        printf("*******************************************************\n");
        printf("请选择操作: ");
        scanf("%d", &choice);
        switch (choice)
        {
        case 1:
            addBook(books, &bookCount);
            break;
        case 2:
            displayBooks(books, bookCount);
            break;
        case 3:
            searchBooks(books, bookCount);
            break;
        case 4:
            countBooksByType(books, bookCount);
            break;
        case 5:
            borrowBook(books, bookCount);
            break;
        case 6:
            returnBook(books, bookCount);
            break;
        case 7:
            saveBooks(books, bookCount);
            break;
        case 0:
            printf("感谢使用图书管理系统!\n");
            break;
        default:
            printf("无效的选项,请重新选择。\n");
        }
        system("pause");
    } while (choice != 0);

    return 0;
}

void addBook(struct Book books[], int *count)
{
    if (*count >= 100)
    {
        printf("图书数量已达上限,无法添加新书。\n");
        return;
    }

    struct Book newBook;
    newBook.id = *count + 1;

    printf("请输入书名: ");
    scanf(" %[^\n]s", newBook.title);

    printf("请输入作者: ");
    scanf(" %[^\n]s", newBook.author);

    printf("请输入图书类型 (例如: 小说, 传记, 科普, 散文, 工具书, 政治): ");
    scanf("%s", newBook.type);

    newBook.available = 1; // 初始状态为可借
    // 初始化日期为0-0-0-0
    strcpy(newBook.borrowDate, "0-0-0-0");
    strcpy(newBook.dueDate, "0-0-0-0");

    books[*count] = newBook;
    (*count)++;

    printf("图书添加成功!\n");
}

void displayBooks(struct Book books[], int count)
{
    if (count == 0)
    {
        printf("图书库为空。\n");
        return;
    }

    printf("图书列表:\n");
    printf("%-4s %-20s %-20s %-10s %-10s %-12s %-12s\n", "ID", "书名", "作者", "类型", "状态", "借阅日期", "到期日期");

    for (int i = 0; i < count; i++)
    {
        char statusStr[20];
        if (books[i].available == 1)
        {
            strcpy(statusStr, "可借");
        }
        else
        {
            strcpy(statusStr, "已借出");
        }

        printf("%-4d %-20s %-20s %-10s %-10s %-12s %-12s\n", books[i].id, books[i].title, books[i].author, books[i].type, statusStr, books[i].borrowDate, books[i].dueDate);
    }
}

void searchBooks(struct Book books[], int count)
{
    if (count == 0)
    {
        printf("图书库为空。\n");
        return;
    }

    int searchOption;
    printf("请选择查询方式:\n");
    printf("1. 按书名查询\n");
    printf("2. 按作者查询\n");
    printf("3. 按类型查询\n");
    printf("0. 返回主菜单\n");
    printf("请输入选项: ");
    scanf("%d", &searchOption);

    char searchStr[100];
    printf("请输入查询关键词: ");
    scanf(" %[^\n]s", searchStr);

    printf("查询结果:\n");
    printf("ID\t书名\t作者\t类型\t状态\t借阅日期\t到期日期\n");

    for (int i = 0; i < count; i++)
    {
        int match = 0;

        switch (searchOption)
        {
        case 1: // 按书名查询
            if (strstr(books[i].title, searchStr) != NULL)
            {
                match = 1;
            }
            break;

        case 2: // 按作者查询
            if (strstr(books[i].author, searchStr) != NULL)
            {
                match = 1;
            }
            break;

        case 3: // 按类型查询
            if (strstr(books[i].type, searchStr) != NULL)
            {
                match = 1;
            }
            break;

        default:
            printf("无效选项。\n");
            return;
        }

        if (match)
        {
            char statusStr[20];
            if (books[i].available == 1)
            {
                strcpy(statusStr, "可借");
            }
            else
            {
                strcpy(statusStr, "已借出");
            }

            printf("%d\t%s\t%s\t%s\t%s\t%s\t%s\n", books[i].id, books[i].title, books[i].author, books[i].type, statusStr, books[i].borrowDate, books[i].dueDate);
        }
    }
}

void countBooksByType(struct Book books[], int count)
{
    if (count == 0)
    {
        printf("图书库为空。\n");
        return;
    }

    // 定义一个数组来记录不同种类的图书
    char uniqueTypes[100][20]; // 假设最多有100种不同的图书类型
    int typeCounts[100] = {0}; // 记录每种类型的数量
    int uniqueTypeCount = 0;   // 记录不同种类的数量

    for (int i = 0; i < count; i++)
    {
        char type[20];
        strcpy(type, books[i].type);

        // 检查当前类型是否已存在于记录中
        int typeExists = 0;
        for (int j = 0; j < uniqueTypeCount; j++)
        {
            if (strcmp(uniqueTypes[j], type) == 0)
            {
                typeExists = 1;
                break;
            }
        }

        // 如果类型不存在,添加到记录中
        if (!typeExists)
        {
            strcpy(uniqueTypes[uniqueTypeCount], type);
            uniqueTypeCount++;
        }

        // 增加相应类型的数量
        for (int j = 0; j < uniqueTypeCount; j++)
        {
            if (strcmp(uniqueTypes[j], type) == 0)
            {
                typeCounts[j]++;
                break;
            }
        }
    }

    // 输出结果
    printf("图书种类及数量:\n");
    for (int i = 0; i < uniqueTypeCount; i++)
    {
        printf("%s: %d 本\n", uniqueTypes[i], typeCounts[i]);
    }
}

int isOverdue(char borrowDate[11], char dueDate[11])
{
    // 将日期字符串转换为整数 (yyyy-mm-dd)
    int borrowYear, borrowMonth, borrowDay;
    sscanf(borrowDate, "%d-%d-%d", &borrowYear, &borrowMonth, &borrowDay);

    int dueYear, dueMonth, dueDay;
    sscanf(dueDate, "%d-%d-%d", &dueYear, &dueMonth, &dueDay);

    // 比较年份、月份和日期
    if (borrowYear > dueYear ||
        (borrowYear == dueYear && borrowMonth > dueMonth) ||
        (borrowYear == dueYear && borrowMonth == dueMonth && borrowDay > dueDay))
    {
        return 1; // 逾期
    }
    else
    {
        return 0; // 未逾期
    }
}

void borrowBook(struct Book books[], int count)
{
    if (count == 0)
    {
        printf("图书库为空,无法借阅图书。\n");
        return;
    }

    int bookID;
    printf("请输入要借阅的图书ID: ");
    scanf("%d", &bookID);

    int index = -1;
    for (int i = 0; i < count; i++)
    {
        if (books[i].id == bookID)
        {
            index = i;
            break;
        }
    }

    if (index == -1)
    {
        printf("找不到指定ID的图书。\n");
        return;
    }

    if (books[index].available == 0)
    {
        printf("图书已借出,无法再次借阅。\n");
        return;
    }

    char borrowDate[11];
    printf("请输入借阅日期 (yyyy-mm-dd): ");
    scanf("%s", borrowDate);

    // 计算到期日期(借书期限为30天)
    struct tm borrowTime;
    sscanf(borrowDate, "%d-%d-%d", &borrowTime.tm_year, &borrowTime.tm_mon, &borrowTime.tm_mday);
    borrowTime.tm_year -= 1900; // 年份需要减去1900
    borrowTime.tm_mon -= 1;     // 月份从0开始计数
    time_t borrowTimestamp = mktime(&borrowTime);
    time_t dueTimestamp = borrowTimestamp + 30 * 24 * 60 * 60; // 30天的秒数

    struct tm *dueTime = localtime(&dueTimestamp);
    strftime(books[index].dueDate, 11, "%Y-%m-%d", dueTime);

    books[index].available = 0;
    strcpy(books[index].borrowDate, borrowDate);
    printf("图书借阅成功!到期日期:%s\n", books[index].dueDate);
}

void returnBook(struct Book books[], int count)
{
    if (count == 0)
    {
        printf("图书库为空,无法归还图书。\n");
        return;
    }

    int bookID;
    printf("请输入要归还的图书ID: ");
    scanf("%d", &bookID);

    int index = -1;
    for (int i = 0; i < count; i++)
    {
        if (books[i].id == bookID)
        {
            index = i;
            break;
        }
    }

    if (index == -1)
    {
        printf("找不到指定ID的图书。\n");
        return;
    }

    if (books[index].available == 1)
    {
        printf("图书未借出,无法归还。\n");
        return;
    }

    if (isOverdue(books[index].borrowDate, books[index].dueDate))
    {
        printf("图书逾期归还,需要缴纳罚款。\n");
    }
    else
    {
        printf("图书归还成功!\n");
    }

    // 清空借阅日期和到期日期
    strcpy(books[index].borrowDate, "0-0-0-0");
    strcpy(books[index].dueDate, "0-0-0-0");

    books[index].available = 1;
}

void saveBooks(struct Book books[], int count)
{
    FILE *file = fopen("library.txt", "w");
    if (file == NULL)
    {
        printf("无法保存数据。\n");
        return;
    }

    fprintf(file, "%d\n", count); // 先保存图书数量

    for (int i = 0; i < count; i++)
    {
        fprintf(file, "%d\t", books[i].id);
        fprintf(file, "%s\t", books[i].title);
        fprintf(file, "%s\t", books[i].author);
        fprintf(file, "%s\t", books[i].type);
        fprintf(file, "%d\t", books[i].available);
        fprintf(file, "%s\t", books[i].borrowDate);
        fprintf(file, "%s\n", books[i].dueDate);
    }

    fclose(file);
    printf("数据保存成功!\n");
}

void loadBooks(struct Book books[], int *count)
{
    FILE *file = fopen("library.txt", "r");
    if (file == NULL)
    {
        printf("无法加载数据。\n");
        return;
    }

    fscanf(file, "%d\n", count); // 先加载图书数量

    for (int i = 0; i < *count; i++)
    {
        fscanf(file, "%d\t", &books[i].id);
        fscanf(file, "%s\t", books[i].title);
        fscanf(file, "%s\t", books[i].author);
        fscanf(file, "%s\t", books[i].type);
        fscanf(file, "%d\t", &books[i].available);
        fscanf(file, "%s\t", books[i].borrowDate);
        fscanf(file, "%s\n", books[i].dueDate);
    }

    fclose(file);
    printf("数据加载成功!\n");
}

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weixin_45230280

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值