我不明白 K&R 书中的 itoa()

发布于 2024-08-06 09:17:40 字数 637 浏览 3 评论 0原文

我正在读《K&R》;到目前为止,我做得很好,但是函数 itoa() 中有一些我不明白的东西。在 itoa() 中,他们说他们自己反转了数字。例如 10 是 01 (它们反转字符串):

void itoa(int n, char s[])
{
    int i, sign;
    if ((sign = n) < 0) /* record sign */
        n = -n; /* make n positive */
    i = 0;
    do { /* generate digits in reverse order */
        s[i++] = n % 10 + '0'; /* get next digit */
    } while ((n /= 10) > 0); /* delete it */
    if (sign < 0)
        s[i++] = '-';
    s[i] = '\0';
    reverse(s);
    return;
}

我不明白它是如何反转数字的。即使我们只是在执行 n % 10 + '0' ,那么接下来的数字 10 然后 1 会被删除,然后它会变为 0,对吗?或者我不明白它的逻辑?

I am reading K&R; so far I'm doing well with it, but there is something in function itoa() which I don't understand. Here in itoa() they say they reverse the numbers themselves. For example 10 is 01 (they reverse the string):

void itoa(int n, char s[])
{
    int i, sign;
    if ((sign = n) < 0) /* record sign */
        n = -n; /* make n positive */
    i = 0;
    do { /* generate digits in reverse order */
        s[i++] = n % 10 + '0'; /* get next digit */
    } while ((n /= 10) > 0); /* delete it */
    if (sign < 0)
        s[i++] = '-';
    s[i] = '\0';
    reverse(s);
    return;
}

I don't understand how it reversed the number. Even though we are just doing n % 10 + '0' then its the following digit which 10 then 1 gets deleted then it goes to 0 right ? Or I don't get its logic ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

人心善变 2024-08-13 09:17:40

在 do-while 循环中,它从后面拉出数字(首先是最低有效数字)。因此,如果您有数字 -123456789,它会处理 9,然后是 8,然后是 7,等等。

因此,当它遇到空终止符(倒数第三行)时,您将得到“987654321-”,即然后被反转。

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.

挽清梦 2024-08-13 09:17:40

n % 10 对于 n = 10 给出 0,因此在循环之后,字符串 s 包含 01 。

reverse() 的调用解决了这个问题。

n % 10 gives 0 for n = 10, so after the loop, the string s contains 01.

The call to reverse() fixes this.

2024-08-13 09:17:40

该算法确定数字从最低有效位到最高有效位的顺序。由于预先不知道将生成的数字总数,因此无法在生成数字时确定正确的位置 - 最低有效数字将位于末尾,但“末尾”未知。因此,它们按照计算顺序(反向)进行缓冲,然后反转整个字符串以纠正顺序。

避免这种情况的一种方法是提前确定长度:

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

但在没有 FPU 的设备上(以及一些具有非常简单的 FPU 的设备),这可能是比字符串反转更繁重的任务。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文