Я не понимаю itoa () в книге K&R

Это - просто огромное изображение, состоящее из квадратных блоков, которые загружаются indepedently (использующий Ajax и так далее). Я полагаю, что это сделано некоторыми внутренними библиотеками Google (мог быть также GWT).

[еще 112] по этой теме: http://blog.grimpoteuthis.org/2005/02/mapping-google.html

5
задан 11 revs, 8 users 36% 19 June 2010 в 19:35
поделиться

3 ответа

In the do-while loop, it is pulling the numbers off from behind (the least significant digit first). So, if you had the number -123456789, it processes the 9, then the 8, then the 7, etc.

So, when it hits the null-terminator (3rd to last line), you would have "987654321-", which is then reversed.

12
ответ дан 18 December 2019 в 13:16
поделиться

The algorithm determines the digits from least to most significant order. Because the total number of digits that will be generated is not known in advance, the correct position cannot be determined as they are generated - the least significant digit will be at the end, but the 'end' is not known. So they are buffered in the order they are calculated (reverse) and then the whole string is reversed to correct the ordering.

One way of avoiding this is to determine the length in advance:

decimal_digits = (int)log10( n ) + 1 ;

but on devices without an FPU (and some with very simple FPUs) that is likely to be a heavier task than string reversal.

0
ответ дан 18 December 2019 в 13:16
поделиться

n% 10 дает 0 для n = 10 , поэтому после цикла строка s содержит 01 .

Вызов reverse () исправляет это.

2
ответ дан 18 December 2019 в 13:16
поделиться
Другие вопросы по тегам:

Похожие вопросы: