Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 打印转换错误,年份始终等于零_C - Fatal编程技术网

C 打印转换错误,年份始终等于零

C 打印转换错误,年份始终等于零,c,C,我有以下代码: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <assert.h> #define MAX_TITLE_LENGTH 100 #define MAX_AUTHOR_LENGTH 100 /* Book structure */ struct Book { /* Book details */ char title[MAX_T

我有以下代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#define MAX_TITLE_LENGTH  100
#define MAX_AUTHOR_LENGTH 100

/* Book structure */

struct Book
{
   /* Book details */
   char title[MAX_TITLE_LENGTH+1];   /* name string */
   char author[MAX_AUTHOR_LENGTH+1]; /* job string */
   int year;                         /* year of publication */

   /* pointers to left and right branches pointing down to next level in
      the binary tree */
   struct Book *left, *right;
};

/* tree of books, initialized to NULL. */
static struct Book *book_tree = NULL;

/* read_line():
 *
 * Read line of characters from file pointer "fp", copying the characters
 * into the "line" string, up to a maximum of "max_length" characters, plus
 * one for the string termination character '\0'. Reading stops upon
 * encountering the end-of-line character '\n', for which '\0' is substituted
 * in the string. If the end of file character EOF is reached before the end
 * of the line, the failure condition (-1) is returned. If the line is longer
 * than the maximum length "max_length" of the string, the extra characters
 * are read but ignored. Success is returned (0) on successfully reading
 * a line.
 */
static int read_line ( FILE *fp, char *line, int max_length )
{
   int i;
   char ch;

   /* initialize index to string character */
   i = 0;

   /* read to end of line, filling in characters in string up to its
      maximum length, and ignoring the rest, if any */
   for(;;)
   {
      /* read next character */
      ch = fgetc(fp);

      /* check for end of file error */
      if ( ch == EOF )
     return -1;

      /* check for end of line */
      if ( ch == '\n' )
      {
     /* terminate string and return */
     line[i] = '\0';
     return 0;
      }

      /* fill character in string if it is not already full*/
      if ( i < max_length )
     line[i++] = ch;
   }

   /* the program should never reach here */
   return -1;
}

/* read_string():
 *
 * Reads a line from the input file pointer "fp", starting with the "prefix"
 * string, and filling the string "string" with the remainder of the contents
 * of the line. If the start of the line does not match the "prefix" string,
 * the error condition (-1) is returned. Having read the prefix string,
 * read_string() calls read_line() to read the remainder of the line into
 * "string", up to a maximum length "max_length", and returns the result.
 */
static int read_string ( FILE *fp,
             char *prefix, char *string, int max_length )
{
   int i;

   /* read prefix string */
   for ( i = 0; i < strlen(prefix); i++ )
      if ( fgetc(fp) != prefix[i] )
     /* file input doesn't match prefix */
     return -1;

   /* read remaining part of line of input into string */
   return ( read_line ( fp, string, max_length ) );
}

/* book_details(): */
static void book_details (char title[MAX_TITLE_LENGTH+1] , char author[MAX_AUTHOR_LENGTH+1] , int year  , int bn)
{
   char line[301];

   fprintf(stderr,"Enter Title:\n");
   read_line (stdin, line, 101);
   strcpy(title , line);
   fprintf(stderr,"Entered Title: %s\n",title);

   fprintf(stderr,"Enter Author:\n");
   read_line (stdin, line, 101);
   strcpy(author , line);
   fprintf(stderr,"Entered Author %s\n",author);

   fprintf(stderr,"Enter Year:\n");
   read_line (stdin, line, 4);
   sscanf(line,"%i" , &year);
   fprintf(stderr,"Entered Year: %i\n",year);   

   fprintf (stderr,"Book Number = %i\n",bn);
}

/* compare */
static void compare(struct Book *new , struct Book *book_tree)
{
   if (strcmp (new->title , book_tree->title) > 0 ) 
      { 
         if (book_tree->right == NULL) 
            {
            book_tree->right = new; 
            fprintf(stderr, "\nBook successfully added to tree\n\n"); 
            }
         else 
            {
            book_tree = book_tree->right;
            compare(new , book_tree);
            }
      }
   else
      { 
         if (book_tree->left == NULL) 
            { 
            book_tree->left = new; 
            fprintf(stderr, "\nBook successfully added to tree\n\n"); 

            } 
         else 
            {
            book_tree = book_tree->left;
            compare(new , book_tree);
            }
      }
}

