鱼C论坛

 找回密码
 立即注册
查看: 1230|回复: 3

[已解决]这个为什么不能打印,求帮忙

[复制链接]
发表于 2018-12-15 20:24:50 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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

struct Date
{
        int year;
        int month;
        int day;
};
struct Book
{
        char title[128];
        char author[48];
        float price;
        struct Date date;
        char publisher[48];
        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;
        }
       
}
最佳答案
2018-12-15 20:44:48
改变指针的指向就需要传入指针的指针,就是二级指针。把 AddBook 函数改成下面那样:
  1. void AddBook(struct Book** phead)
  2. {
  3.         struct Book *temp = (struct Book*)malloc(sizeof(struct Book));
  4.         printf("请输入书名:");
  5.         scanf("%s", temp->title);
  6.         printf("请输入作者:");
  7.         scanf("%s", temp->author);
  8.         printf("请输入售价:");
  9.         scanf("%f", &temp->price);
  10.         printf("请输入出版日期(以“年-月-日”的格式输入):");
  11.         scanf("%d-%d-%d", &temp->date.year, &temp->date.month, &temp->date.day);
  12.         printf("请输入出版社:");
  13.         scanf(" %s", temp->publisher);
  14.         //头插法
  15.         if ( *phead == NULL )
  16.         {
  17.                 *phead = temp;
  18.                 temp->pnext = NULL;
  19.         }
  20.         else
  21.         {
  22.                 temp->pnext = *phead;
  23.                 *phead = temp;
  24.         }
  25. }
复制代码


调用就用 AddBook(&phead); ,还有,你没返回值就声明为void就好了呀。
QQ截图20181215202347.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-12-15 20:44:48 | 显示全部楼层    本楼为最佳答案   
改变指针的指向就需要传入指针的指针,就是二级指针。把 AddBook 函数改成下面那样:
  1. void AddBook(struct Book** phead)
  2. {
  3.         struct Book *temp = (struct Book*)malloc(sizeof(struct Book));
  4.         printf("请输入书名:");
  5.         scanf("%s", temp->title);
  6.         printf("请输入作者:");
  7.         scanf("%s", temp->author);
  8.         printf("请输入售价:");
  9.         scanf("%f", &temp->price);
  10.         printf("请输入出版日期(以“年-月-日”的格式输入):");
  11.         scanf("%d-%d-%d", &temp->date.year, &temp->date.month, &temp->date.day);
  12.         printf("请输入出版社:");
  13.         scanf(" %s", temp->publisher);
  14.         //头插法
  15.         if ( *phead == NULL )
  16.         {
  17.                 *phead = temp;
  18.                 temp->pnext = NULL;
  19.         }
  20.         else
  21.         {
  22.                 temp->pnext = *phead;
  23.                 *phead = temp;
  24.         }
  25. }
复制代码


调用就用 AddBook(&phead); ,还有,你没返回值就声明为void就好了呀。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-12-16 11:27:10 | 显示全部楼层
丶忘却的年少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[128];
        char author[48];
        float price;
        struct Date date;
        char publisher[48];
        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;
        }
       
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-12-16 13:19:20 | 显示全部楼层
费曼 发表于 2018-12-16 11:27
照着你的改了,但50行报错
#include
#include

忘记改函数原型了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-20 03:48

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表