鱼C论坛

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

我想用switch语句来实现小甲鱼图书管理系统,为什么不可以?下面是我的代码,

[复制链接]
发表于 2021-8-25 00:56:26 From FishC Mobile | 显示全部楼层 |阅读模式

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

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

x
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct library
{
    char bookname[500];
    char author[500];
    struct library* next;
};
void addbook(struct library** first);
void printbook(struct library* first);
void releasespace(struct library** first);

int main()
{
    struct library* first = NULL;
    int ch;
       printf("1添加图书\n");
                printf("2打印图书\n");
                printf("3退出\n");
                 
    while (1)
    {
      
        do
        {  printf("输入选项");
            scanf("%d", &ch);
             switch(ch)
            {case 1:
               addbook(&first);
               break;
            case 2:
                    printbook(first);
                                break;}
        } while (ch !=3);
    }
    releasespace(&first);
    return 0;
}
void addbook(struct library** first)
{
    struct library* book, * temp;
    book = (struct library*)malloc(sizeof(struct library));
    *first = (struct library*)malloc(sizeof(struct library));
    printf("请输入需要添加图书的名字:");
    scanf("%s", book->bookname);
    printf("请输入图书作者:");
    scanf("%s", book->author);
    {
       book->next=(*first)->next;
    (*first)->next=book;
    }
}
void printbook(struct library* first)
{
    struct library* book;
    book = first;
    while (book != NULL)
    {
        printf("书的名字:%s", book->bookname);
        printf("书的作者:%s", book->author);
        book = book->next;
    }
}
void releasespace(struct library** first)
{
    struct library* temp;
    while (*first != NULL)
    {
        temp = *first;
        *first = (*first)->next;
        free(temp);
    }
}
执行完程序有毛病,我如果添加两次,第一次打印错误,但是第二次就对了,不知道怎么回事,求大佬指点



想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-8-25 09:17:36 | 显示全部楼层
你这个 while(1) 多余了,导致退不出循环,因为 break 语句只能退出一层循环,即退出内层的 do-while 循环
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2021-8-25 09:31:01 | 显示全部楼层
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. //#include<malloc.h>
  4. struct library
  5. {
  6.     char bookname[500];
  7.     char author[500];
  8.     struct library* next;
  9. };
  10. void addbook(struct library** first);
  11. void printbook(struct library* first);
  12. void releasespace(struct library** first);

  13. int main()
  14. {
  15.     struct library* first = NULL;
  16.     first = (struct library*)malloc(sizeof(struct library)); //在这里初始化就好了
  17.     int ch;
  18.        printf("1添加图书\n");
  19.                 printf("2打印图书\n");
  20.                 printf("3退出\n");
  21.                  
  22.     while (1)
  23.     {
  24.       
  25.         do
  26.         {  printf("输入选项");
  27.             scanf("%d", &ch);
  28.              switch(ch)
  29.             {case 1:
  30.                addbook(&first);
  31.                break;
  32.             case 2:
  33.                     printbook(first);
  34.                                 break;}
  35.         } while (ch !=3);
  36.     }
  37.     releasespace(&first);
  38.     return 0;
  39. }
  40. void addbook(struct library** first)
  41. {
  42.     struct library* book, * temp;
  43.     book = (struct library*)malloc(sizeof(struct library));
  44. //    *first = (struct library*)malloc(sizeof(struct library)); 每次进来都你都分配空间,相当于声明一个新变量
  45.     printf("请输入需要添加图书的名字:");
  46.     scanf("%s", book->bookname);
  47.     printf("请输入图书作者:");
  48.     scanf("%s", book->author);
  49.     book->next=(*first)->next;
  50.     (*first)->next=book;
  51. }
  52. void printbook(struct library* first)
  53. {
  54.     struct library* book;
  55.     book = first->next;  //头指针是空节点,要指向下一个
  56.     while (book != NULL)
  57.     {
  58.         printf("书的名字:%s", book->bookname);
  59.         printf("书的作者:%s", book->author);
  60.         book = book->next;
  61.     }
  62. }
  63. void releasespace(struct library** first)
  64. {
  65.     struct library* temp;
  66.     while (*first != NULL)
  67.     {
  68.         temp = *first;
  69.         *first = (*first)->next;
  70.         free(temp);
  71.     }
  72. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-24 04:06

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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