鱼C论坛

 找回密码
 立即注册
查看: 3170|回复: 2

求结构体数组指针构建图书馆管理系统的程序

[复制链接]
发表于 2021-3-5 11:33:21 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
感谢
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-3-5 11:49:53 | 显示全部楼层
        楼主越来越懒了,以前写了那么多结构数组的代码练习,自己拼凑个大概还做不到吗?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2021-4-10 14:31:14 | 显示全部楼层
我的数组元素是结构体,而不是结构体指针,怎么改成结构体指针呢
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. struct Date
  4. {
  5.         int year;
  6.         int month;
  7.         int day;
  8. };

  9. struct Book
  10. {
  11.         char title[128];
  12.         char author[40];
  13.         float price;
  14.         struct Date date;
  15.         char publisher[40];
  16. };

  17. void getInput(struct Book *);
  18. void printBook(struct Book *);

  19. void getInput(struct Book *book)
  20. {
  21.         printf("title: ");
  22.         scanf("%s", book->title);
  23.         printf("author: ");
  24.         scanf("%s", book->author);
  25.         printf("price: ");
  26.         scanf("%f", &book->price);
  27.         printf("date: ");
  28.         scanf("%d-%d-%d", &book->date.year, &book->date.month, &book->date.day);
  29.         printf("publisher: ");
  30.         scanf("%s", book->publisher);
  31. }

  32. void printBook(struct Book *book)
  33. {
  34.         printf("title: %s\n", book->title);
  35.         printf("author: %s\n", book->author);
  36.         printf("price: %.2f\n", book->price);
  37.         printf("date: %u\n", book->date.year, book->date.month, book->date.day);
  38.         printf("publisher: %s\n", book->publisher);
  39. }

  40. int main(void)
  41. {
  42.         int count = 1, zhiling, temp, i;
  43.         struct Book *b = NULL;

  44.         b = (struct Book *)malloc(sizeof(struct Book));
  45.         if(b == NULL)
  46.         {
  47.                 printf("Failed!\n");
  48.                 exit(1);
  49.         }

  50.         printf("Write the first book!\n");
  51.         getInput(&b[0]);

  52.         do
  53.         {
  54.                 printf("Enter an integer:(1 write a book/ 2 seek a book/ 0 leave): ");
  55.                 scanf("%d", &zhiling);

  56.                 switch(zhiling)
  57.                 {
  58.                         case 0:{
  59.                                 free(b);
  60.                                 printf("Thanks for use!\n");
  61.                                 return 0;
  62.                         }break;

  63.                         case 1:{
  64.                                 count++;
  65.                                 b = (struct Book *)realloc(b, count * sizeof(struct Book));
  66.                                 if(b == NULL)
  67.                                 {
  68.                                         printf("Failed!\n");
  69.                                         exit(1);
  70.                                 }

  71.                                 printf("This is No.%d book!\n", count);
  72.                                 getInput(&b[count - 1]);
  73.                         }break;

  74.                         case 2:{
  75.                                 for(i = 0; i < count; i++)
  76.                                 {
  77.                                         printf("No.%d book: %s\n", i + 1, b[i].title);
  78.                                 }
  79.                                 printf("Which one do you want?");
  80.                                 scanf("%d", &temp);

  81.                                 printBook(&b[temp - 1]);
  82.                         }break;
  83.                 }
  84.                 putchar('\n');
  85.         }while(1);

  86.         return 0;
  87. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-3 09:19

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表