鱼C论坛

 找回密码
 立即注册
查看: 1509|回复: 4

[已解决]求助!为什么输入指令3后程序自动退出了,就是搜索功能那

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

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

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

x
求助!
QQ截图20181222101908.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-12-22 10:22:02 | 显示全部楼层
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct Date
{
        int year;
        int month;
        int day;
};
struct Book
{
        char title[128];
        char author[48];
        float price;
        struct Date date;
        char publisher[48];
        struct Book*pnext;
};

void ReadBook(struct Book**pphead)
{
        struct Book*book=(struct Book*)malloc(sizeof(struct Book));
        printf("---开始录人书籍---\n");
        getchar();
        printf("请输入书名:");
        gets(book->title);
        printf("请输入作者:");
        gets(book->author);
        printf("请输入售价:");
        scanf("%f",&book->price);
        printf("请输入出版日期(按“ 2018-12-12 ”的格式输入):");
        scanf("%d-%d-%d",&book->date.year,&book->date.month,&book->date.day) ;
        getchar();  //重要
        printf("请输入出版社:");
        //scanf("%s",book->publisher);
        gets(book->publisher);
        if(*pphead==NULL)
        {
                *pphead=book;
                book->pnext=NULL;
        }
        else
        {
                struct Book*temp=*pphead;
                while(1)
                {
                        if(temp->pnext==NULL)
                        {
                                break;
                        }
                        temp=temp->pnext;
                }
                temp->pnext=book;
                book->pnext=NULL;
        }
}
void print_book(struct Book*phead)
{
        struct Book *book=phead;
        printf("\n---开始打印图书信息---\n");
        if(phead==NULL)
        {
                printf("还未录入图书!\n");
        }
        else
        {
                while(1)
                {
                        if(book==NULL)
                        {
                                break;
                        }
                        else
                        {
                                printf("书名:%s\n",book->title);
                                printf("作者:%s\n",book->author);
                                printf("售价:%.2f",book->price);
                                printf("出版日期:%d-%d-%d\n",book->date.year,book->date.month,book->date.day);
                                printf("出版社:%s\n",book->publisher);
                                putchar('\n');
                                book=book->pnext;
                        }
                }
        }
        printf("\n---打印完毕---\n");
}

struct Book*SearchBook(struct Book*book)  //搜索图书并返回指向该图书信息的指针,未找到则返回NULL
{
        char inform[100];
        printf("请输入搜索图书的的书名或作者:");
        getchar(); //没有的话会异常退出
        gets(inform);
        while(1)
        {
                if(book=NULL)
                {
                        return book;
                }
                if(!strcmp(book->title,inform)||!strcmp(book->author,inform))
                {
                        return book;
                }
                book=book->pnext;
        }
}

void print_search(struct Book*book) //打印被搜索的图书信息
{
        if(book==NULL)
        {
                printf("未找到图书信息!\n");
        }
        else
        {
                printf("---你所寻找的图书信息如下---\n");
                printf("书名:%s\n",book->title);
                printf("作者:%s\n",book->author);
                printf("售价:%.2f\n",book->price);
                printf("出版日期:%d-%d-%d\n",book->date.year,book->date.month,book->date.day);
                printf("出版社:%s\n",book->publisher);
        }       
}
void cutoff_book(struct Book*book)  //删除被搜索的图书信息 ,并释放内存
{
        char cutbook[100];
        struct Book *pervious=NULL;
        struct Book *current=book;
        printf("请输入你要删除的图书的书名或作者:");
        getchar();
        gets(cutbook);
        if(current==NULL)
        {
                printf("未找到需要删除的图书\n");
                return; //返回
        }
        while(1)
        {
                if(!strcmp(current->title,cutbook)||!strcmp(current->author,cutbook))
                {
                        break;
                }
                if(current==NULL)
                {
                        break;
                }
                pervious=current;
                current=current->pnext;
        }
        if(current==NULL)
        {
                printf("未找到需要删除的图书!\n");
        }
        else
        {
                pervious=pervious->pnext;
                free(current);
        }
}

