什么是在下面的脚本中使用IN和OUT状态的需要? [英] What is the need of using IN and OUT state in the following script?

查看:307
本文介绍了什么是在下面的脚本中使用IN和OUT状态的需要?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好我刚开始通过C语言编程第二版学习C
布赖恩。 W.Kernighnan(ISBN-13:978-8131704943)

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);
}

不过,我做了一个脚本,将执行以下操作,而不需要定义的状态IN和OUT

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);
}

那么究竟是什么这两个,为什么作者用状态来定义IN和OUT的区别?

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


哦!我看到这样的剧本只是为了避免两件事情:
1.避免字数时,有字下面一个以上的空间。
2.其次我的脚本将计数N-2的话我想,如果适当的间距进行。
这让笔者的剧本更fullproof .....还有什么,除了这两个??

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 ??

和感谢您的answes太....

And thank You for your answes too....

P.S:对不起,这是一个有点题外话是确定标签的问题[解决]或者是有这样做?任何其他方式

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 ??

推荐答案

您的版本是从他的略有不同:在你的程序中,如果你有N个连续的空格,它们将被视为N - 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.

这篇关于什么是在下面的脚本中使用IN和OUT状态的需要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