鱼C论坛

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

[已解决]这代码是那错了,怎么只能输出第一本书,就很头疼,编译没问题,用的Vc++6.0

[复制链接]
发表于 2022-3-29 19:13:55 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 cyn.c 于 2022-3-29 19:26 编辑

这代码是那错了,怎么只能输出第一本书,就很头疼,编译没问题,用的Vc++6.0
#include<stdio.h>
#include<stdlib.h>
struct Book
{
        char title[128];
        char author[40];
        struct Book *next;
};
void printlibrary(struct Book *library);
void getInput(struct Book *book);
void addBook(struct Book **library);
void releaselibrary(struct Book *library);
void printlibrary(struct Book *library)
{
        struct Book *book;
        int count=1;
        book=library;
        if(book!=NULL)
        {
                printf("book%d:",count);
                printf("书名:%s\n",book->title);
                printf("作者:%s\n",book->author);
                book=book->next;
                count++;
        }
}
void getInput(struct Book *book)
{
        printf("请输入书名:");
        scanf("%s",book->title);
        printf("请输入作者:");
        scanf("%s",book->author);
}
void addBook(struct Book **library)
{
        struct Book *book,*temp;
        book=(struct Book *)malloc(sizeof(struct Book));
        if(book==NULL)
        {
                printf("内存分配失败!");
                exit(1);
        }
        getInput(book);
        if(*library!=NULL)
        {
                temp=*library;
                *library=book;
                book->next=temp;
        }
        else
        {
                *library=book;
                book->next=NULL;
        }
}
void releaselibrary(struct Book *library)
{
        while(library!=NULL)
        {
                library=library->next;
                free(library);
        }
}
int main(void)
{
        struct Book *library=NULL;
        int ch;
        while(1)
        {
                printf("是否需要录入书籍信息(Y/N):");
                do
                {
                        ch=getchar();
                }while(ch!='Y'&&ch!='N');
                if(ch=='Y')
                {
                        addBook(&library);
                }
                else
                {
                        break;
                }
        }
                printf("是否需要打印书籍信息(Y/N):");
       
                do
                {
                        ch=getchar();
                }while(ch!='Y'&&ch!='N');
                if(ch=='Y')
                {
                        printlibrary(library);
                }
       
                releaselibrary(library);

        return 0;

}
最佳答案
2022-3-29 19:54:13
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. struct Book
  4. {
  5.         char title[128];
  6.         char author[40];
  7.         struct Book *next;
  8. };

  9. void printlibrary(struct Book *library)
  10. {
  11.         struct Book *book;
  12.         int count=1;
  13.         book=library;
  14.         while(book!=NULL) // while
  15.         {
  16.                 printf("book%d:",count);
  17.                 printf("书名:%s\n",book->title);
  18.                 printf("作者:%s\n",book->author);
  19.                 book=book->next;
  20.                 count++;
  21.         }
  22. }
  23. void getInput(struct Book *book)
  24. {
  25.         printf("请输入书名:");
  26.         scanf("%s",book->title);
  27.         printf("请输入作者:");
  28.         scanf("%s",book->author);
  29. }
  30. void addBook(struct Book **library)
  31. {
  32.         struct Book *book;
  33.         book=(struct Book *)malloc(sizeof(struct Book));
  34.         if(book==NULL)
  35.         {
  36.                 printf("内存分配失败!");
  37.                 exit(1);
  38.         }
  39.         getInput(book);
  40.                 // 按你的思路这里使用的应该是 头插法
  41.         if(*library!=NULL)
  42.         {
  43.                 book->next=*library;
  44.                                 *library = book;
  45.         }
  46.         else
  47.         {
  48.                 *library=book;
  49.                 book->next=NULL;
  50.         }
  51. }
  52. void releaselibrary(struct Book *library)
  53. {
  54.         struct Book *temp;
  55.         while(library!=NULL)
  56.         {
  57.                         // 释放顺序  
  58.                         temp = library->next;
  59.                         free(library);
  60.                         library=temp;
  61.                        
  62.         }
  63. }
  64. int main(void)
  65. {
  66.         struct Book *library=NULL;
  67.         int ch;
  68.         while(1)
  69.         {
  70.                 printf("是否需要录入书籍信息(Y/N):");
  71.                 do
  72.                 {
  73.                         ch=getchar();
  74.                 }while(ch!='Y'&&ch!='N');
  75.                 if(ch=='Y')
  76.                 {
  77.                         addBook(&library);
  78.                 }
  79.                 else
  80.                 {
  81.                         break;
  82.                 }
  83.         }


  84.                 printf("是否需要打印书籍信息(Y/N):");
  85.       
  86.                 do
  87.                 {
  88.                         ch=getchar();
  89.                 }while(ch!='Y'&&ch!='N');
  90.                 if(ch=='Y')
  91.                 {
  92.                         printlibrary(library);
  93.                 }
  94.       
  95.                 releaselibrary(library);

  96.         return 0;

  97. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-3-29 19:54:13 | 显示全部楼层    本楼为最佳答案   
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. struct Book
  4. {
  5.         char title[128];
  6.         char author[40];
  7.         struct Book *next;
  8. };

  9. void printlibrary(struct Book *library)
  10. {
  11.         struct Book *book;
  12.         int count=1;
  13.         book=library;
  14.         while(book!=NULL) // while
  15.         {
  16.                 printf("book%d:",count);
  17.                 printf("书名:%s\n",book->title);
  18.                 printf("作者:%s\n",book->author);
  19.                 book=book->next;
  20.                 count++;
  21.         }
  22. }
  23. void getInput(struct Book *book)
  24. {
  25.         printf("请输入书名:");
  26.         scanf("%s",book->title);
  27.         printf("请输入作者:");
  28.         scanf("%s",book->author);
  29. }
  30. void addBook(struct Book **library)
  31. {
  32.         struct Book *book;
  33.         book=(struct Book *)malloc(sizeof(struct Book));
  34.         if(book==NULL)
  35.         {
  36.                 printf("内存分配失败!");
  37.                 exit(1);
  38.         }
  39.         getInput(book);
  40.                 // 按你的思路这里使用的应该是 头插法
  41.         if(*library!=NULL)
  42.         {
  43.                 book->next=*library;
  44.                                 *library = book;
  45.         }
  46.         else
  47.         {
  48.                 *library=book;
  49.                 book->next=NULL;
  50.         }
  51. }
  52. void releaselibrary(struct Book *library)
  53. {
  54.         struct Book *temp;
  55.         while(library!=NULL)
  56.         {
  57.                         // 释放顺序  
  58.                         temp = library->next;
  59.                         free(library);
  60.                         library=temp;
  61.                        
  62.         }
  63. }
  64. int main(void)
  65. {
  66.         struct Book *library=NULL;
  67.         int ch;
  68.         while(1)
  69.         {
  70.                 printf("是否需要录入书籍信息(Y/N):");
  71.                 do
  72.                 {
  73.                         ch=getchar();
  74.                 }while(ch!='Y'&&ch!='N');
  75.                 if(ch=='Y')
  76.                 {
  77.                         addBook(&library);
  78.                 }
  79.                 else
  80.                 {
  81.                         break;
  82.                 }
  83.         }


  84.                 printf("是否需要打印书籍信息(Y/N):");
  85.       
  86.                 do
  87.                 {
  88.                         ch=getchar();
  89.                 }while(ch!='Y'&&ch!='N');
  90.                 if(ch=='Y')
  91.                 {
  92.                         printlibrary(library);
  93.                 }
  94.       
  95.                 releaselibrary(library);

  96.         return 0;

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

使用道具 举报

 楼主| 发表于 2022-3-29 20:15:36 | 显示全部楼层

谢谢哥们,可以了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-24 12:48

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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