void releasebook(struct Book*book)  //释放链表空间
{
        struct Book *temp=NULL;
        while(book!=NULL)
        {
                temp=book;
                book=book->pnext;
                free(temp);
        }
}

int main()
{
        int N;
        struct Book*phead=NULL;
//        struct Book*searchbook;
         putchar('\n');
         printf("%46c图书管理系统\n",' ');
         printf("\n\n");
         printf("1 录入图书\n");
         printf("2 打印已录入图书信息\n");
         printf("3 搜素图书\n");
         printf("4 删除已录入图书\n");
         printf("5 退出程序\n");
         printf(" \n\n   ---输入功能前的相应序号进行操作---  \n\n");
         
         while(1)
         {         
                  printf("请输入指令:");
                  scanf("%d",&N);
                  putchar('\n');
                 switch(N)
                 {
                 
                        case 1:ReadBook(&phead); break;
                        case 2:print_book(phead);break;
                        case 3:print_search(SearchBook(phead));break;
                        case 4:cutoff_book(phead);break;
                        case 5:releasebook(phead);exit(1);break;
                        defult:printf("指令错误\n");break;  //?
                }
                putchar('\n');
        }
}
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-12-22 12:18:05 | 显示全部楼层
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>

  4. struct Date
  5. {
  6.         int year;
  7.         int month;
  8.         int day;
  9. };
  10. struct Book
  11. {
  12.         char title[128];
  13.         char author[48];
  14.         float price;
  15.         struct Date date;
  16.         char publisher[48];
  17.         struct Book*pnext;
  18. };

  19. void ReadBook(struct Book**pphead)
  20. {
  21.         struct Book*book = (struct Book*)malloc(sizeof(struct Book));
  22.         printf("---开始录人书籍---\n");
  23.         getchar();
  24.         printf("请输入书名:");
  25.         gets(book->title);
  26.         printf("请输入作者:");
  27.         gets(book->author);
  28.         printf("请输入售价:");
  29.         scanf("%f", &book->price);
  30.         printf("请输入出版日期(按“ 2018-12-12 ”的格式输入):");
  31.         scanf("%d-%d-%d", &book->date.year, &book->date.month, &book->date.day);
  32.         getchar();  //重要
  33.         printf("请输入出版社:");
  34.         //scanf("%s",book->publisher);
  35.         gets(book->publisher);
  36.         if(*pphead == NULL)
  37.         {
  38.                 *pphead = book;
  39.                 book->pnext = NULL;
  40.         }
  41.         else
  42.         {
  43.                 struct Book*temp = *pphead;
  44.                 while(1)
  45.                 {
  46.                         if(temp->pnext == NULL)
  47.                         {
  48.                                 break;
  49.                         }
  50.                         temp = temp->pnext;
  51.                 }
  52.                 temp->pnext = book;
  53.                 book->pnext = NULL;
  54.         }
  55. }
  56. void print_book(struct Book*phead)
  57. {
  58.         struct Book *book = phead;
  59.         printf("\n---开始打印图书信息---\n");
  60.         if(phead == NULL)
  61.         {
  62.                 printf("还未录入图书!\n");
  63.         }
  64.         else
  65.         {
  66.                 while(1)
  67.                 {
  68.                         if(book == NULL)
  69.                         {
  70.                                 break;
  71.                         }
  72.                         else
  73.                         {
  74.                                 printf("书名:%s\n", book->title);
  75.                                 printf("作者:%s\n", book->author);
  76.                                 printf("售价:%.2f", book->price);
  77.                                 printf("出版日期:%d-%d-%d\n", book->date.year, book->date.month, book->date.day);
  78.                                 printf("出版社:%s\n", book->publisher);
  79.                                 putchar('\n');
  80.                                 book = book->pnext;
  81.                         }
  82.                 }
  83.         }
  84.         printf("\n---打印完毕---\n");
  85. }

  86. struct Book*SearchBook(struct Book*book)  //搜索图书并返回指向该图书信息的指针,未找到则返回NULL
  87. {
  88.         char inform[100];
  89.         printf("请输入搜索图书的的书名或作者:");
  90.         getchar(); //没有的话会异常退出
  91.         gets(inform);
  92.         while(1)
  93.         {
  94.                
  95.                
  96.                
  97.                
  98.                
  99.                 //if(book = NULL)






  100.                 if(book == NULL)
  101.                 {
  102.                         return book;
  103.                 }
  104.                 if(!strcmp(book->title, inform) || !strcmp(book->author, inform))
  105.                 {
  106.                         return book;
  107.                 }
  108.                 book = book->pnext;
  109.         }
  110. }

  111. void print_search(struct Book*book) //打印被搜索的图书信息
  112. {
  113.         if(book == NULL)
  114.         {
  115.                 printf("未找到图书信息!\n");
  116.         }
  117.         else
  118.         {
  119.                 printf("---你所寻找的图书信息如下---\n");
  120.                 printf("书名:%s\n", book->title);
  121.                 printf("作者:%s\n", book->author);
  122.                 printf("售价:%.2f\n", book->price);
  123.                 printf("出版日期:%d-%d-%d\n", book->date.year, book->date.month, book->date.day);
  124.                 printf("出版社:%s\n", book->publisher);
  125.         }
  126. }
  127. void cutoff_book(struct Book*book)  //删除被搜索的图书信息 ,并释放内存
  128. {
  129.         char cutbook[100];
  130.         struct Book *pervious = NULL;
  131.         struct Book *current = book;
  132.         printf("请输入你要删除的图书的书名或作者:");
  133.         getchar();
  134.         gets(cutbook);
  135.         if(current == NULL)
  136.         {
  137.                 printf("未找到需要删除的图书\n");
  138.                 return; //返回
  139.         }
  140.         while(1)
  141.         {
  142.                 if(!strcmp(current->title, cutbook) || !strcmp(current->author, cutbook))
  143.                 {
  144.                         break;
  145.                 }
  146.                 if(current == NULL)
  147.                 {
  148.                         break;
  149.                 }
  150.                 pervious = current;
  151.                 current = current->pnext;
  152.         }
  153.         if(current == NULL)
  154.         {
  155.                 printf("未找到需要删除的图书!\n");
  156.         }
  157.         else
  158.         {
  159.                 pervious = pervious->pnext;
  160.                 free(current);
  161.         }
  162. }

  163. void releasebook(struct Book*book)  //释放链表空间
  164. {
  165.         struct Book *temp = NULL;
  166.         while(book != NULL)
  167.         {
  168.                 temp = book;
  169.                 book = book->pnext;
  170.                 free(temp);
  171.         }
  172. }

  173. int main(void)
  174. {
  175.         int N;
  176.         struct Book*phead = NULL;
  177.         //        struct Book*searchbook;
  178.         putchar('\n');
  179.         printf("%46c图书管理系统\n", ' ');
  180.         printf("\n\n");
  181.         printf("1 录入图书\n");
  182.         printf("2 打印已录入图书信息\n");
  183.         printf("3 搜素图书\n");
  184.         printf("4 删除已录入图书\n");
  185.         printf("5 退出程序\n");
  186.         printf(" \n\n   ---输入功能前的相应序号进行操作---  \n\n");

  187.         while(1)
  188.         {
  189.                 printf("请输入指令:");
  190.                 scanf("%d", &N);
  191.                 putchar('\n');
  192.                 switch(N)
  193.                 {

  194.                 case 1:ReadBook(&phead); break;
  195.                 case 2:print_book(phead); break;
  196.                 case 3:print_search(SearchBook(phead)); break;
  197.                 case 4:cutoff_book(phead); break;
  198.                 case 5:releasebook(phead); exit(1); break;
  199.                
  200.                
  201.                
  202.                
  203.                
  204.                
  205.                 //defult:printf("指令错误\n"); break;  //?
  206.                 default:printf("指令错误\n"); break;  //?
  207.                
  208.                
  209.                
  210.                
  211.                
  212.                
  213.                 }
  214.                 putchar('\n');
  215.         }
  216. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-12-23 13:32:34 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-12-23 13:46:53 | 显示全部楼层    本楼为最佳答案   

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-14 16:06

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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