What is the need of using IN and OUT state in the following script?

半腔热情 提交于 2019-12-23 03:32:31

问题


Hello everyone I just started learning C through THE c PROGRAMMING LANGUAGE Second Edition by Brian. W.Kernighnan (ISBN-13: 978-8131704943)

So here is a script which counts the characters, line, words

#include <stdio.h>

#define IN  1
#define OUT 0

main()
{
    int c, nl, nw, nc, state; 
    /* c = input, nl = new line, nc = new character, nw = new word, state = (IN/OUT) */
    state = OUT;
    nl = nw = nc = 0;
    while ((c = getchar()) != EOF)
    {
        ++nc;
        if (c == '\n')
            ++nl;
        if (c == ' ' || c == '\n' || c == '\t')
            state = OUT;
        else if (state == OUT)
        {
            state = IN;
            ++nw;
        }
    }
    printf(" The number of lines is: %d,\n The number of words is: %d,\n The number of characters is: %d. \n", nl, nw, nc);
}

However I made a script which does the following without the need of defining state IN and OUT

#include <stdio.h>
main()
{
    int nw, nl, nc, c ;
    nl = nw = nc = c = 0 ;
    while ((c = getchar()) != EOF)
    {
        ++nc;
        if (c == '\n')
            ++nl;
        else if (c == ' ' || c == '\n' || c == '\t')
            ++nw;
    }
    printf("Words:%d \nCharacters:%d \nLines:%d", nw, nc, nl);
}

So what is the difference between these two, why does the author use the state to define IN and OUT ??

[EDIT] Oh! I see so the script is just to avoid two things : 1. To avoid word count when there are more than one spaces following the word. 2. Secondly my script would count n-2 words I suppose if proper spacing is done. Which makes the author's script more fullproof.....Is there anything else except these two ??

And thank You for your answes too....

P.S: I'm sorry this is a bit off-topic is it ok to label the question [SOLVED] or is there any other way of doing this ??


回答1:


Your version is slightly different from his one: in your program, if you have N consecutive spaces, they will be considered as N - 1 words, because for every space you add one to the word count. Also, the last input word won't be considered.




回答2:


IN literally means "inside a word" and OUT literally means "outside a word". He is tracking the state of the proverbial cursor as he moves through the lines.



来源:https://stackoverflow.com/questions/23609836/what-is-the-need-of-using-in-and-out-state-in-the-following-script

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!