鱼C论坛

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

[已解决]程序执行指令后会退出,为什么?求助!!!

[复制链接]
发表于 2018-12-28 17:46:03 | 显示全部楼层 |阅读模式

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

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

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;
};

struct USER
{
        unsigned int id;
        unsigned int password;
        struct USER *pnext;
};

void print_log(void)
{
        printf("1 登录\n");
        printf("2 注册\n");
        printf("3 退出\n\n\n");
        printf("     ---请输入操作前的序号进行相应操作---\n\n");
}

void AddUser(struct USER**pphead)
{
        struct USER*newuser=(struct USER*)malloc(sizeof(struct USER));
        if(newuser==NULL)
        {
                printf("分配内存失败!\n");
                exit(EXIT_FAILURE);
        }
        printf("--注册新账户--\n");
        printf("账户:");
        scanf("%u",&newuser->id);
        printf("密码:");
        scanf("%u",&newuser->password);
        newuser->pnext=*pphead;
        *pphead=newuser;       
}

void log_on(unsigned int*id,unsigned int*password)
{
        printf("——请输入账号和密码(未注册请先注册账户)——\n");
        printf("账号:");
        scanf("%u",id);
        printf("密码:");
        scanf("%u",password);
}

int confirm_user(struct USER*user,unsigned int id,unsigned int password)
{
       
        while(1)
        {
                if(user==NULL) //要放前面
                {
                        return -1;
                }
                if(id==user->id&&password==user->password)
                {
                        return 1;
                }
                user=user->pnext;
        }
}



int choose_num(struct USER**pphead,unsigned int *id,unsigned int *password)
{
               
        int key=-1,kiss;
        do{
                        printf("输入序号:");               
                        scanf("%d",&kiss);
                        switch(kiss)
                        {
                                case 1:log_on(id,password);key=confirm_user(*pphead,*id,*password);
                                                if(key!=1)
                                                {
                                                        printf("账号或密码错误!\n");
                                                }
                                                break; //+break
                                case 2:AddUser(pphead);break;
                                case 3:exit(1);break;
                                default:printf("输入指令错误!\n");
                        }
                       
          }while(key==1);
               
          return key;       
}
                     
void print_meau(void)
{
        putchar('\n');
    printf("%46c图书管理系统\n", ' ');
    printf("\n\n");
    printf("* * * * * * * * * * * * * * * * * * *\n");
    printf("*  1 录入图书                       *\n");
    printf("*  2 打印已录入图书信息             *\n");
    printf("*  3 搜素图书                       *\n");
    printf("*  4 删除已录入图书                 *\n");
    printf("*  5 退出程序                       *\n");
    printf("* * * * * * * * * * * * * * * * * * *\n");
    printf(" \n\n   ---输入功能前的相应序号进行操作---  \n\n");
}

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\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");
}
void scanf_search(char inform[])
{
        printf("请输入搜索图书的的书名或作者:");
    getchar(); //没有的话会异常退出
    gets(inform);
}

struct Book*SearchBook(struct Book*book,char inform[])  //搜索图书并返回指向该图书信息的指针,未找到则返回NULL
{
        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,char inform[]) //打印被搜索的图书信息
{
        if(book == NULL)
        {
                printf("未找到图书信息!\n");
        }
        else
        {
                putchar('\n');
                printf("---已找到被搜索图书,图书信息如下---\n\n");
            do{
                        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);
                        putchar('\n');
                           }while((book=SearchBook(book->pnext,inform))!=NULL);
        }
}
void cutoff_book(struct Book**pphead)  //删除被搜索的图书信息 ,并释放内存
{
        char cutbook[100];
        struct Book *pervious = NULL;
        struct Book *current = *pphead;
        printf("请输入你要删除的图书的书名或作者:");
        getchar();
        gets(cutbook);
       /* if(current == NULL)
        {
                printf("未找到需要删除的图书\n");
                return; //返回
        }*/
        while(1)
        {
                        if(current == NULL)
                {
                        break;
                }
                if(!strcmp(current->title, cutbook) || !strcmp(current->author, cutbook))
                {
                        break;
                }
                pervious = current;
                current = current->pnext;
        }
        if(current == NULL)
        {
                printf("未找到需要删除的图书!\n");
        }
        else
        {
                if(pervious==NULL)
                {
                        *pphead=current->pnext;
                                }
                                else
                                {
                                        pervious->pnext=current->pnext;                               
                                }
                free(current);
        }
}

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

int main(void)
{
        int N,enter=-1;
        char inform[128];
        struct Book*phead = NULL;
        //        struct Book*searchbook;  
        struct USER *pheaduser=NULL;
                unsigned int id,password;
               
                print_log();
                enter= choose_num(&pheaduser,&id,&password);
                if(enter==1)
                {
                print_meau();
                while(1)
                {
                        printf("请输入指令:");
                        scanf("%d", &N);
                        putchar('\n');
                        switch(N)
                        {
       
                                case 1:ReadBook(&phead); break;
                                case 2:print_book(phead); break;
                                case 3:scanf_search(inform);print_search(SearchBook(phead,inform),inform); break;
                                case 4:cutoff_book(&phead); break;
                                case 5:releasebook(phead); exit(1); break;
                                default:printf("指令错误\n"); break;
                                          
                        }
                        putchar('\n');
                }
            }
            
        return 0;
}
最佳答案
2018-12-28 20:18:02
int choose_num(struct USER**pphead,unsigned int *id,unsigned int *password)
{
               
        int key=-1,kiss;
        do{
                        printf("输入序号:");               
                        scanf("%d",&kiss);
                        switch(kiss)
                        {
                                case 1:log_on(id,password);key=confirm_user(*pphead,*id,*password);
                                                if(key!=1)
                                                {
                                                        printf("账号或密码错误!\n");
                                                }
                                                break; //+break
                                case 2:AddUser(pphead);break;
                                case 3:exit(1);break;
                                default:printf("输入指令错误!\n");
                        }
                        
          }while(key==1);
               
          return key;        
}

这个函数里 while(key==1)改成 while(key !=1)
QQ截图20181228174547.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-12-28 19:11:02 | 显示全部楼层
不清楚
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-12-28 20:18:02 | 显示全部楼层    本楼为最佳答案   
int choose_num(struct USER**pphead,unsigned int *id,unsigned int *password)
{
               
        int key=-1,kiss;
        do{
                        printf("输入序号:");               
                        scanf("%d",&kiss);
                        switch(kiss)
                        {
                                case 1:log_on(id,password);key=confirm_user(*pphead,*id,*password);
                                                if(key!=1)
                                                {
                                                        printf("账号或密码错误!\n");
                                                }
                                                break; //+break
                                case 2:AddUser(pphead);break;
                                case 3:exit(1);break;
                                default:printf("输入指令错误!\n");
                        }
                        
          }while(key==1);
               
          return key;        
}

这个函数里 while(key==1)改成 while(key !=1)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-10-3 04:38

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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