zz2329017 发表于 2020-2-13 14:15:43

if选择分支懵逼问题

本帖最后由 zz2329017 于 2020-2-13 14:20 编辑

if(*library != NULL)
        {
                temp = *library;
                *library = book;
                book->next = temp;
        }
        else
        {   
          *library = book;
                book->next = NULL;
        }




if(*library = NULL)
        {
                *library = book;
                book->next = NULL;
               
        }
        else
        {   
             temp = *library;
                *library = book;
                book->next = temp;
        }



有区别吗,为什么在同一段代码中结果不一样。




完整代码如下:
#include<stdio.h>
#include<stdlib.h>

struct Date
{
        int year;
        int month;
        int day;
};

struct Book
{
        char name;
        char author;
        float price;
        struct Date date;
    struct Book *next;
};

void getinput(struct Book *book);
void addbook(struct Book **library);
void print(struct Book *library);
void releaselibrary(struct Book *library);

void addbook(struct Book **library)
{
        struct Book *book,*temp;
        book = (struct Book*)malloc(sizeof(struct Book));

        if (book == NULL)
        {
                printf("申请空间失败!\n");
                exit(1);
        }
       
        getinput(book);
       
        if(*library != NULL)
        {
                temp = *library;
                *library = book;
                book->next = temp;
        }
        else
        {   
          *library = book;
                book->next = NULL;
        }//注这个if不能反过来写!!!啊!!!!
}

void getinput(struct Book *book)
{
        printf("请输入书名:");
        scanf("%s",book->name);
        printf("请输入作者:");
        scanf("%s",book->author);
        printf("请输入日期:");
        scanf("%d%d%d",&book->date.year,&book->date.month,&book->date.day);
        printf("请输入价格:");
        scanf("%f",&book->price);
}

void print(struct Book *library)
{
        struct Book *book;
        int count = 1;
       
        book = library;
        while(book != NULL)
        {
                printf("Book%d:\n",count);
          printf("书名:%s\n",book->name);
          printf("作者:%s\n",book->author);
          printf("日期:%d.%d.%d\n",book->date.year,book->date.month,book->date.day);
          printf("价格:%.2f\n",book->price);
          book = book->next;
      count++;   
        }
}

void release(struct Book *library)
{
        struct Book *book;
        int count = 1;
        book = library;
        while(book!=NULL)
        {
                free(book);
                book = book->next;
        }
       
}

int main()
{

        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("\n\n");
       
        printf("是否需要打印所录入的书?(Y/N)");
    do
    {
      ch = getchar();
        }while(ch != 'Y' && ch != 'N');

        if(ch=='Y')
        {
                print(library);
        }
        release(library);
       
        return 0;
}

best028ajun 发表于 2020-2-13 14:26:00

虽然我看不懂,但是我推荐楼主 在复制代码的时候 试用编辑页面的代码服务 来表示你所需要表示的代码,这样看的直观点。

人造人 发表于 2020-2-13 14:48:40

就不能认真一点吗?

zz2329017 发表于 2020-2-13 15:25:18

人造人 发表于 2020-2-13 14:48
就不能认真一点吗?

嘿嘿嘿,谢了,新手,老是把赋值运算符继承等于。。
页: [1]
查看完整版本: if选择分支懵逼问题