这个为什么不能打印,求帮忙
#include<stdio.h>#include<stdlib.h>
struct Date
{
int year;
int month;
int day;
};
struct Book
{
char title;
char author;
float price;
struct Date date;
char publisher;
struct Book *pnext;
};
struct Book*AddBook(struct Book *);
void print_book(struct Book*);
int main()
{
struct Book *phead=NULL;
char ch;
printf("欢迎来到图书馆\n");
do{
printf("请问是否需要录入图书(Y/N):") ;
scanf(" %c",&ch);
if(ch=='Y')
{
AddBook(phead);
}
putchar('\n');
}while(ch!='N');
putchar('\n');
do
{
printf("请问是否需要打印图书信息(Y/N):");
scanf(" %c",&ch);
if(ch=='Y')
{
print_book(phead);
}
putchar('\n');
}while(ch!='N');
return 0;
}
struct Book*AddBook(struct Book*phead)
{
struct Book *temp=(struct Book*)malloc(sizeof(struct Book));
printf("请输入书名:");
scanf("%s",temp->title);
printf("请输入作者:");
scanf("%s",temp->author);
printf("请输入售价:");
scanf("%f",&temp->price);
printf("请输入出版日期(以“年-月-日”的格式输入):");
scanf("%d-%d-%d",&temp->date.year,&temp->date.month,&temp->date.day);
printf("请输入出版社:");
scanf(" %s",temp->publisher);
//头插法
if(phead==NULL)
{
phead=temp;
temp->pnext=NULL;
}
else
{
temp->pnext=phead;
phead=temp;
}
}
void print_book(struct Book *phead)
{
struct Book*temp=phead;
int count=1;
while(temp!=NULL)
{
printf("打印第%d本图书信息...\n",count);
printf("书名:%s\n",temp->title);
printf("作者:%s\n",temp->author);
printf("售价:%.2f\n",temp->price);
printf("出版日期:%d-%d-%d\n",temp->date.year,temp->date.month,temp->date.day);
printf("出版社:%s",temp->publisher);
count++;
temp=temp->pnext;
}
}
改变指针的指向就需要传入指针的指针,就是二级指针。把 AddBook 函数改成下面那样:
void AddBook(struct Book** phead)
{
struct Book *temp = (struct Book*)malloc(sizeof(struct Book));
printf("请输入书名:");
scanf("%s", temp->title);
printf("请输入作者:");
scanf("%s", temp->author);
printf("请输入售价:");
scanf("%f", &temp->price);
printf("请输入出版日期(以“年-月-日”的格式输入):");
scanf("%d-%d-%d", &temp->date.year, &temp->date.month, &temp->date.day);
printf("请输入出版社:");
scanf(" %s", temp->publisher);
//头插法
if ( *phead == NULL )
{
*phead = temp;
temp->pnext = NULL;
}
else
{
temp->pnext = *phead;
*phead = temp;
}
}
调用就用 AddBook(&phead); ,还有,你没返回值就声明为void就好了呀。 丶忘却的年少o 发表于 2018-12-15 20:44
改变指针的指向就需要传入指针的指针,就是二级指针。把 AddBook 函数改成下面那样:
照着你的改了,但50行报错C:\Users\薛定谔\Desktop\QQ截图20181216112515.png
#include<stdio.h>
#include<stdlib.h>
struct Date
{
int year;
int month;
int day;
};
struct Book
{
char title;
char author;
float price;
struct Date date;
char publisher;
struct Book *pnext;
};
void AddBook(struct Book *);
void print_book(struct Book*);
int main()
{
struct Book *phead=NULL;
char ch;
printf("欢迎来到图书馆\n");
do{
printf("请问是否需要录入图书(Y/N):") ;
scanf(" %c",&ch);
if(ch=='Y')
{
AddBook(&phead);
}
putchar('\n');
}while(ch!='N');
putchar('\n');
do
{
printf("请问是否需要打印图书信息(Y/N):");
scanf(" %c",&ch);
if(ch=='Y')
{
print_book(phead);
}
putchar('\n');
}while(ch!='N');
return 0;
}
void AddBook(struct Book** phead)
{
struct Book *temp = (struct Book*)malloc(sizeof(struct Book));
printf("请输入书名:");
scanf("%s", temp->title);
printf("请输入作者:");
scanf("%s", temp->author);
printf("请输入售价:");
scanf("%f", &temp->price);
printf("请输入出版日期(以“年-月-日”的格式输入):");
scanf("%d-%d-%d", &temp->date.year, &temp->date.month, &temp->date.day);
printf("请输入出版社:");
scanf(" %s", temp->publisher);
//头插法
if ( *phead == NULL )
{
*phead = temp;
temp->pnext = NULL;
}
else
{
temp->pnext = *phead;
*phead = temp;
}
}
void print_book(struct Book *phead)
{
struct Book*temp=phead;
int count=1;
while(temp!=NULL)
{
printf("打印第%d本图书信息...\n",count);
printf("书名:%s\n",temp->title);
printf("作者:%s\n",temp->author);
printf("售价:%.2f\n",temp->price);
printf("出版日期:%d-%d-%d\n",temp->date.year,temp->date.month,temp->date.day);
printf("出版社:%s",temp->publisher);
count++;
temp=temp->pnext;
}
} 费曼 发表于 2018-12-16 11:27
照着你的改了,但50行报错
#include
#include
忘记改函数原型了{:5_99:}
页:
[1]