c语言-设计图书管理系统

 设计图书管理系统,要求:

  假设系统可管理N本图书,每本图书包含下列信息:书名、作者、出版社、出版日期、价格,所以定义如下结构体数组:

#define  N  1000

    struct BOOK

{

      char name[30];

      char author[20];

      char press[30];

      struct date{

           int yy, mm, dd;

}pub_date;

double price;

}book[N];

 在主函数中创建上述结构体数组struct BOOK book[N],然后完成操作:

(1) 假设在磁盘上存在若干本图书的信息文件:D:\mybook\book.txt,利用子函数int import ( struct BOOK *p) 完成数据的导入,将文件中的图书信息保存到数book中,形成书库,并将导入图书的数量返回至主函数。

    其中,int import ( struct  BOOK  *p)的形参p表示书库首地址。

 (2) 根据书名查询图书。在主函数中输入待查询图书的书名str,利用子函数void query(struct BOOK *p, int n, char *str),在导入的书库中精确查找书名为str的图书,就把相应的图书信息输出至屏幕,如果未查询到,屏幕显示“没有此本图书”。

    其中,void query(struct BOOK *p,  int n,  char *str)的形参p表示书库首地址,n表示书库中图书的总数量,str表示待查询图书的书名。

仿真代码:

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

#define N 1000

// 日期结构体
struct date {
    int yy, mm, dd;
};

// 图书结构体
struct BOOK {
    char name[30];
    char author[20];
    char press[30];
    struct date pub_date;
    double price;
};

// 函数原型
int import(struct BOOK *p);
void query(struct BOOK *p, int n, char *str);

int main() {
    // 创建图书结构体数组
    struct BOOK book[N];

    // 导入图书信息并获取导入的图书数量
    int bookCount = import(book);

    // 根据书名查询图书
    char searchStr[30];
    printf("请输入要查询的图书书名:");
    scanf("%s", searchStr);
    query(book, bookCount, searchStr);

    return 0;
}

// 导入图书信息
int import(struct BOOK *p) {
    FILE *file;
    int count = 0;

    // 打开文件
    file = fopen("D:\\mybook\\book.txt", "r");
    if (file == NULL) {
        printf("文件打开失败\n");
        return 0;
    }

    // 读取文件内容到图书结构体数组
    while (fscanf(file, "%s %s %s %d-%d-%d %lf",
                  p[count].name, p[count].author, p[count].press,
                  &p[count].pub_date.yy, &p[count].pub_date.mm, &p[count].pub_date.dd,
                  &p[count].price) == 7) {
        count++;
        if (count >= N) {
            printf("图书数量超过最大限制\n");
            break;
        }
    }

    // 关闭文件
    fclose(file);

    return count;
}

// 根据书名查询图书
void query(struct BOOK *p, int n, char *str) {
    int found = 0;

    // 遍历图书结构体数组,查找匹配的书名
    for (int i = 0; i < n; i++) {
        if (strcmp(p[i].name, str) == 0) {
            printf("找到匹配的图书信息:\n");
            printf("书名:%s\n", p[i].name);
            printf("作者:%s\n", p[i].author);
            printf("出版社:%s\n", p[i].press);
            printf("出版日期:%d-%02d-%02d\n", p[i].pub_date.yy, p[i].pub_date.mm, p[i].pub_date.dd);
            printf("价格:%.2lf\n", p[i].price);
            found = 1;
            break;
        }
    }

    // 如果未查询到匹配的书名,显示提示信息
    if (!found) {
        printf("没有此本图书\n");
    }
}

仿真结果:

  • 18
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值