鱼C论坛

 找回密码
 立即注册
查看: 2142|回复: 9

结构体

[复制链接]
发表于 2021-11-8 16:41:51 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 林江楠 于 2021-11-8 19:50 编辑
  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[20];
  14.         struct Date date;
  15.         char publisher[40];
  16.         struct Book *next;
  17. }book;

  18. void getInput(struct Book *book);

  19. void getInput(struct Book *book)
  20. {
  21.         printf("请输入书名:");
  22.         scanf("%s",book->title );
  23.         printf("请输入作者:");
  24.         scanf("%s",book->author );
  25.         printf("请输入价格:");
  26.         scanf("%f",&book->price );
  27.         printf("请输入日期:");
  28.         printf("请输入出版社:");
  29.         scanf("%s",book->publisher );
  30. }

  31. void addBook(struct Book **library);

  32. void addBook(struct Book **library)
  33. {
  34.         struct Book *book,*temp;
  35.        
  36.         book = (struct Book *)malloc(sizeof(struct Book));
  37.         if(book == NULL)
  38.         {
  39.                 printf("内存分配失败!\n");
  40.                 exit(1);
  41.         }
  42.         getInput(book);
  43.         if(*library != NULL)
  44.         {
  45.                 temp = *library;
  46.                 *library = book;
  47.                 book->next = temp;
  48.         }
  49.         else
  50.         {
  51.                 *library = book;
  52.                 book->next = NULL;
  53.         }
  54. }
  55. void printLibrary(struct Book *library)
  56. {
  57.         struct Book *book;
  58.         int count = 1;
  59.        
  60.         book = library;
  61.         while(book != NULL)
  62.         {
  63.                 printf("Book%d:\n",count);
  64.                 printf("书名:%s\n",book->title);
  65.                 printf("作者:%s",book->author);
  66.                 printf("价格:%.2f",book->price);
  67.                 printf("日期:%d-%d-%d",book.date.year ,book.date.month ,book.date.day);
  68.                 printf("出版社:%s",book->publisher);
  69.                 book = book->next ;
  70.                 count++;
  71.         }
  72. }
  73. void releaseLibrary(struct Book *library)
  74. {
  75.         while(library != NULL)
  76.         {
  77.                 free(library);
  78.                 library = library->next ;
  79.                
  80.         }
  81. }
  82. int main(void)
  83. {
  84.         struct Book *library = NULL;
  85.         int ch;
  86.        
  87.         while(1)
  88.         {
  89.                 printf("请问是否要录入书籍信息(Y/N):");
  90.                 do
  91.                 {
  92.                         ch = getchar();
  93.                 }while(ch != 'Y'&&ch != 'N');
  94.                
  95.                 if(ch == 'Y')
  96.                 {
  97.                         addBook(&library);
  98.                 }
  99.                 else
  100.                 {
  101.                         break;
  102.                 }
  103.         }
  104.        
  105.         printf("请问是否需要打印图书馆信息(Y/N):");
  106.         do
  107.         {
  108.                 ch = getchar();
  109.         }while(ch != 'Y' && ch!= 'N');
  110.         if(ch== 'Y')
  111.         {
  112.                 printLibrary(library);
  113.         }
  114.         releaseLibrary(library);
  115.        
  116.         return 0;
  117. }
复制代码




报错显示date不是一个结构体或共用体

想问一下怎末改一下


这程序可以使用结构体数组来实现吗?

getInput()函数需要传入的是一个地址,book是结构体的变量名,不是一个地址呀,问什么不会出错,而加上&就会出错


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

使用道具 举报

发表于 2021-11-8 16:51:15 | 显示全部楼层
没有看代码逻辑,就仅仅只是能够通过编译,没有警告
不知道运行怎么样
  1. #include <stdio.h>
  2. #include <stdlib.h>

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

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

  16. void getInput(struct Book *book);

  17. void getInput(struct Book *book) {
  18.     printf("请输入书名:");
  19.     scanf("%s", book->title);
  20.     printf("请输入作者:");
  21.     scanf("%s", book->author);
  22.     printf("请输入价格:");
  23.     //scanf("%f", &book->price);
  24.     scanf("%f", book->price);
  25.     printf("请输入日期:");
  26.     printf("请输入出版社:");
  27.     scanf("%s", book->publisher);
  28. }

  29. void addBook(struct Book **library);

  30. void addBook(struct Book **library) {
  31.     struct Book *book, *temp;

  32.     book = (struct Book *)malloc(sizeof(struct Book));
  33.     if(book == NULL) {
  34.         printf("内存分配失败!\n");
  35.         exit(1);
  36.     }
  37.     getInput(book);
  38.     if(*library != NULL) {
  39.         temp = *library;
  40.         *library = book;
  41.         book->next = temp;
  42.     } else {
  43.         *library = book;
  44.         book->next = NULL;
  45.     }
  46. }
  47. void printLibrary(struct Book *library) {
  48.     struct Book *book;
  49.     int count = 1;

  50.     book = library;
  51.     while(book != NULL) {
  52.         printf("Book%d:\n", count);
  53.         printf("书名:%s\n", book->title);
  54.         printf("作者:%s", book->author);
  55.         //printf("价格:%.2f", book->price);
  56.         printf("价格:%.2f", book->price[0]);
  57.         printf("价格:%p", book->price);
  58.         //printf("日期:%d-%d-%d", book.date.year, book.date.month, book.date.day);
  59.         printf("日期:%d-%d-%d", book->date.year, book->date.month, book->date.day);
  60.         printf("出版社:%s", book->publisher);
  61.         book = book->next;
  62.         count++;
  63.     }
  64. }
  65. void releaseLibrary(struct Book *library) {
  66.     while(library != NULL) {
  67.         free(library);
  68.         library = library->next;
  69.     }
  70. }
  71. int main(void) {
  72.     struct Book *library = NULL;
  73.     int ch;

  74.     while(1) {
  75.         printf("请问是否要录入书籍信息(Y/N):");
  76.         do {
  77.             ch = getchar();
  78.         } while(ch != 'Y' && ch != 'N');

  79.         if(ch == 'Y') {
  80.             addBook(&library);
  81.         } else {
  82.             break;
  83.         }
  84.     }

  85.     printf("请问是否需要打印图书馆信息(Y/N):");
  86.     do {
  87.         ch = getchar();
  88.     } while(ch != 'Y' && ch != 'N');
  89.     if(ch == 'Y') {
  90.         printLibrary(library);
  91.     }
  92.     releaseLibrary(library);

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

使用道具 举报

发表于 2021-11-8 17:03:43 | 显示全部楼层
  1. #include <stdio.h>
  2. #include <stdlib.h>

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

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

  17. //void getInput(struct Book *book);

  18. void getInput(struct Book *book) {
  19.     printf("请输入书名:");
  20.     scanf("%s", book->title);
  21.     printf("请输入作者:");
  22.     scanf("%s", book->author);
  23.     printf("请输入价格:");
  24.     //scanf("%f", &book->price);
  25.     //scanf("%f", book->price);
  26.     scanf("%f", &book->price);
  27.     printf("请输入日期:");
  28.     // 输入呢?
  29.     scanf("%d-%d-%d", &book->date.year, &book->date.month, &book->date.day);
  30.     printf("请输入出版社:");
  31.     scanf("%s", book->publisher);
  32. }

  33. //void addBook(struct Book **library);

  34. void addBook(struct Book **library) {
  35.     //struct Book *book, *temp;
  36.     struct Book *book;

  37.     //book = (struct Book *)malloc(sizeof(struct Book));
  38.     book = malloc(sizeof(struct Book));
  39.     if(book == NULL) {
  40.         printf("内存分配失败!\n");
  41.         exit(1);
  42.     }
  43.     getInput(book);
  44.     /*
  45.     if(*library != NULL) {
  46.         temp = *library;
  47.         *library = book;
  48.         book->next = temp;
  49.     } else {
  50.         *library = book;
  51.         book->next = NULL;
  52.     }
  53.     */
  54.     book->next = *library;
  55.     *library = book;
  56. }

  57. void printLibrary(struct Book *library) {
  58.     struct Book *book;
  59.     int count = 1;

  60.     book = library;
  61.     while(book != NULL) {
  62.         printf("Book%d:\n", count);
  63.         printf("书名:%s\n", book->title);
  64.         printf("作者:%s", book->author);
  65.         //printf("价格:%.2f", book->price);
  66.         //printf("价格:%.2f", book->price[0]);
  67.         printf("价格:%.2f", book->price);
  68.         //printf("日期:%d-%d-%d", book.date.year, book.date.month, book.date.day);
  69.         printf("日期:%d-%d-%d", book->date.year, book->date.month, book->date.day);
  70.         printf("出版社:%s", book->publisher);
  71.         book = book->next;
  72.         count++;
  73.     }
  74.     printf("\n");
  75. }

  76. /*
  77. void releaseLibrary(struct Book *library) {
  78.     while(library != NULL) {
  79.         free(library);
  80.         library = library->next;
  81.     }
  82. }
  83. */

  84. void releaseLibrary(struct Book *library) {
  85.     if(!library) return;
  86.     releaseLibrary(library->next);
  87.     free(library);
  88. }

  89. int main(void) {
  90.     struct Book *library = NULL;
  91.     int ch;

  92.     while(1) {
  93.         printf("请问是否要录入书籍信息(Y/N):");
  94.         do {
  95.             ch = getchar();
  96.         } while(ch != 'Y' && ch != 'N');

  97.         if(ch == 'Y') {
  98.             addBook(&library);
  99.         } else {
  100.             break;
  101.         }
  102.     }

  103.     printf("请问是否需要打印图书馆信息(Y/N):");
  104.     do {
  105.         ch = getchar();
  106.     } while(ch != 'Y' && ch != 'N');
  107.     if(ch == 'Y') {
  108.         printLibrary(library);
  109.     }
  110.     releaseLibrary(library);

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

使用道具 举报

发表于 2021-11-8 17:06:01 | 显示全部楼层
        第 73 行有错
  1.                 printf("日期:%d-%d-%d",book.date.year ,book.date.month ,book.date.day);
复制代码

        这样修改
  1.                 printf("日期:%d-%d-%d",book -> date.year , book -> date.month , book -> date.day) ; // 【修改】
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-11-8 19:19:41 | 显示全部楼层
jackz007 发表于 2021-11-8 17:06
第 73 行有错

        这样修改

为什莫不能用点号运算符呢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-11-8 19:27:45 | 显示全部楼层
林江楠 发表于 2021-11-8 19:19
为什莫不能用点号运算符呢

因为 book 是一个指针,指向结构体的指针
指针用 箭头(->)
结构体用 点 (.)

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-11-8 19:34:42 | 显示全部楼层
人造人 发表于 2021-11-8 19:27
因为 book 是一个指针,指向结构体的指针
指针用 箭头(->)
结构体用 点 (.)

记错了,我还以为指针直接可以用点号运算符,看了一眼笔记要加*进行解引用才可以,谢谢了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-11-8 19:53:55 | 显示全部楼层
知道了,book在函数中是一个结构体指针
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-11-8 19:59:32 | 显示全部楼层
林江楠 发表于 2021-11-8 19:34
记错了,我还以为指针直接可以用点号运算符,看了一眼笔记要加*进行解引用才可以,谢谢了

你的代码还有其他问题,具体看 3 楼
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-11-8 21:11:00 | 显示全部楼层
指针和结构体用的符号不一样
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-25 22:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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