- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有这个代码:
#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;
}
目前我只对选项 0 和 3 的函数感兴趣。
当我运行选项 3 menu_print_database 时,尽管输入书籍详细信息时它是正确的年份,但我的年份始终为零。我相当确定我只是在当年的打印品上遗漏了一些东西?也许是转换字符或其他东西?
干杯!
最佳答案
添加是错误的。所以,打印(如果它不是 buggy)不能对此做任何事情。
问题出在这里:
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);
}
如果您想要刷新按值和按引用传递变量,我有一个小例子 here .
按照 OP 的评论和我在链接中的示例,如果您想从 main 调用两个函数并保留更改,您可以这样做:
void bla(int* argumentTwo)
{
*argumentTwo = 32000;
}
/* Definition of function foo. */
void foo(int argumentOne, int* argumentTwo)
{
argumentOne = 1500;
bla(argumentTwo);
}
关于c - 打印转换错误,年份始终为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23596628/
我正在完成一项让我难堪好几天的学校作业。任务是使用 View (VAvailableGolfers) 将与从组合框中选择的给定事件/年份无关的高尔夫球手填充到列表框中。以下是表中的数据: 那么,表单上
给定一年和那年的某一天,我如何获得完整的日期?例如:60/2014 = 2014 年 3 月 1 日 和 61/2016 = 2016 年 3 月 1 日 注释:-年和日可以作为单独的参数传递。 -结
我正在使用 ExtJS 3,当我想为日期选择器选择日期时,我只能设置日期和月份(通过左/右箭头)。月/年面板会放下来,但很快就会消失,因此无法选择它。这是最能描述该行为的桌面记录:http://fli
我正在尝试获取用于在 div 中显示当前月份和年份的 jquery 或 java 脚本代码,但目前还不能。我的意思是我想以这种格式显示当前的月份和年份:October 2012 这样每个月我都不需要编
我有这段代码可以从数据库中获取一个字段: $end_date=$row1['end_date']; 如果我打印它,它会给我这样的信息:25-09-2012我需要的是获取月份值、年份和日期。类似于: $
我有两个格式如下的数据集: df1 #> Artist Album Year #> 1 Beatles Sgt. Pepper's 1967 #>
我使用 jQuery 验证插件和 maskedInput 插件对输入进行了自定义日期和时间验证。 有没有办法在自定义验证中进行验证以防止输入大于当前年份的年份? 我的代码: $("#date").ma
我在 xml 解析方面没有经验,所以也许我写的一些东西对某些人来说看起来很愚蠢,也许我的一些术语不太正确。请原谅。 我开发了一个Android应用程序,它需要解析来自YR.no的天气数据。 。该组织提
我需要当前年份,月份和日期为3个不同的变量。下面的代码给出了日期时间 val now = Calendar.getInstance().getTime() 2016年9月29日星期四18:27:38
在模态对话框中使用日期选择器,请不要在 Firefox 19.0.2 中使用更改月/年下拉列表,请参阅: http://jsfiddle.net/469zV/2/ HTML
我希望日期输入在年份有 4 位数字后停止输入。我尝试使用 HTML 中的 min 和 max 组件,但它仍然允许您键入删除第一个数字。参见示例 1。 请参阅示例 2 以了解可以输入超过 4 位数字的年
因此不同是基于唯一的月/年,而不仅仅是一个不同的月份(所以我希望 2011 年 1 月和 2012 年 1 月是不同的) // Test set of data List Compl
在 Programming in the Key of C# 中,作者给出了一个示例(附源代码),说明如何将日期(年、月、日 -- 数字)打包为 32 位整数。在示例中,作者将信息打包如下: int
这里我有一个任务,如果我写这样的代码,年份应该像 1947 年到 2016 年一样绑定(bind)在 DropdownList 中 1947 2016 拍了一整天 最佳答案 例如,
显然,我的 SQL 不是最好的,但我想做的是通过查找条目中的最大年份和月份来获取数据库中的最新日期。现在我有: select max(Month), max(Year) from posts wher
我试图弄清楚是否有任何 ID 发生在任何早年(即 dfo 中的 Duplicate 列)。如果是这样,我想将该行标记为重复行并包括 ID 首次出现的年份(即 Year_Duplicate)。 我确实有
我已经从 df 中提取了列列表,这些列是字符串类型,如下所示:1999-1(如 1999 年第一个月)。 我想删除 2000 年之前的所有列,因此我提取了列列表并使用列表理解来检查字符串的第一个字符是
日期时间是什么时候?我的 DateTimePicker 控件的源更改为 null,我想选择另一个日期,日历下拉列表默认为最后选择的月份和年份。 我正在为 DOB 使用 DateTimePicker,因
我正在获取系统当前日期并尝试在 TextView 中显示它。 尝试下面的代码后 private OnClickListener listener1 = new OnClickListener() {
我想使用 knockoutJS 库验证年份。我面临的问题是,当用户输入错误的年份时,不会对年份进行验证。以下是年份错误的情况。 (01/01/12 becomes 01/01/0012) (01/0
我是一名优秀的程序员,十分优秀!