/* place_book(): */
static void place_book (char title[MAX_TITLE_LENGTH+1] , char author[MAX_AUTHOR_LENGTH+1] , int year , int bn)
{
   struct Book *new;
   new = (struct Book *) malloc ( sizeof(struct Book) );

   strcpy ( new->title, title );
   strcpy ( new->author, author );
   new->year = year;
   new->left = new->right = NULL;

   if (bn == 1)   
      {
         if (book_tree == NULL)   
            {   
            book_tree = new; 
            fprintf(stderr, "\nBook successfully added to tree\n\n");   
            }   
         else   
            fprintf(stderr, "\nBook unsuccessfully added to tree\n\n");   
      }

   if (bn >= 2) compare(new , book_tree);    
}

/* menu_add_book(): */
static void menu_add_book (char title[MAX_TITLE_LENGTH+1] , char author[MAX_AUTHOR_LENGTH+1] , int year , int bn)
{
   book_details(title , author , year , bn);
   place_book(title , author , year , bn); 
}

/* descend () */
static void descend (struct Book *print_pointer)
{
   if (print_pointer->left != NULL) 
      descend(print_pointer->left);

   printf ("Title: %s\n" , print_pointer->title);
   printf ("Author: %s\n" , print_pointer->author);
   printf ("Year: %i\n\n" , print_pointer->year); 

   if (print_pointer->right != NULL) 
      descend(print_pointer->right);
}

/* menu_print_database(): */
static void menu_print_database (char title[MAX_TITLE_LENGTH+1] , char author[MAX_AUTHOR_LENGTH+1] , int year , int bn)
{
struct Book *print_pointer = book_tree;
descend(print_pointer);
}

/* codes for menu */
#define ADD_CODE     0
#define DETAILS_CODE 1
#define DELETE_CODE  2
#define PRINT_CODE   3
#define TREE_CODE    4
#define EXIT_CODE    5

