马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 HZX0229 于 2021-3-20 21:18 编辑
大佬们帮忙看看有啥问题?#include <stdio.h>
#include <stdlib.h>
struct Book{
char title[128];
char author[40];
struct Book *next;
};
void addbook(struct Book **);
//void Getinput(struct Book *);
void printbook(struct Book *);
void releaselibrary(struct Book *);
void addbook(struct Book **library)
{
struct Book *book, *temp;
book = (struct Book *)malloc(sizeof(struct Book));
if (book = NULL)
{
printf("内存分配错误!\n");
exit(1);
}
printf("请输入书名:");
scanf("%s", book->title);
printf("请输入作者:");
scanf("%s", book->author);
if (*library != NULL)
{
temp = *library;
*library = book;
book->next = temp;
}
else{
*library = book;
book->next = NULL;
}
}
void printbook(struct Book *library)
{
struct Book *book;
int count = 1;
book = library;
while (book != NULL)
{
printf("BOOK%d\n", count++);
printf("书名:%s\n", book->title);
printf("作者:%s\n", book->author);
book = book->next;
}
}
void releaselibrary(struct Book *library)
{
struct Book *book;
while (library != NULL)
{
book = library;
library = library->next;
free(book);
}
}
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("请问是否需要打印图书信息(Y/N):");
do{
ch = getchar();
}while (ch != 'Y' && ch != 'N');
if (ch == 'Y')
{
printbook(library);
}
releaselibrary(library);
return 0;
}
第45节课,课堂程序,
能编译成功,但是输入第一个书名后直接退出了
void addbook(struct Book **library)
{
struct Book *book, *temp;
book = (struct Book *)malloc(sizeof(struct Book));
if (book = NULL) 这个赋值符号 改成 关系符号看看
{
printf("内存分配错误!\n");
exit(1);
}
|