简体   繁体   English

Kernighan 和 Ritchie 的主题 1.6

[英]Topic 1.6 by Kernighan and Ritchie

I was going through the book "The C Programming language" by Kernighan and Ritchie and I am stuck at a topic.我正在阅读 Kernighan 和 Ritchie 的《The C Programming language》一书,但我被困在一个话题上。

Topic number 1.6 talks about Arrays.主题号 1.6 讨论了数组。 In the book, they have included a program that counts the digits, white spaces and other characters.在书中,他们包含了一个计算数字、空格和其他字符的程序。 The program goes like this:程序是这样的:

#include <stdio.h>

 main(){
       int c,i,nother,nwhite;
        int ndigit[10];

        nwhite=nother=0;
        for(i=0;i<10;++i)
            ndigit[i]=0;

        while((c=getchar())!=EOF)
            if (c>='0' && c<='9')
                ++ndigit[c-'0'];

            else if (c==' '|| c=='\t'||c=='\n')
                ++nwhite;
            else
                ++nother;


            printf("digits:");
            for(i=0; i<10;++i)
                printf(" %d",ndigit[i]);
             printf(", white space = %d, other = %d\n",  nwhite, nother);


}

First, I don't understand the purpose of the first for loop that is :首先,我不明白第一个 for 循环的目的是:

 for(i=0;i<10;++i)
             ndigit[i]=0;

And secondly, I can't understand the logic behind this part of the while loop:其次,我无法理解这部分 while 循环背后的逻辑:

if (c>='0' && c<='9')
                ++ndigit[c-'0'];

I really need someone to explain me the logic behind the program so that I can move further with C programming.我真的需要有人向我解释程序背后的逻辑,以便我可以进一步使用 C 编程。

Thanks for the help!谢谢您的帮助!

ndigit[i] holds the number of times digit i (0-9) was counted. ndigit[i]保存数字i (0-9) 被计数的次数。 Eg, ndigit[5] contains the number of times the digit 5 was counted.例如, ndigit[5]包含数字5被计数的次数。 So the first loop just initializes all to 0 , as nothing was seen thus far.所以第一个循环只是将 all 初始化为0 ,因为到目前为止什么都没有看到。

The if statement checks whether the current character c is a digit. if 语句检查当前字符c是否为数字。 If so, it determines which digit it is by subtracting '0' from it.如果是这样,它会通过从中减去'0'来确定它是哪个数字。 This will give the desired index, for which the value contained is increased by one.这将给出所需的索引,其中包含的值增加一。

This loop这个循环

for(i=0;i<10;++i)
             ndigit[i]=0;

is used to set all elements of array ndigit to 0. The array will count numbers of eneterd digits.用于将数组ndigit的所有元素设置为 0。数组将计算 eneterd 位数。

Instead of this loop you could initially initialize all elements of the array to 0 when it was declared.您可以在声明数组时将数组的所有元素初始化为 0,而不是这个循环。

int ndigit[10] = { 0 };

As for this statement至于这个说法

if (c>='0' && c<='9')
                ++ndigit[c-'0'];

then if the entered char is a digit c>='0' && c<='9' then expression c-'0' gives you the integer value of the digit.那么如果输入的字符是一个数字c>='0' && c<='9'那么表达式c-'0'会给你这个数字的整数值。 Characters that correspond to character constant '0' - '9' internally in the computer memory represented by their ASCII or some other coding scheme codes.与计算机内存中的字符常量'0' - '9'相对应的字符,由它们的 ASCII 或其他一些编码方案代码表示。 For example cgaracter '0' in ASCII has internal code 48 , character '1' - 49 , character '2' - 50 and so on.例如 ASCII 中的 cgaracter '0'具有内部代码48 ,字符'1' - 49 ,字符'2' - 50等等。 For example in EBCDIC cgaracter '0' has another code 240 , character '1' - 241 and so on.例如在 EBCDIC cgaracter '0'有另一个代码240 ,字符'1' - 241等等。

The C Standard guarantees that all digits follow each other. C 标准保证所有数字相互跟随。

So if variable c keeps some digit then expression c - '0' gives number from 0 (if c keeps '0' ) to 9 (if c keeps character '9' ).因此,如果变量c保持某个数字,则表达式c - '0'给出从 0 (如果 c 保持 '0' )到 9 (如果 c 保持字符 '9' )的数字。

This value (from 0 to 9) is used as an index in array ndigit .该值(从 0 到 9)用作数组ndigit中的索引。

For example let assume that c keeps character '6' .例如,假设 c 保留字符'6' Then c - '0' will equal to integer number 6. So ndigit[6] is increased然后c - '0'将等于整数 6。因此 ndigit[6] 增加

++ndigit[c-'0']

This element of the array with index 6 counts how many times character '6' was entered.索引为 6 的数组元素计算输入字符“6”的次数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM