鱼C论坛

 找回密码
 立即注册
查看: 3177|回复: 11

我又来了了了了了了了!!!!!!!

[复制链接]
发表于 2018-10-23 20:33:10 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 Yo乄 于 2018-10-23 20:39 编辑
  1.    #include"stdio.h"
  2. #include"stdlib.h"
  3. void getInput (struct Book *book);
  4. void addBook (struct Book **library);
  5. void printLibrary(struct Book *library);
  6. void releaseLibrary(struct Book *library);
  7. struct Book
  8. {
  9.         char name[128];
  10.         char author[40];
  11.         int money;
  12.         struct Book *next;
  13. };
  14. void getInput (struct Book *book)
  15. {
  16.         printf("请输入书名:");
  17.         scanf("%s",book->name);
  18.         printf("请输入作者:");
  19.         scanf("%s",book->author);
  20.         printf("请输入售价:");
  21.         scanf("%d",book->money);
  22.        
  23. }
  24. void addBook (struct Book **library)
  25. {
  26.         struct Book *book,*temp;
  27.         book = (struct Book *)malloc(sizeof(struct Book));
  28.         if (book == NULL)
  29.         {
  30.                 printf("内存分配失败!\n");
  31.                 exit(1);
  32.          }
  33.         getInput(book);
  34.         if(*library != NULL)
  35.         {
  36.                 temp = *library;
  37.                 *library = book;
  38.                 book->next = temp;
  39.         }
  40.         else
  41.         {
  42.                 *library = book;
  43.                 book->next = NULL;               
  44.         }
  45.        
  46. }
  47. void printLibrary(struct Book *library)
  48. {
  49.         struct Book *book;
  50.         int count = 1 ;
  51.         book = library;
  52.         while (book != NUll)
  53.         {
  54.                 printf("Book%d:",count)
  55.                 printf("书名:%s",book->name);
  56.                 printf("作者:%s",book->author);
  57.                 printf("售价:%d",book->money);
  58.                 book = book->next;
  59.                 count ++ ;
  60.                
  61.          }
  62. }
  63. void releaseLibrary(struct Book *library)
  64. {
  65.         while (library!=NULL)
  66.         {
  67.                 library=library->next;
  68.                 free(library);
  69.                
  70.         }
  71. }
  72. int main()
  73. {
  74.         struct Book *library = NULL;
  75.         char ch;
  76.         while(1)
  77.         {
  78.                 printf("请问是否需要录入书籍信息(Y/N)");
  79.                 do
  80.                 {
  81.                         ch = getchar();
  82.                  } while (ch != 'Y' && ch!= 'N');
  83.                  if(ch == 'Y')
  84.                  {
  85.                          addBook(&library);
  86.                  }
  87.                  else
  88.                  {
  89.                          break;
  90.                  }
  91.         }
  92.         printf("请问是否需要打印图书信息(Y/N)");
  93.         do
  94.         {
  95.                 ch=getchar();
  96.          } while (ch!='Y'&& ch!= 'N');
  97.          if(ch == 'Y')
  98.          {
  99.                  printLibrary(library);
  100.          }
  101.          releaseLibrary(library);
  102.           
  103. //        addBook(&library);
  104.         return 0;
  105. }
复制代码


