鱼C论坛

 找回密码
 立即注册
查看: 2607|回复: 11

我又来了了了了了了了!!!!!!!

[复制链接]
发表于 2018-10-23 20:33:10 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 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[128];
        char author[40];
        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;
 } 

又是一片红啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-10-23 20:51:15 | 显示全部楼层
没有红了,但是 addBook 还是哪里有问题,不能继续添加,代码抄好来
1. 你的函数声明必须在结构体的定义后面,不然编译器不知道什么是 struct Book
2. NULL 不是 NUll
3. 标准库用尖箭头
#include <stdio.h>
#include <stdlib.h>

struct Book 
{
        char name[128];
        char author[40];
        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;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2018-10-23 21:02:50 | 显示全部楼层
claws0n 发表于 2018-10-23 20:51
没有红了,但是 addBook 还是哪里有问题,不能继续添加,代码抄好来
1. 你的函数声明必须在结构体的定义后 ...

老哥   我现在编译运行   不能添加书。。  输入完第一本书的信息后回车 就卡住 然后自动完成程序
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-10-23 21:05:40 | 显示全部楼层
Yo乄 发表于 2018-10-23 21:02
老哥   我现在编译运行   不能添加书。。  输入完第一本书的信息后回车 就卡住 然后自动完成程序

嗯,我知道,所以说代码抄好来~~
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-10-23 21:06:20 | 显示全部楼层
claws0n 发表于 2018-10-23 21:05
嗯,我知道,所以说代码抄好来~~

我大概也许 差不多   应该是对着视频打的。。。。  难受啊啊啊啊啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-10-23 21:22:17 | 显示全部楼层
Yo乄 发表于 2018-10-23 21:06
我大概也许 差不多   应该是对着视频打的。。。。  难受啊啊啊啊啊

找到以前现成的,改一改吧
#include <stdio.h>
#include <stdlib.h>

// insert from head

struct Book
{
       char title[120];
       char author[40];
       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;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-10-23 21:30:29 | 显示全部楼层
Yo乄 发表于 2018-10-23 21:06
我大概也许 差不多   应该是对着视频打的。。。。  难受啊啊啊啊啊

改好了,中文的话就真的要自己改了
#include <stdio.h>
#include <stdlib.h>

// insert from head

struct Book
{
       char title[120];
       char author[40];
       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;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-10-24 10:32:33 | 显示全部楼层
行,那我也再来  还是同上次 先指出你错误 看图 ,所以改正就比上次更简单了
#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[128];
        char author[40];
        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;
}


加油老哥

粗心害人

粗心害人
粗心害人.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-10-24 12:10:28 | 显示全部楼层
西瓜小刚 发表于 2018-10-24 10:32
行,那我也再来  还是同上次 先指出你错误 看图 ,所以改正就比上次更简单了

难受啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-10-24 12:13:26 | 显示全部楼层
本帖最后由 Yo乄 于 2018-10-24 12:15 编辑
西瓜小刚 发表于 2018-10-24 10:32
行,那我也再来  还是同上次 先指出你错误 看图 ,所以改正就比上次更简单了


提问~~~~~小甲鱼视频里好像没有取址符  ····   为啥他的可以编译并正常运行//////////   我错了 。。小甲鱼的没有售价。。。。。。。。。。。。。。。。新提问 : 数组的话就可以不加取址符吗  
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-10-24 12:16:21 | 显示全部楼层
claws0n 发表于 2018-10-23 21:30
改好了,中文的话就真的要自己改了

谢谢谢谢谢谢   啦啦啦
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-10-30 09:15:46 | 显示全部楼层
西瓜小刚 发表于 2018-10-24 10:32
行,那我也再来  还是同上次 先指出你错误 看图 ,所以改正就比上次更简单了

66又是这老哥基本功差一些 他还需要大量练习呀
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-10-6 12:55

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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