关于#c语言#的知识点:关于打印的问题

为什么无法打印出我全部录入的书籍信息



#include<stdio.h>
#include<stdlib.h>
struct Book
{
    char title[128];
    struct Book *next;
};
void addBook(struct Book **lbr);
void getInput(struct Book *book);
void printlbr(struct Book *lbr);
void release(struct Book **lbr);
void addBook(struct Book **lbr)
{
    struct Book *book,*temp;
    book = (struct Book *)malloc(sizeof(struct Book));
    if(book == NULL)
    {
        printf("内存分配失败\n");
        exit(1);
    }
    getInput(book);
    if(*lbr != NULL)
    {
        temp = *lbr;
        *lbr = book;
        book = temp;
    }
    else
    {
        *lbr = book;
        book->next = NULL;
    }
}
void getInput(struct Book *book)
{
    printf("请输入书名:");
    scanf("%s",book->title);
}
void printlbr(struct Book *lbr)
{
    struct Book *book;
    int count = 1;
    book = lbr;
    while(book != NULL)
    {
        printf("Book%d",count);
        printf("书名:%s",book->title);
        book = book->next;
        count++;
    }
}
void release(struct Book **lbr)
{
    struct Book *temp;
    while(lbr != NULL)
    {
        temp = *lbr;
        *lbr = (*lbr)->next;
        free(temp);
    }
}
int main(void)
{
    struct Book *lbr;
    int ch;
    while(1)
    {
        printf("请问是否需要录入书籍信息(Y/N):");
        do
        {
            ch = getchar();
        }while(ch != 'Y' && ch != 'N');
        if(ch = 'Y')
        {
            addBook(&lbr);
        }
        else
        {
            break;
        }
    }
    printf("请问是否需要打印书籍信息(Y/N):");
    do
    {
        ch = getchar();
    }while(ch != 'Y' && ch != 'N');
    if(ch = 'Y')
    {
        printlbr(lbr);
    }
    release(&lbr);
    return 0;
}


第75行:if(ch = 'Y') ,第89行:if(ch = 'Y') , 都缺了‘=’号, 应该是:ch == 'Y'

if(ch = 'Y')
没改?
if(ch == 'Y')
有两处,别忘记

【以下回答由 GPT 生成】

改进建议: 1. 确保在调用printlbr函数之前,已经正确调用了addBook函数,将书籍信息录入到链表中。 2. 在printlbr函数中,添加每个书籍信息打印完毕后的换行符,以提高输出的可读性。 3. 检查输入书名的方式是否正确。可以尝试使用fgets函数来输入字符串,以避免因使用scanf函数可能导致的输入格式问题。 4. 在调用printlbr函数之前,确保链表头指针lbr不为空,否则链表为空时,程序会直接退出。

以下是优化后的代码示例:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Book
{
    char title[128];
    struct Book *next;
};

void addBook(struct Book **lbr);
void getInput(struct Book *book);
void printlbr(struct Book *lbr);
void release(struct Book **lbr);

void addBook(struct Book **lbr)
{
    struct Book *book, *temp;
    book = (struct Book *)malloc(sizeof(struct Book));
    if (book == NULL)
    {
        printf("内存分配失败\n");
        exit(1);
    }
    getInput(book);
    if (*lbr != NULL)
    {
        temp = *lbr;
        *lbr = book;
        book->next = temp;
    }
    else
    {
        *lbr = book;
        book->next = NULL;
    }
}

void getInput(struct Book *book)
{
    printf("请输入书名:");
    fgets(book->title, 128, stdin);
    // 移除输入字符串中的换行符
    book->title[strcspn(book->title, "\n")] = 0;
}

void printlbr(struct Book *lbr)
{
    if (lbr == NULL)
    {
        printf("链表为空\n");
        return;
    }
    struct Book *book;
    int count = 1;
    book = lbr;
    while (book != NULL)
    {
        printf("Book%d\n", count);
        printf("书名: %s\n", book->title);
        book = book->next;
        count++;
    }
}

通过这些改进,你应该能够正确地打印出录入的全部书籍信息了。如果仍然有问题,请提供更多的上下文信息,以便我们可以更准确地帮助你解决问题。



【相关推荐】



如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^