又是一片红啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-10-23 20:51:15 | 显示全部楼层
没有红了,但是 addBook 还是哪里有问题,不能继续添加,代码抄好来
1. 你的函数声明必须在结构体的定义后面,不然编译器不知道什么是 struct Book
2. NULL 不是 NUll
3. 标准库用尖箭头
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. struct Book
  4. {
  5.         char name[128];
  6.         char author[40];
  7.         int money;
  8.         struct Book *next;
  9. };

  10. void getInput(struct Book *book)
  11. {
  12.         printf("请输入书名:");
  13.         scanf("%s",book->name);
  14.         printf("请输入作者:");
  15.         scanf("%s",book->author);
  16.         printf("请输入售价:");
  17.         scanf("%d",book->money);
  18.         
  19. }
  20. void addBook(struct Book **library)
  21. {
  22.         struct Book *book,*temp;
  23.         book = (struct Book *)malloc(sizeof(struct Book));
  24.         if (book == NULL)
  25.         {
  26.                 printf("内存分配失败!\n");
  27.                 exit(1);
  28.          }
  29.         getInput(book);
  30.         if(*library != NULL)
  31.         {
  32.                 temp = *library;
  33.                 *library = book;
  34.                 book->next = temp;
  35.         }
  36.         else
  37.         {
  38.                 *library = book;
  39.                 book->next = NULL;               
  40.         }
  41.         
  42. }

  43. void printLibrary(struct Book *library)
  44. {
  45.         struct Book *book;
  46.         int count = 1 ;
  47.         book = library;
  48.         while (book != NULL)
  49.         {
  50.                 printf("Book%d:",count);
  51.                 printf("书名:%s",book->name);
  52.                 printf("作者:%s",book->author);
  53.                 printf("售价:%d",book->money);
  54.                 book = book->next;
  55.                 count++;
  56.                
  57.          }
  58. }

  59. void releaseLibrary(struct Book *library)
  60. {
  61.         while (library!=NULL)
  62.         {
  63.                 library=library->next;
  64.                 free(library);
  65.                
  66.         }
  67. }

  68. void getInput(struct Book *book);
  69. void addBook(struct Book **library);
  70. void printLibrary(struct Book *library);
  71. void releaseLibrary(struct Book *library);


  72. int main()
  73. {
  74.         struct Book *library = NULL;
  75.         char ch;
  76.         while(1)
  77.         {
  78.                 printf("请问是否需要录入书籍信息(Y/N)");
  79.                 do
  80.                 {
  81.                         ch = getchar();
  82.                  } while (ch != 'Y' && ch!= 'N');
  83.                  if(ch == 'Y')
  84.                  {
  85.                          addBook(&library);
  86.                  }
  87.                  else
  88.                  {
  89.                          break;
  90.                  }
  91.         }
  92.         printf("请问是否需要打印图书信息(Y/N)");
  93.         do
  94.         {
  95.                 ch=getchar();
  96.          } while (ch!='Y'&& ch!= 'N');
  97.          if(ch == 'Y')
  98.          {
  99.                  printLibrary(library);
  100.          }
  101.          releaseLibrary(library);
  102.          
  103. //        addBook(&library);
  104.         return 0;
  105. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2018-10-23 21:02:50 | 显示全部楼层
claws0n 发表于 2018-10-23 20:51
没有红了,但是 addBook 还是哪里有问题,不能继续添加,代码抄好来
1. 你的函数声明必须在结构体的定义后 ...

老哥   我现在编译运行   不能添加书。。  输入完第一本书的信息后回车 就卡住 然后自动完成程序
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-10-23 21:05:40 | 显示全部楼层
Yo乄 发表于 2018-10-23 21:02
老哥   我现在编译运行   不能添加书。。  输入完第一本书的信息后回车 就卡住 然后自动完成程序

嗯,我知道,所以说代码抄好来~~
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-10-23 21:06:20 | 显示全部楼层
claws0n 发表于 2018-10-23 21:05
嗯,我知道,所以说代码抄好来~~

我大概也许 差不多   应该是对着视频打的。。。。  难受啊啊啊啊啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-10-23 21:22:17 | 显示全部楼层
Yo乄 发表于 2018-10-23 21:06
我大概也许 差不多   应该是对着视频打的。。。。  难受啊啊啊啊啊

找到以前现成的,改一改吧
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. // insert from head

  4. struct Book
  5. {
  6.        char title[120];
  7.        char author[40];
  8.        struct Book *next;
  9. };

  10. void getInput(struct Book *book);
  11. void addBook(struct Book **library);
  12. void printLibrary(struct Book *library);
  13. void releaseLibrary(struct Book **library);


  14. void getInput(struct Book *book)
  15. {
  16.      printf("Enter a book name: ");
  17.      scanf("%s", book->title);
  18.      printf("Enter the author name: ");
  19.      scanf("%s", book->author);
  20. }

  21. void addBook(struct Book **library)
  22. {
  23.      struct Book *book, *temp;
  24.      
  25.      book = (struct Book *)malloc(sizeof(struct Book));
  26.      if (book == NULL)
  27.      {
  28.          printf("Memory allocation failed");
  29.          exit(1);
  30.      }
  31.      
  32.      getInput(book);
  33.      
  34.      if(*library != NULL)
  35.      {
  36.          temp = *library;
  37.          *library = book;
  38.          book->next = temp;
  39.      }
  40.      else
  41.      {
  42.          *library = book;
  43.          book->next = NULL;
  44.      }
  45. }

  46. void printLibrary(struct Book *library)
  47. {
  48.      struct Book *book;
  49.      int count = 1;
  50.      
  51.      book = library;
  52.      while (book != NULL)
  53.      {
  54.            printf("Book%d: ", count);
  55.            printf("Name: %s\t", book->title);
  56.            printf("Author: %s\n", book->author);
  57.            book = book->next;
  58.            count++;
  59.      }
  60. }

  61. void releaseLibrary(struct Book **library)
  62. {
  63.      struct Book *temp;
  64.      
  65.      while (library != NULL)
  66.      {
  67.            temp = *library;
  68.            *library = (*library)->next;
  69.            free(temp);
  70.      }
  71. }
  72.      
  73. int main(void)
  74. {
  75.     struct Book *library = NULL;
  76.     int ch;
  77.    
  78.     while (1)
  79.     {
  80.           printf("Add new book? (Y/N):");
  81.           do
  82.           {
  83.               ch = getchar();
  84.           }while (ch != 'Y' && ch != 'N');
  85.          
  86.           if (ch == 'Y')
  87.           {
  88.               addBook(&library);
  89.           }
  90.           else
  91.           {
  92.               break;
  93.           }
  94.     }
  95.    
  96.     printf("Print books? (Y/N):");
  97.     do
  98.     {
  99.         ch = getchar();
  100.     }while (ch != 'Y' && ch != 'N');
  101.          
  102.     if (ch == 'Y')
  103.     {
  104.         printLibrary(library);
  105.     }
  106.    
  107.     releaseLibrary(&library);
  108.          
  109.     system("pause");
  110.     return 0;
  111. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-10-23 21:30:29 | 显示全部楼层
Yo乄 发表于 2018-10-23 21:06
我大概也许 差不多   应该是对着视频打的。。。。  难受啊啊啊啊啊

改好了,中文的话就真的要自己改了
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. // insert from head

  4. struct Book
  5. {
  6.        char title[120];
  7.        char author[40];
  8.        float price;
  9.        struct Book *next;
  10. };

  11. void getInput(struct Book *book);
  12. void addBook(struct Book **library);
  13. void printLibrary(struct Book *library);
  14. void releaseLibrary(struct Book **library);


  15. void getInput(struct Book *book)
  16. {
  17.      printf("Enter a book name: ");
  18.      scanf("%s", book->title);
  19.      printf("Enter the author name: ");
  20.      scanf("%s", book->author);
  21.      printf("Enter the price: ");
  22.      scanf("%f", &book->price);
  23. }

  24. void addBook(struct Book **library)
  25. {
  26.      struct Book *book, *temp;
  27.      
  28.      book = (struct Book *)malloc(sizeof(struct Book));
  29.      if (book == NULL)
  30.      {
  31.          printf("Memory allocation failed");
  32.          exit(1);
  33.      }
  34.      
  35.      getInput(book);
  36.      
  37.      if(*library != NULL)
  38.      {
  39.          temp = *library;
  40.          *library = book;
  41.          book->next = temp;
  42.      }
  43.      else
  44.      {
  45.          *library = book;
  46.          book->next = NULL;
  47.      }
  48. }

  49. void printLibrary(struct Book *library)
  50. {
  51.      struct Book *book;
  52.      int count = 1;
  53.      
  54.      book = library;
  55.      while (book != NULL)
  56.      {
  57.            printf("Book%d: ", count);
  58.            printf("Name: %s\t", book->title);
  59.            printf("Author: %s\t", book->author);
  60.            printf("Price: %0.2f\n", book->price);
  61.            book = book->next;
  62.            count++;
  63.      }
  64. }

  65. void releaseLibrary(struct Book **library)
  66. {
  67.      struct Book *temp;
  68.      
  69.      while (library != NULL)
  70.      {
  71.            temp = *library;
  72.            *library = (*library)->next;
  73.            free(temp);
  74.      }
  75. }
  76.      
  77. int main(void)
  78. {
  79.     struct Book *library = NULL;
  80.     int ch;
  81.    
  82.     while (1)
  83.     {
  84.           printf("Add new book? (Y/N):");
  85.           do
  86.           {
  87.               ch = getchar();
  88.           }while (ch != 'Y' && ch != 'N');
  89.          
  90.           if (ch == 'Y')
  91.           {
  92.               addBook(&library);
  93.           }
  94.           else
  95.           {
  96.               break;
  97.           }
  98.     }
  99.    
  100.     printf("Print books? (Y/N):");
  101.     do
  102.     {
  103.         ch = getchar();
  104.     }while (ch != 'Y' && ch != 'N');
  105.          
  106.     if (ch == 'Y')
  107.     {
  108.         printLibrary(library);
  109.     }
  110.    
  111.     releaseLibrary(&library);
  112.          
  113.     system("pause");
  114.     return 0;
  115. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-10-24 10:32:33 | 显示全部楼层
行,那我也再来  还是同上次 先指出你错误 看图 ,所以改正就比上次更简单了
  1. #include"stdio.h"
  2. #include"stdlib.h"
  3. void getInput (struct Book *book);
  4. void addBook (struct Book **library);
  5. void printLibrary(struct Book *library);
  6. void releaseLibrary(struct Book *library);
  7. struct Book
  8. {
  9.         char name[128];
  10.         char author[40];
  11.         int money;
  12.         struct Book *next;
  13. };
  14. void getInput (struct Book *book)
  15. {
  16.         printf("请输入书名: ");
  17.         scanf("%s",book->name);
  18.         printf("请输入作者:");
  19.         scanf("%s",book->author);
  20.         printf("请输入售价:");
  21.         scanf("%d",&book->money);     
  22.         
  23. }
  24. void addBook (struct Book **library)
  25. {
  26.         struct Book *book,*temp;
  27.         book = (struct Book *)malloc(sizeof(struct Book));
  28.         if (book == NULL)
  29.         {
  30.                 printf("内存分配失败!\n");
  31.                 exit(1);
  32.          }
  33.         getInput(book);
  34.         if(*library != NULL)
  35.         {
  36.                 temp = *library;
  37.                 *library = book;
  38.                 book->next = temp;
  39.         }
  40.         else
  41.         {
  42.                 *library = book;
  43.                 book->next = NULL;               
  44.         }
  45.         
  46. }
  47. void printLibrary(struct Book *library)
  48. {
  49.         struct Book *book;
  50.         int count = 1 ;
  51.         book = library;
  52.         while (book != NULL)
  53.         {
  54.                 printf("Book%d:",count);
  55.                 printf("书名:%s ",book->name);
  56.                 printf("作者:%s ",book->author);
  57.                 printf("售价:%d \n",book->money);
  58.                 book = book->next;
  59.                 count ++ ;
  60.                
  61.          }
  62. }
  63. void releaseLibrary(struct Book *library)
  64. {
  65.         while (library!=NULL)
  66.         {
  67.                 library=library->next;
  68.                 free(library);
  69.                
  70.         }
  71. }
  72. int main()
  73. {
  74.         struct Book *library = NULL;
  75.         char ch;
  76.         while(1)
  77.         {
  78.                 printf("请问是否需要录入书籍信息(Y/N)  ");
  79.                 do
  80.                 {
  81.                         ch = getchar();
  82.                  } while (ch != 'Y' && ch!= 'N');
  83.                  if(ch == 'Y')
  84.                  {
  85.                          addBook(&library);
  86.                  }
  87.                  else
  88.                  {
  89.                          break;
  90.                  }
  91.         }
  92.         printf("请问是否需要打印图书信息(Y/N)  ");
  93.         do
  94.         {
  95.                 ch=getchar();
  96.          } while (ch!='Y'&& ch!= 'N');
  97.          if(ch == 'Y')
  98.          {
  99.                  printLibrary(library);
  100.          }
  101.          releaseLibrary(library);
  102.          
  103. //        addBook(&library);
  104.         return 0;
  105. }
复制代码



加油老哥

粗心害人

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

使用道具 举报

 楼主| 发表于 2018-10-24 12:10:28 | 显示全部楼层
西瓜小刚 发表于 2018-10-24 10:32
行,那我也再来  还是同上次 先指出你错误 看图 ,所以改正就比上次更简单了

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

使用道具 举报

 楼主| 发表于 2018-10-24 12:13:26 | 显示全部楼层
本帖最后由 Yo乄 于 2018-10-24 12:15 编辑
西瓜小刚 发表于 2018-10-24 10:32
行,那我也再来  还是同上次 先指出你错误 看图 ,所以改正就比上次更简单了


提问~~~~~小甲鱼视频里好像没有取址符  ····   为啥他的可以编译并正常运行//////////   我错了 。。小甲鱼的没有售价。。。。。。。。。。。。。。。。新提问 : 数组的话就可以不加取址符吗  
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-10-24 12:16:21 | 显示全部楼层
claws0n 发表于 2018-10-23 21:30
改好了,中文的话就真的要自己改了

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

使用道具 举报

发表于 2018-10-30 09:15:46 | 显示全部楼层
西瓜小刚 发表于 2018-10-24 10:32
行,那我也再来  还是同上次 先指出你错误 看图 ,所以改正就比上次更简单了

66又是这老哥基本功差一些 他还需要大量练习呀
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-11 14:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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