鱼C论坛

 找回密码
 立即注册
查看: 1853|回复: 6

[已解决]这不是我想要的输出结果啊!

[复制链接]
发表于 2021-2-13 20:30:07 | 显示全部楼层 |阅读模式

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

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

x
昨天刚学完结构体,小甲鱼布置了个图书馆程序的课后作业,我写的程序可以运行,可是输出结果总不正确,写程序花了1个小时,改BUG花了我2个半小时还没搞好,请各位大神帮看看,改好我好开始学习链表了
#include <stdio.h>
#include <string.h>
#include <stdlib.h>


struct Date
{
        int year;
        int month;
        int day;
};

struct Book
{
        char title[128];
        char auther[40];
        float price;
        struct Date date;
        char publisher[40];
};




//录入书本信息
void getInput(struct Book *book,int *hao2)
{
       
        printf("请输入书名:");
        scanf("%s",book->title);
        printf("请输入作者:");
        scanf("%s",book->auther);
        printf("请输入售价:");
        scanf("%f",&book->price);
        printf("请输入出版日期:");
        scanf("%d %d %d",&(*book).date.year,&(*book).date.month,&book->date.day);
        printf("请输入出版社:");
        scanf("%s",book->publisher);
        (*hao2)++;
        printf("\n录入成功!!!\n书本编号为:%d\n",*hao2);
       

}

