鱼C论坛

 找回密码
 立即注册
查看: 895|回复: 3

[已解决]求助!删除函数那出现了问题,急!!

[复制链接]
发表于 2018-12-21 21:49:18 | 显示全部楼层 |阅读模式

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

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

x
为什么输入指令后会产生这种情况,该怎么改呢?
#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;
        printf("请输入搜索图书的的书名或作者:");
        getchar(); //没有的话会异常退出
        gets(inform);
        while(1)
        {
                if(!strcmp(book->title,inform)||!strcmp(book->author,inform))
                {
                        return book;
                }
                if(book=NULL)
                {
                        break;
                }  
                book=book->pnext;
        }
        return book;
}

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=NULL;
        struct Book *perious=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))
                {
                        perious=current;
                        current=current->pnext;
                        break;
                }
                if(current=NULL)
                {
                        break;
                }
        }
        if(current==NULL)
        {
                printf("未找到需要删除的图书\n");
        }
        else
        {
                perious=current->pnext;
                free(current);
        }
}
int main()
{
        int N;
        struct Book*phead=NULL;
//        struct Book*searchbook;
//        printf("%43s",图书管理系统\n);
         putchar('\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:exit(1);break;
                        defult:printf("指令错误\n");break;  //?
                }
                putchar('\n');
        }
}
最佳答案
2018-12-21 23:40:03
问题真的很多:
  case 4:cutoff_book(&phead);break; //删除请用指向指针的指针
void cutoff_book(struct Book **book)  //删除被搜索的图书信息 ,并释放内存 
{
                char c[100]={'\0'}; // 要使用请先分配空间
        struct Book *perious=NULL;
        struct Book *current=NULL;

                current = *book;
                perious = *book;

        printf("请输入你要删除的图书的书名或作者:");
                while(getchar()!='\n')continue;
        gets(c);

        if(current==NULL)
        {
                printf("未找到需要删除的图书\n");
                return; //返回 
        }

        while(!(!strcmp(current->title,c)||!strcmp(current->author,c)))
        {
                                perious=current;
                                current=current->pnext;

                if(current==NULL) // 别把== 当=用。。
                {
                        break;
                }
        }

        if(current==NULL) // 尾部
        {
                printf("未找到需要删除的图书\n");
        }
        else
        {
                        if(current == *book) // 是否指向头
                        {
                                *book=current->pnext;
                        }

                        else // 中间
                        {

                perious->pnext = current->pnext;

                
                        }
                        
                        free(current);
        }
}
QQ截图20181221214806.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-12-21 23:40:03 | 显示全部楼层    本楼为最佳答案   
问题真的很多:
  case 4:cutoff_book(&phead);break; //删除请用指向指针的指针
void cutoff_book(struct Book **book)  //删除被搜索的图书信息 ,并释放内存 
{
                char c[100]={'\0'}; // 要使用请先分配空间
        struct Book *perious=NULL;
        struct Book *current=NULL;

                current = *book;
                perious = *book;

        printf("请输入你要删除的图书的书名或作者:");
                while(getchar()!='\n')continue;
        gets(c);

        if(current==NULL)
        {
                printf("未找到需要删除的图书\n");
                return; //返回 
        }

        while(!(!strcmp(current->title,c)||!strcmp(current->author,c)))
        {
                                perious=current;
                                current=current->pnext;

                if(current==NULL) // 别把== 当=用。。
                {
                        break;
                }
        }

        if(current==NULL) // 尾部
        {
                printf("未找到需要删除的图书\n");
        }
        else
        {
                        if(current == *book) // 是否指向头
                        {
                                *book=current->pnext;
                        }

                        else // 中间
                        {

                perious->pnext = current->pnext;

                
                        }
                        
                        free(current);
        }
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-12-22 09:42:13 | 显示全部楼层
我问的是为什么输入4后我还没输入书名或作者就往下走了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-12-22 09:48:09 | 显示全部楼层
看错了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-10-3 02:27

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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