图书管理系统2

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

#include <stdbool.h>

#include <ctype.h>

#define MAXTITL 40

#define MAXAUTL 40

#define MAXBKS 20 // 最大图书数量

char *s_gets(char *st, int n);

struct book

{ // 建立book模板

    char title[MAXTITL];

    char author[MAXAUTL];

    float value;

    int delete_flag; // 标记0为未删除 1为已删除

}; // 添加删除标记成员

int main(void)

{

    int count = 0;

    char bookname_delete[MAXTITL];

    char choice;

    FILE *fr, *fw;

    struct book arr[MAXBKS];

    fr = fopen("book.dat", "rb");

    if (fr == NULL)

    {

        printf("Failed to open file!\n");

        exit(EXIT_FAILURE);

    }

    while (count < MAXBKS && fread(&arr[count], sizeof(struct book), 1, fr) == 1)

    {

        if (count == 0)

        {

            printf("书籍信息如下:\n");

            printf("书名 作者 价格\n");

        }

        if (arr[count].delete_flag == 0)

        {

            printf("%-16s%-16s%.2f %d\n", arr[count].title, arr[count].author, arr[count].value, arr[count].delete_flag);

            count++;

        }

    }

    fclose(fr);

    if (count == MAXBKS)

    {

        printf("书籍已经放满了.");

        exit(2);

    }

    struct book *ptr = &arr[0];

    while (1)

    {

        printf("请问需要删除文件吗?(请输入y或者n):");

        int result = scanf(" %c", &choice); // 注意这里加上空格,以清除之前的缓存

        if (result == 1)

        {

            if (choice == 'y' || choice == 'Y')

            {

                printf("请输入想要删除的书籍:");

                scanf("%s", bookname_delete);

                while (getchar() != '\n')

                    continue;

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

                {

                    if (strcmp(arr[i].title, bookname_delete) == 0)

                    {

                        arr[i].delete_flag = 1;

                    }

                }

            }

            else if (choice == 'n')

            {

                break;

            }

            else

            {

                printf("请重新输入,请输入y或者n(y代需要,n代表不需要):");

            }

        }

        while (getchar() != '\n')

            continue;

    }

    printf("输入两个回车退出输入\n");

    printf("请输入书名:");

    count = 0;

    int books = 0;

    while (count < MAXBKS && s_gets((ptr + count)->title, MAXTITL) != NULL)

    {

        if ((ptr + count)->title[0] != '\0')

        {

            printf("现在请输入作者名:");

            s_gets((ptr + count)->author, MAXAUTL);

            printf("现在请输入价格:");

            scanf("%f", &(ptr + count)->value);

            (ptr + count)->delete_flag = 0;

            while (getchar() != '\n')

                continue;

            count++;

        }

        else

        {

            break;

        }

        printf("请输入书名:\n");

    }

    for (int i = 0; i < MAXBKS; i++)

    {

        if ((ptr + i)->delete_flag == 0)

        {

            books++;

        }

    }

    if (books == 0)

    {

        printf("文件中没有书");

        exit(1);

    }

    else

    {

        for (int i = 0; i < books; i++)

        {

            if (arr[i].delete_flag == 0)

                printf("%-10s的作者是%-10s,价格为%.2f\n", arr[i].title, arr[i].author, arr[i].value);

        }

    }

    fw = fopen("book_new.dat", "wb");

    if (fw == NULL)

    {

        printf("Failed to open file!\n");

        exit(EXIT_FAILURE);

    }

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

    {

        if (arr[i].delete_flag == 0)

        {

            fwrite(&arr[i], sizeof(struct book), 1, fw);

        }

    }

    fclose(fw);

    remove("book.dat");

    rename("book_new.dat", "book.dat");

    printf("程序结束.");

    return 0;

}

char *s_gets(char *st, int n)

{

    char *ret_val; // 存储 fgets 函数的返回值

    char *find; // 存储查找到的换行符的位置

    ret_val = fgets(st, n, stdin); // 从标准输入读取字符串

    if (ret_val) // 如果读取成功

    {

        find = strchr(st, '\n'); // 查找换行符

        if (find) // 如果找到了换行符

            *find = '\0'; // 在此处放置一个空字符, 表示字符串的结束

        else // 如果没有找到换行符

            while (getchar() != '\n') // 丢弃多余的输入字符

                continue;

    }

    return ret_val; // 返回字符串的起始地址

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
毕业设计,基于SpringBoot+Vue+MySQL开发的公寓报修管理系统,源码+数据库+毕业论文+视频演示 现代经济快节奏发展以及不断完善升级的信息化技术,让传统数据信息的管理升级为软件存储,归纳,集中处理数据信息的管理方式。本公寓报修管理系统就是在这样的大环境下诞生,其可以帮助管理者在短时间内处理完毕庞大的数据信息,使用这种软件工具可以帮助管理人员提高事务处理效率,达到事半功倍的效果。此公寓报修管理系统利用当下成熟完善的Spring Boot框架,使用跨平台的可开发大型商业网站的Java语言,以及最受欢迎的RDBMS应用软件之一的MySQL数据库进行程序开发。公寓报修管理系统有管理员,住户,维修人员。管理员可以管理住户信息和维修人员信息,可以审核维修人员的请假信息,住户可以申请维修,可以对维修结果评价,维修人员负责住户提交的维修信息,也可以请假。公寓报修管理系统的开发根据操作人员需要设计的界面简洁美观,在功能模块布局上跟同类型网站保持一致,程序在实现基本要求功能时,也为数据信息面临的安全问题提供了一些实用的解决方案。可以说该程序在帮助管理者高效率地处理工作事务的同时,也实现了数据信息的整体化,规范化与自动化。 关键词:公寓报修管理系统;Spring Boot框架;MySQL;自动化;VUE
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值