//打印输入结果
void printBook(struct Book *book )
{
        printf("书名:%s\n",book->title);
        printf("作者:%s\n",(*book).auther);
        printf("出版日期:%d-%d-%d\n",(*book).date.year,(*book).date.month,(*book).date.day);
        printf("售价:%.2f\n",(*book).price);
        printf("出版社:%s\n",(*book).publisher);
       

}
int main(void)
{       
       
        int i=0,hao1=1000,zhao1,c=1;
        int*hao2;
        hao2=&hao1;
        struct Book *b;
        struct Book *library;
       
        b = (struct Book*) malloc(sizeof(struct Book));
        //b = (struct Book*) malloc(sizeof(struct Book));
        //b =
        /*        if(b[i] == NULL)
                {
                        printf("申请失败!!!");
                        exit(1);
                }
        */
                while(c!=-1)
        {
       
               
                printf("请输入书的信息;\n");
                getInput((b+i),hao2);
                putchar('\n');
                i++;
                        printf("\n是否继续录入书本信息?输入-1退出输入,输入1继续录\n");
        scanf("%d",&c);
                b++;
       
        }
       
        printf("请问是否要查找书本?,1--查找,0--退出\n");
        scanf("%d",&zhao1);
        if(zhao1==1)
        {

//============这里就是查找书籍的模块了============================//

                printf("请输入您要查找的书的编号\n");
                scanf("%d",&hao1);
               
                if(hao1<1000||hao1>1050)
                {
                        printf("对不起,输入无效,请检查\n");
                }
                else
                {
                       
                        hao1 = hao1-1002;
//====================这里是函数打印语句=======================//
                        printBook(&(b[hao1]));
                }
        }
        free(b);
       
return 0;
       
最佳答案
2021-2-13 22:29:46
本帖最后由 一叶枫残 于 2021-2-13 22:33 编辑

大部分没错的,简单改改就ok了,看注释,不明白再问
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>


  4. struct Date
  5. {
  6.         int year;
  7.         int month;
  8.         int day;
  9. };

  10. struct Book
  11. {
  12.         char title[128];
  13.         char auther[40];
  14.         float price;
  15.         struct Date date;
  16.         char publisher[40];
  17. };




  18. //录入书本信息
  19. void getInput(struct Book *book,int *hao2)
  20. {
  21.       
  22.         printf("请输入书名:");
  23.         scanf("%s",book->title);
  24.         printf("请输入作者:");
  25.         scanf("%s",book->auther);
  26.         printf("请输入售价:");
  27.         scanf("%f",&book->price);
  28.         printf("请输入出版日期:");
  29.         scanf("%d %d %d",&(*book).date.year,&(*book).date.month,&book->date.day);
  30.         printf("请输入出版社:");
  31.         scanf("%s",book->publisher);
  32.         (*hao2)++;
  33.         printf("\n录入成功!!!\n书本编号为:%d\n",*hao2);
  34.       

  35. }

  36. //打印输入结果
  37. void printBook(struct Book *book )
  38. {
  39.         printf("书名:%s\n",book->title);
  40.         printf("作者:%s\n",(*book).auther);
  41.         printf("出版日期:%d-%d-%d\n",(*book).date.year,(*book).date.month,(*book).date.day);
  42.         printf("售价:%.2f\n",(*book).price);
  43.         printf("出版社:%s\n",(*book).publisher);
  44.       

  45. }
  46. int main(void)
  47. {      
  48.       
  49.         int i=0,hao1=1000,zhao1,c=1,j=0;        //这里多定义一个j,方便释放内存(i的数据要保留,不然清除了就不知道分配了多少个结构体指针的内存)
  50.         int*hao2;
  51.         hao2=&hao1;
  52.         struct Book *b[20];        //这里改用指针数组,就是用来放指针的数组
  53.         struct Book *library;
  54.       
  55.         while(c!=-1)
  56.         {
  57.       
  58.                        b[i] = (struct Book*) malloc(sizeof(struct Book));        //每放一本书,就分配一次内存
  59.                 printf("请输入书的信息;\n");
  60.                 getInput( b[i] , hao2);
  61.                 putchar('\n');
  62.                 i++;
  63.                 printf("\n是否继续录入书本信息?输入-1退出输入,输入1继续录\n");
  64.                         scanf("%d",&c);
  65.                                                                                        //b++删掉
  66.         }
  67.       
  68.         printf("请问是否要查找书本?,1--查找,0--退出\n");
  69.         scanf("%d",&zhao1);
  70.         if(zhao1==1)
  71.         {

  72. //============这里就是查找书籍的模块了============================//

  73.                 printf("请输入您要查找的书的编号\n");
  74.                 scanf("%d",&hao1);
  75.                
  76.                 if(hao1<1000||hao1>1050)
  77.                 {
  78.                         printf("对不起,输入无效,请检查\n");
  79.                 }
  80.                 else
  81.                 {
  82.                        
  83.                         hao1 = hao1-1001;        //这里改为-1001,因为数组从0开始算
  84. //====================这里是函数打印语句=======================//
  85.                         printBook( b[hao1] );                //因为这个数组存放的就是指针,所以不用加&
  86.                 }
  87.         }
  88.         for(j=0;j<i;j++)                        //分配的内存都释放掉,i的作用就是保存分配了多少个内存的数据
  89.         {
  90.                 free( b[j] );               
  91.         }
  92.         
  93.       
  94. return 0;

  95. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-2-13 21:58:39 | 显示全部楼层
getInput((b+i),hao2);这里怎么能随便传入结构体地址加一个整型值。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-2-13 22:21:15 | 显示全部楼层
本帖最后由 一叶枫残 于 2021-2-13 22:44 编辑

1.如果你要存多本书的信息,建议用结构体数组;(后面会有更好用的单链表会学到的,现在没学可以用结构体数组)
2.每个结构体都要分配内存,但你的程序只分配了1个结构体指针的内存;
3.你前面没定义结构体的数组不要这样用:printBook(&(b[hao1]);
4.while这个b++也很随便,不能这样用;
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-2-13 22:29:46 | 显示全部楼层    本楼为最佳答案   
本帖最后由 一叶枫残 于 2021-2-13 22:33 编辑

大部分没错的,简单改改就ok了,看注释,不明白再问
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>


  4. struct Date
  5. {
  6.         int year;
  7.         int month;
  8.         int day;
  9. };

  10. struct Book
  11. {
  12.         char title[128];
  13.         char auther[40];
  14.         float price;
  15.         struct Date date;
  16.         char publisher[40];
  17. };




  18. //录入书本信息
  19. void getInput(struct Book *book,int *hao2)
  20. {
  21.       
  22.         printf("请输入书名:");
  23.         scanf("%s",book->title);
  24.         printf("请输入作者:");
  25.         scanf("%s",book->auther);
  26.         printf("请输入售价:");
  27.         scanf("%f",&book->price);
  28.         printf("请输入出版日期:");
  29.         scanf("%d %d %d",&(*book).date.year,&(*book).date.month,&book->date.day);
  30.         printf("请输入出版社:");
  31.         scanf("%s",book->publisher);
  32.         (*hao2)++;
  33.         printf("\n录入成功!!!\n书本编号为:%d\n",*hao2);
  34.       

  35. }

  36. //打印输入结果
  37. void printBook(struct Book *book )
  38. {
  39.         printf("书名:%s\n",book->title);
  40.         printf("作者:%s\n",(*book).auther);
  41.         printf("出版日期:%d-%d-%d\n",(*book).date.year,(*book).date.month,(*book).date.day);
  42.         printf("售价:%.2f\n",(*book).price);
  43.         printf("出版社:%s\n",(*book).publisher);
  44.       

  45. }
  46. int main(void)
  47. {      
  48.       
  49.         int i=0,hao1=1000,zhao1,c=1,j=0;        //这里多定义一个j,方便释放内存(i的数据要保留,不然清除了就不知道分配了多少个结构体指针的内存)
  50.         int*hao2;
  51.         hao2=&hao1;
  52.         struct Book *b[20];        //这里改用指针数组,就是用来放指针的数组
  53.         struct Book *library;
  54.       
  55.         while(c!=-1)
  56.         {
  57.       
  58.                        b[i] = (struct Book*) malloc(sizeof(struct Book));        //每放一本书,就分配一次内存
  59.                 printf("请输入书的信息;\n");
  60.                 getInput( b[i] , hao2);
  61.                 putchar('\n');
  62.                 i++;
  63.                 printf("\n是否继续录入书本信息?输入-1退出输入,输入1继续录\n");
  64.                         scanf("%d",&c);
  65.                                                                                        //b++删掉
  66.         }
  67.       
  68.         printf("请问是否要查找书本?,1--查找,0--退出\n");
  69.         scanf("%d",&zhao1);
  70.         if(zhao1==1)
  71.         {

  72. //============这里就是查找书籍的模块了============================//

  73.                 printf("请输入您要查找的书的编号\n");
  74.                 scanf("%d",&hao1);
  75.                
  76.                 if(hao1<1000||hao1>1050)
  77.                 {
  78.                         printf("对不起,输入无效,请检查\n");
  79.                 }
  80.                 else
  81.                 {
  82.                        
  83.                         hao1 = hao1-1001;        //这里改为-1001,因为数组从0开始算
  84. //====================这里是函数打印语句=======================//
  85.                         printBook( b[hao1] );                //因为这个数组存放的就是指针,所以不用加&
  86.                 }
  87.         }
  88.         for(j=0;j<i;j++)                        //分配的内存都释放掉,i的作用就是保存分配了多少个内存的数据
  89.         {
  90.                 free( b[j] );               
  91.         }
  92.         
  93.       
  94. return 0;

  95. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-2-14 08:47:52 | 显示全部楼层
一叶枫残 发表于 2021-2-13 21:58
getInput((b+i),hao2);这里怎么能随便传入结构体地址加一个整型值。。。

hao2是一个基本指针变量,不是整型变量啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-2-14 09:32:47 | 显示全部楼层
谢谢你,我也会加油的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-2-14 17:44:51 | 显示全部楼层
三刀流.索隆 发表于 2021-2-14 08:47
hao2是一个基本指针变量,不是整型变量啊

我说那个b+i,指针加整数一般只有声明了数组之后才用
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-26 07:41

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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