我又来了了了了了了了!!!!!!!
本帖最后由 Yo乄 于 2018-10-23 20:39 编辑#include"stdio.h"
#include"stdlib.h"
void getInput (struct Book *book);
void addBook (struct Book **library);
void printLibrary(struct Book *library);
void releaseLibrary(struct Book *library);
struct Book
{
char name;
char author;
int money;
struct Book *next;
};
void getInput (struct Book *book)
{
printf("请输入书名:");
scanf("%s",book->name);
printf("请输入作者:");
scanf("%s",book->author);
printf("请输入售价:");
scanf("%d",book->money);
}
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;
}
}
void printLibrary(struct Book *library)
{
struct Book *book;
int count = 1 ;
book = library;
while (book != NUll)
{
printf("Book%d:",count)
printf("书名:%s",book->name);
printf("作者:%s",book->author);
printf("售价:%d",book->money);
book = book->next;
count ++ ;
}
}
void releaseLibrary(struct Book *library)
{
while (library!=NULL)
{
library=library->next;
free(library);
}
}
int main()
{
struct Book *library = NULL;
char 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')
{
printLibrary(library);
}
releaseLibrary(library);
// addBook(&library);
return 0;
}
又是一片红啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊 没有红了,但是 addBook 还是哪里有问题,不能继续添加,代码抄好来
1. 你的函数声明必须在结构体的定义后面,不然编译器不知道什么是 struct Book
2. NULL 不是 NUll
3. 标准库用尖箭头#include <stdio.h>
#include <stdlib.h>
struct Book
{
char name;
char author;
int money;
struct Book *next;
};
void getInput(struct Book *book)
{
printf("请输入书名:");
scanf("%s",book->name);
printf("请输入作者:");
scanf("%s",book->author);
printf("请输入售价:");
scanf("%d",book->money);
}
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;
}
}
void printLibrary(struct Book *library)
{
struct Book *book;
int count = 1 ;
book = library;
while (book != NULL)
{
printf("Book%d:",count);
printf("书名:%s",book->name);
printf("作者:%s",book->author);
printf("售价:%d",book->money);
book = book->next;
count++;
}
}
void releaseLibrary(struct Book *library)
{
while (library!=NULL)
{
library=library->next;
free(library);
}
}
void getInput(struct Book *book);
void addBook(struct Book **library);
void printLibrary(struct Book *library);
void releaseLibrary(struct Book *library);
int main()
{
struct Book *library = NULL;
char 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')
{
printLibrary(library);
}
releaseLibrary(library);
// addBook(&library);
return 0;
} claws0n 发表于 2018-10-23 20:51
没有红了,但是 addBook 还是哪里有问题,不能继续添加,代码抄好来
1. 你的函数声明必须在结构体的定义后 ...
老哥 我现在编译运行 不能添加书。。输入完第一本书的信息后回车 就卡住 然后自动完成程序 Yo乄 发表于 2018-10-23 21:02
老哥 我现在编译运行 不能添加书。。输入完第一本书的信息后回车 就卡住 然后自动完成程序
嗯,我知道,所以说代码抄好来~~ claws0n 发表于 2018-10-23 21:05
嗯,我知道,所以说代码抄好来~~
我大概也许 差不多 应该是对着视频打的。。。。难受啊啊啊啊啊 Yo乄 发表于 2018-10-23 21:06
我大概也许 差不多 应该是对着视频打的。。。。难受啊啊啊啊啊
找到以前现成的,改一改吧#include <stdio.h>
#include <stdlib.h>
// insert from head
struct Book
{
char title;
char author;
struct Book *next;
};
void getInput(struct Book *book);
void addBook(struct Book **library);
void printLibrary(struct Book *library);
void releaseLibrary(struct Book **library);
void getInput(struct Book *book)
{
printf("Enter a book name: ");
scanf("%s", book->title);
printf("Enter the author name: ");
scanf("%s", book->author);
}
void addBook(struct Book **library)
{
struct Book *book, *temp;
book = (struct Book *)malloc(sizeof(struct Book));
if (book == NULL)
{
printf("Memory allocation failed");
exit(1);
}
getInput(book);
if(*library != NULL)
{
temp = *library;
*library = book;
book->next = temp;
}
else
{
*library = book;
book->next = NULL;
}
}
void printLibrary(struct Book *library)
{
struct Book *book;
int count = 1;
book = library;
while (book != NULL)
{
printf("Book%d: ", count);
printf("Name: %s\t", book->title);
printf("Author: %s\n", book->author);
book = book->next;
count++;
}
}
void releaseLibrary(struct Book **library)
{
struct Book *temp;
while (library != NULL)
{
temp = *library;
*library = (*library)->next;
free(temp);
}
}
int main(void)
{
struct Book *library = NULL;
int ch;
while (1)
{
printf("Add new book? (Y/N):");
do
{
ch = getchar();
}while (ch != 'Y' && ch != 'N');
if (ch == 'Y')
{
addBook(&library);
}
else
{
break;
}
}
printf("Print books? (Y/N):");
do
{
ch = getchar();
}while (ch != 'Y' && ch != 'N');
if (ch == 'Y')
{
printLibrary(library);
}
releaseLibrary(&library);
system("pause");
return 0;
} Yo乄 发表于 2018-10-23 21:06
我大概也许 差不多 应该是对着视频打的。。。。难受啊啊啊啊啊
改好了,中文的话就真的要自己改了#include <stdio.h>
#include <stdlib.h>
// insert from head
struct Book
{
char title;
char author;
float price;
struct Book *next;
};
void getInput(struct Book *book);
void addBook(struct Book **library);
void printLibrary(struct Book *library);
void releaseLibrary(struct Book **library);
void getInput(struct Book *book)
{
printf("Enter a book name: ");
scanf("%s", book->title);
printf("Enter the author name: ");
scanf("%s", book->author);
printf("Enter the price: ");
scanf("%f", &book->price);
}
void addBook(struct Book **library)
{
struct Book *book, *temp;
book = (struct Book *)malloc(sizeof(struct Book));
if (book == NULL)
{
printf("Memory allocation failed");
exit(1);
}
getInput(book);
if(*library != NULL)
{
temp = *library;
*library = book;
book->next = temp;
}
else
{
*library = book;
book->next = NULL;
}
}
void printLibrary(struct Book *library)
{
struct Book *book;
int count = 1;
book = library;
while (book != NULL)
{
printf("Book%d: ", count);
printf("Name: %s\t", book->title);
printf("Author: %s\t", book->author);
printf("Price: %0.2f\n", book->price);
book = book->next;
count++;
}
}
void releaseLibrary(struct Book **library)
{
struct Book *temp;
while (library != NULL)
{
temp = *library;
*library = (*library)->next;
free(temp);
}
}
int main(void)
{
struct Book *library = NULL;
int ch;
while (1)
{
printf("Add new book? (Y/N):");
do
{
ch = getchar();
}while (ch != 'Y' && ch != 'N');
if (ch == 'Y')
{
addBook(&library);
}
else
{
break;
}
}
printf("Print books? (Y/N):");
do
{
ch = getchar();
}while (ch != 'Y' && ch != 'N');
if (ch == 'Y')
{
printLibrary(library);
}
releaseLibrary(&library);
system("pause");
return 0;
} 行,那我也再来还是同上次 先指出你错误 看图 ,所以改正就比上次更简单了
#include"stdio.h"
#include"stdlib.h"
void getInput (struct Book *book);
void addBook (struct Book **library);
void printLibrary(struct Book *library);
void releaseLibrary(struct Book *library);
struct Book
{
char name;
char author;
int money;
struct Book *next;
};
void getInput (struct Book *book)
{
printf("请输入书名: ");
scanf("%s",book->name);
printf("请输入作者:");
scanf("%s",book->author);
printf("请输入售价:");
scanf("%d",&book->money);
}
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;
}
}
void printLibrary(struct Book *library)
{
struct Book *book;
int count = 1 ;
book = library;
while (book != NULL)
{
printf("Book%d:",count);
printf("书名:%s ",book->name);
printf("作者:%s ",book->author);
printf("售价:%d \n",book->money);
book = book->next;
count ++ ;
}
}
void releaseLibrary(struct Book *library)
{
while (library!=NULL)
{
library=library->next;
free(library);
}
}
int main()
{
struct Book *library = NULL;
char 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')
{
printLibrary(library);
}
releaseLibrary(library);
// addBook(&library);
return 0;
}
加油老哥 {:10_254:} 西瓜小刚 发表于 2018-10-24 10:32
行,那我也再来还是同上次 先指出你错误 看图 ,所以改正就比上次更简单了
难受啊 本帖最后由 Yo乄 于 2018-10-24 12:15 编辑
西瓜小刚 发表于 2018-10-24 10:32
行,那我也再来还是同上次 先指出你错误 看图 ,所以改正就比上次更简单了
提问~~~~~小甲鱼视频里好像没有取址符···· 为啥他的可以编译并正常运行////////// 我错了 。。小甲鱼的没有售价。。。。。。。。。。。。。。。。新提问 : 数组的话就可以不加取址符吗 claws0n 发表于 2018-10-23 21:30
改好了,中文的话就真的要自己改了
谢谢谢谢谢谢 啦啦啦 西瓜小刚 发表于 2018-10-24 10:32
行,那我也再来还是同上次 先指出你错误 看图 ,所以改正就比上次更简单了
66又是这老哥{:10_256:}基本功差一些 他还需要大量练习呀
页:
[1]