int main ( int argc, char *argv[] )
{
   char title[MAX_TITLE_LENGTH+1];   /* name string */
   char author[MAX_AUTHOR_LENGTH+1]; /* job string */
   int year = 0;                     /* year of publication */
   int bn = 0;                       /* book number */

   /* check arguments */
   if ( argc != 1 && argc != 2 )
   {
      fprintf ( stderr, "Usage: %s [<database-file>]\n", argv[0] );
      exit(-1);
   }

   /* read database file if provided, or start with empty database */
   if ( argc == 2 )
      read_book_database ( argv[1] );

   for(;;)
   {
      int choice, result;
      char line[301];

      /* print menu to standard error */
      fprintf ( stderr, "\nOptions:\n" );
      fprintf ( stderr, "%d: Add new book to database\n",      ADD_CODE );
      fprintf ( stderr, "%d: Get details of book\n",       DETAILS_CODE );
      fprintf ( stderr, "%d: Delete book from database\n",  DELETE_CODE );
      fprintf ( stderr, "%d: Print database to screen\n",    PRINT_CODE );
      fprintf ( stderr, "%d: Print tree\n",                   TREE_CODE );
      fprintf ( stderr, "%d: Exit database program\n",        EXIT_CODE );
      fprintf ( stderr, "\nEnter option: " );

      if ( read_line ( stdin, line, 300 ) != 0 ) continue;

      result = sscanf ( line, "%d", &choice );
      if ( result != 1 )
      {
     fprintf ( stderr, "corrupted menu choice\n" );
     continue;
      }

      switch ( choice )
      {
         case ADD_CODE: /* add book to database */
         bn++;
     menu_add_book(title , author , year , bn);
     break;

         case DETAILS_CODE: /* get book details from database */
     menu_get_book_details();
     break;

         case DELETE_CODE: /* delete book from database */
     menu_delete_book();
     break;

         case PRINT_CODE: /* print database contents to screen (standard output) */
         menu_print_database(title , author , year , bn);
     break;

         case TREE_CODE: /* print tree to screen (standard output) */
     menu_print_tree();
     break;

     /* exit */
         case EXIT_CODE:
     break;

         default:
     fprintf ( stderr, "illegal choice %d\n", choice );
     break;
      }

      /* check for exit menu choice */
      if ( choice == EXIT_CODE )
     break;
   }

   return 0;   
}
#包括
#包括
#包括
#包括
#定义最大标题长度100
#定义最大作者长度100
/*图书结构*/
结构书
{
/*书籍详情*/
字符标题[最大标题长度+1];/*名称字符串*/
char author[MAX_author_LENGTH+1];/*作业字符串*/
int年;/*出版年份*/
/*指向左分支和右分支的指针,指向中的下一个级别
二叉树*/
结构书*左,*右;
};
/*书籍树,初始化为空*/
静态结构Book*Book_tree=NULL;
/*读一行():
*
*从文件指针“fp”读取字符行,复制字符
*输入“行”字符串,最多为“max_length”字符,加上
*一个用于字符串终止字符“\0”。阅读停止于
*遇到行尾字符'\n',替换了'\0'
*在字符串中。如果在结束之前达到文件结束字符EOF
*对于该行,返回故障条件(-1)。如果线路较长
*超过字符串的最大长度“max_length”,额外的字符
*被读取但被忽略。成功读取时返回成功(0)
*一行。
*/
静态整型读取行(文件*fp,字符*line,整型最大长度)
{
int i;
char ch;
/*将索引初始化为字符串*/
i=0;
/*读到行尾,在字符串中填入字符,直到其
最大长度,忽略其余部分(如果有)*/
对于(;;)
{
/*读下一个字符*/
ch=fgetc(fp);
/*检查文件结束错误*/
如果(ch==EOF)
返回-1;
/*检查生产线的末端*/
如果(ch='\n')
{
/*终止字符串并返回*/
第[i]行=“\0”;
返回0;
}
/*如果字符串尚未满,则在字符串中填充该字符*/
如果(i<最大长度)
行[i++]=ch;
}
/*程序永远不应该到达这里*/
返回-1;
}
/*读取字符串():
*
*从输入文件指针“fp”读取一行,以“前缀”开头
*字符串,并用剩余内容填充字符串“string”
*排队等候。如果行的开头与“前缀”字符串不匹配,
*返回错误条件(-1)。读取了前缀字符串之后,
*read_string()调用read_line()将该行的其余部分读入
*“字符串”,最大长度为“max_length”,并返回结果。
*/
静态整型读取字符串(文件*fp,
字符*前缀,字符*字符串,整数最大长度)
{
int i;
/*读取前缀字符串*/
对于(i=0;i标题,图书目录树->标题)>0)
{ 
if(book_tree->right==NULL)
{
书本树->右=新建;
fprintf(stderr,“\n书本已成功添加到树\n\n”);
}
其他的
{
书本树=书本树->右侧;
比较(新的,书本树);
}
}
其他的
{ 
如果(书本树->左==NULL)
{ 
书本树->左=新建;
fprintf(stderr,“\n书本已成功添加到树\n\n”);
} 
其他的
{
书本树=书本树->左;
比较(新的,书本树);
}
}
}
/*放在书上():*/
静态无效位置书(字符标题[MAX_title_LENGTH+1],字符作者[MAX_author_LENGTH+1],整数年,整数bn)
{
结构书*新;
新=(结构书*)malloc(sizeof(结构书));
strcpy(新建->标题,标题);
strcpy(新建->作者,作者);
新->年=年;
新建->左=新建->右=空;
如果(bn==1)
{
if(book_tree==NULL)
{   
图书树=新的;
fprintf(stderr,“\n书本已成功添加到树\n\n”);
}   
其他的
fprintf(stderr,“\n将书本添加到树中失败\n\n”);
}
如果(bn>=2)比较(新的,书本树);
}
/*菜单添加书籍():*/
静态无效菜单添加书籍(字符标题[最大标题长度+1],字符作者[最大作者长度+1],整数年,整数bn)
{
书籍详细信息(标题、作者、年份、bn);
地名书(标题、作者、年份、bn);
}
/*下降()*/
静态无效下降(结构书*打印指针)
{
如果(打印指针->左!=NULL)
下降(打印指针->左);
printf(“标题:%s\n”,打印指针->标题);
printf(“作者:%s\n”,打印指针->作者);
printf(“年:%i\n\n”,打印指针->年);
如果(打印指针->右!=NULL)
下降(打印指针->右侧);
}
/*菜单\打印\数据库():*/
静态无效菜单打印数据库(字符标题[最大标题长度+1],字符作者[最大作者长度+1],整数年,整数bn)
{
结构书本*打印指针=书本树;
下降(打印指针);
}
/*菜单代码*/
#定义添加代码0
#定义详细信息\u代码1
#定义删除代码2
#定义打印代码3
#定义树_代码4
#定义退出代码5
int main(int argc,char*argv[])
{
字符标题[最大标题长度]
static void book_details(char title[MAX_TITLE_LENGTH + 1],
                         char author[MAX_AUTHOR_LENGTH + 1], int year, int bn) {
  char line[301];

  fprintf(stderr, "Enter Year:\n");
  read_line(stdin, line, 4);
  // I changed %i to %d, which is the one you should use for integers
  sscanf(line, "%d", &year);
  fprintf(stderr, "Entered Year: %d\n", year);

  // and here yes, year has the value that you expect, but because it is passed by value, when the function is terminated, all the changes made to year will be lost
}

static void menu_add_book(char title[MAX_TITLE_LENGTH + 1],
                      char author[MAX_AUTHOR_LENGTH + 1], int year, int bn) {
  // you call book_details here, and you pass year by value!!!!!
  book_details(title, author, year, bn);
  place_book(title, author, year, bn);
}
void bla(int* argumentTwo)
{
  *argumentTwo = 32000;
}


/* Definition of function foo. */
void foo(int argumentOne, int* argumentTwo)
{
    argumentOne = 1500;
    bla(argumentTwo);
}