Can';我无法理解C语言中的单词计数
[已解决] 我最近问了一个关于一些代码的问题,我认为我真的很理解这些代码 但几天后,当我回顾修订时,新的问题来自同一段代码(由Brian.W.Kernighnan(ISBN-13:978-8131704943)采用的C编程语言第二版) 代码如下:Can';我无法理解C语言中的单词计数,c,if-statement,boolean,state,getchar,C,If Statement,Boolean,State,Getchar,[已解决] 我最近问了一个关于一些代码的问题,我认为我真的很理解这些代码 但几天后,当我回顾修订时,新的问题来自同一段代码(由Brian.W.Kernighnan(ISBN-13:978-8131704943)采用的C编程语言第二版) 代码如下: #include <stdio.h> #define IN 1 #define OUT 0 main() { int nw, c, state; state = OUT; nw = 0; while (
#include <stdio.h>
#define IN 1
#define OUT 0
main()
{
int nw, c, state;
state = OUT;
nw = 0;
while ((c = getchar()) != EOF)
{
if (c == ' ' || c == '\n' || c == '\t')
state = OUT;
else if (state == OUT)
{
state = IN;
++nw;
}
}
printf("%d", nw);
}
#包括
#在1中定义
#定义出0
main()
{
北卡罗来纳州西北部;
状态=输出;
nw=0;
而((c=getchar())!=EOF)
{
如果(c=''| | c='\n'| | c='\t')
状态=输出;
else if(state==OUT)
{
状态=IN;
++西北;
}
}
printf(“%d”,西北);
}
所以我做了一些随机测试,发现了一些我无法解释或理解的东西:
state=OUT
在nw=0之后
state=OUT和nw=0的顺序时,它总是返回等于0的答案,为什么会这样??
我知道顺序在C语言中很重要,但为什么只有这个特定的顺序
2.if else stament的简要含义是什么
3.Acc。在这本书中,state变量被定义为定义getchar是否在一个单词中,但我不明白state变量究竟是如何做到的
4.作者如何使用等价和平等??
我注意到当他在做一个条件时,他使用等价,否则使用相等,对吗
谢谢您……以下是您发布的代码和一些评论:
#include <stdio.h>
#define IN 1 // in the code below, whenever you see IN, it's replaced by 1
#define OUT 0 // same as IN, but for 0
int main(void) {
int nw, c, state; // nw is the number of the words
// c stands for the character we get from input and state is the state that are
// currently.
// init state as OUT
state = OUT;
// init counter to 0
nw = 0;
// while user doesn't give EOF
while ((c = getchar()) != EOF) {
// we found whitespace, newline or tab
if (c == ' ' || c == '\n' || c == '\t')
state = OUT; // put state as OUT,
// so that we do not count them as words
else if (state == OUT) { // enter here only if state is OUT
// Now, we see that we found a letter/number, which means that a word
// was typed by the user.
// set state as IN, so that we remember that we are eating the characters of
// the word given (in the next loops)
state = IN;
// increase the counter of the words
++nw;
}
}
// print the number of words received by the user
printf("%d", nw);
return 0;
}
当然,输出(正如您应该从上面的评论中猜到的)是:
2
让我们为inputsam
一步一步地运行(有趣的)代码:
// input: "sam" (without the quotes)
while ((c = getchar()) != EOF) { // eat first character, i.e. 's'
/****** 1st execution of the loop ********************/
// 's' is not going into this if
if (c == ' ' || c == '\n' || c == '\t')
state = OUT;
// first time we execute the loop so state is OUT,
// thus we enter the loop
else if (state == OUT) {
// set state as IN
state = IN;
// increase the counter of the words
++nw;
}
/****************************************************/
/****** 2nd execution of the loop ********************/
// while loop's getchar() gives as 'a'
// 'a' is not going into this if
if (c == ' ' || c == '\n' || c == '\t')
state = OUT;
// state is IN, so we don't go into this loop
else if (state == OUT) {
state = IN;
++nw;
}
/****************************************************/
/****** 3rd execution of the loop ********************/
// same as 2nd, but for 'm'
/****************************************************/
/****** 4rth execution of the loop ********************/
// while loop's getchar() gives as '\n'
// '\n' is going into this if (the c == '\n' is true
if (c == ' ' || c == '\n' || c == '\t')
state = OUT; // state sets to OUT
// we are not going into this if else, since we already entered the above if!
else if (state == OUT) {
state = IN;
++nw;
}
/****************************************************/
/****** 5th execution of the loop ********************/
// while loop's getchar() gives as 'd' (first letter of dad)
// 'd' is not going into this if
if (c == ' ' || c == '\n' || c == '\t')
state = OUT;
// state is OUT, so we go into this loop
else if (state == OUT) {
// set state as IN
state = IN;
// increase counter
++nw;
}
/****************************************************/
// and so on :)
}
首先确保您理解这一点,然后回答您自己的问题。:)
此外,我们称之为代码,而不是脚本。不要急于进入脚本。以下是您发布的代码和一些注释:
#include <stdio.h>
#define IN 1 // in the code below, whenever you see IN, it's replaced by 1
#define OUT 0 // same as IN, but for 0
int main(void) {
int nw, c, state; // nw is the number of the words
// c stands for the character we get from input and state is the state that are
// currently.
// init state as OUT
state = OUT;
// init counter to 0
nw = 0;
// while user doesn't give EOF
while ((c = getchar()) != EOF) {
// we found whitespace, newline or tab
if (c == ' ' || c == '\n' || c == '\t')
state = OUT; // put state as OUT,
// so that we do not count them as words
else if (state == OUT) { // enter here only if state is OUT
// Now, we see that we found a letter/number, which means that a word
// was typed by the user.
// set state as IN, so that we remember that we are eating the characters of
// the word given (in the next loops)
state = IN;
// increase the counter of the words
++nw;
}
}
// print the number of words received by the user
printf("%d", nw);
return 0;
}
当然,输出(正如您应该从上面的评论中猜到的)是:
2
让我们为inputsam
一步一步地运行(有趣的)代码:
// input: "sam" (without the quotes)
while ((c = getchar()) != EOF) { // eat first character, i.e. 's'
/****** 1st execution of the loop ********************/
// 's' is not going into this if
if (c == ' ' || c == '\n' || c == '\t')
state = OUT;
// first time we execute the loop so state is OUT,
// thus we enter the loop
else if (state == OUT) {
// set state as IN
state = IN;
// increase the counter of the words
++nw;
}
/****************************************************/
/****** 2nd execution of the loop ********************/
// while loop's getchar() gives as 'a'
// 'a' is not going into this if
if (c == ' ' || c == '\n' || c == '\t')
state = OUT;
// state is IN, so we don't go into this loop
else if (state == OUT) {
state = IN;
++nw;
}
/****************************************************/
/****** 3rd execution of the loop ********************/
// same as 2nd, but for 'm'
/****************************************************/
/****** 4rth execution of the loop ********************/
// while loop's getchar() gives as '\n'
// '\n' is going into this if (the c == '\n' is true
if (c == ' ' || c == '\n' || c == '\t')
state = OUT; // state sets to OUT
// we are not going into this if else, since we already entered the above if!
else if (state == OUT) {
state = IN;
++nw;
}
/****************************************************/
/****** 5th execution of the loop ********************/
// while loop's getchar() gives as 'd' (first letter of dad)
// 'd' is not going into this if
if (c == ' ' || c == '\n' || c == '\t')
state = OUT;
// state is OUT, so we go into this loop
else if (state == OUT) {
// set state as IN
state = IN;
// increase counter
++nw;
}
/****************************************************/
// and so on :)
}
首先确保您理解这一点,然后回答您自己的问题。:)
此外,我们称之为代码,而不是脚本。不要急于进入脚本。以下是您发布的代码和一些注释:
#include <stdio.h>
#define IN 1 // in the code below, whenever you see IN, it's replaced by 1
#define OUT 0 // same as IN, but for 0
int main(void) {
int nw, c, state; // nw is the number of the words
// c stands for the character we get from input and state is the state that are
// currently.
// init state as OUT
state = OUT;
// init counter to 0
nw = 0;
// while user doesn't give EOF
while ((c = getchar()) != EOF) {
// we found whitespace, newline or tab
if (c == ' ' || c == '\n' || c == '\t')
state = OUT; // put state as OUT,
// so that we do not count them as words
else if (state == OUT) { // enter here only if state is OUT
// Now, we see that we found a letter/number, which means that a word
// was typed by the user.
// set state as IN, so that we remember that we are eating the characters of
// the word given (in the next loops)
state = IN;
// increase the counter of the words
++nw;
}
}
// print the number of words received by the user
printf("%d", nw);
return 0;
}
当然,输出(正如您应该从上面的评论中猜到的)是:
2
让我们为inputsam
一步一步地运行(有趣的)代码:
// input: "sam" (without the quotes)
while ((c = getchar()) != EOF) { // eat first character, i.e. 's'
/****** 1st execution of the loop ********************/
// 's' is not going into this if
if (c == ' ' || c == '\n' || c == '\t')
state = OUT;
// first time we execute the loop so state is OUT,
// thus we enter the loop
else if (state == OUT) {
// set state as IN
state = IN;
// increase the counter of the words
++nw;
}
/****************************************************/
/****** 2nd execution of the loop ********************/
// while loop's getchar() gives as 'a'
// 'a' is not going into this if
if (c == ' ' || c == '\n' || c == '\t')
state = OUT;
// state is IN, so we don't go into this loop
else if (state == OUT) {
state = IN;
++nw;
}
/****************************************************/
/****** 3rd execution of the loop ********************/
// same as 2nd, but for 'm'
/****************************************************/
/****** 4rth execution of the loop ********************/
// while loop's getchar() gives as '\n'
// '\n' is going into this if (the c == '\n' is true
if (c == ' ' || c == '\n' || c == '\t')
state = OUT; // state sets to OUT
// we are not going into this if else, since we already entered the above if!
else if (state == OUT) {
state = IN;
++nw;
}
/****************************************************/
/****** 5th execution of the loop ********************/
// while loop's getchar() gives as 'd' (first letter of dad)
// 'd' is not going into this if
if (c == ' ' || c == '\n' || c == '\t')
state = OUT;
// state is OUT, so we go into this loop
else if (state == OUT) {
// set state as IN
state = IN;
// increase counter
++nw;
}
/****************************************************/
// and so on :)
}
首先确保您理解这一点,然后回答您自己的问题。:)
此外,我们称之为代码,而不是脚本。不要急于进入脚本。以下是您发布的代码和一些注释:
#include <stdio.h>
#define IN 1 // in the code below, whenever you see IN, it's replaced by 1
#define OUT 0 // same as IN, but for 0
int main(void) {
int nw, c, state; // nw is the number of the words
// c stands for the character we get from input and state is the state that are
// currently.
// init state as OUT
state = OUT;
// init counter to 0
nw = 0;
// while user doesn't give EOF
while ((c = getchar()) != EOF) {
// we found whitespace, newline or tab
if (c == ' ' || c == '\n' || c == '\t')
state = OUT; // put state as OUT,
// so that we do not count them as words
else if (state == OUT) { // enter here only if state is OUT
// Now, we see that we found a letter/number, which means that a word
// was typed by the user.
// set state as IN, so that we remember that we are eating the characters of
// the word given (in the next loops)
state = IN;
// increase the counter of the words
++nw;
}
}
// print the number of words received by the user
printf("%d", nw);
return 0;
}
当然,输出(正如您应该从上面的评论中猜到的)是:
2
让我们为inputsam
一步一步地运行(有趣的)代码:
// input: "sam" (without the quotes)
while ((c = getchar()) != EOF) { // eat first character, i.e. 's'
/****** 1st execution of the loop ********************/
// 's' is not going into this if
if (c == ' ' || c == '\n' || c == '\t')
state = OUT;
// first time we execute the loop so state is OUT,
// thus we enter the loop
else if (state == OUT) {
// set state as IN
state = IN;
// increase the counter of the words
++nw;
}
/****************************************************/
/****** 2nd execution of the loop ********************/
// while loop's getchar() gives as 'a'
// 'a' is not going into this if
if (c == ' ' || c == '\n' || c == '\t')
state = OUT;
// state is IN, so we don't go into this loop
else if (state == OUT) {
state = IN;
++nw;
}
/****************************************************/
/****** 3rd execution of the loop ********************/
// same as 2nd, but for 'm'
/****************************************************/
/****** 4rth execution of the loop ********************/
// while loop's getchar() gives as '\n'
// '\n' is going into this if (the c == '\n' is true
if (c == ' ' || c == '\n' || c == '\t')
state = OUT; // state sets to OUT
// we are not going into this if else, since we already entered the above if!
else if (state == OUT) {
state = IN;
++nw;
}
/****************************************************/
/****** 5th execution of the loop ********************/
// while loop's getchar() gives as 'd' (first letter of dad)
// 'd' is not going into this if
if (c == ' ' || c == '\n' || c == '\t')
state = OUT;
// state is OUT, so we go into this loop
else if (state == OUT) {
// set state as IN
state = IN;
// increase counter
++nw;
}
/****************************************************/
// and so on :)
}
首先确保您理解这一点,然后回答您自己的问题。:)
此外,我们称之为代码,而不是脚本。不要急于进入脚本。我认为您在这里复制的代码不正确。状态==OUT如果(c==…
应该使用=
而不是==
,则后面的代码>行。此问题似乎与主题无关,因为更正键入可以解决问题。很抱歉,键入错误,(已更正的脚本)但是我真的很想知道这个脚本,请不要把它从主题上划掉……不是答案,但是把一个C源文件称为脚本是相当混乱的……还有,你确定你的问题的第1部分在编辑后仍然有效吗?请不要因为OP的语言问题而抨击他。不是每个人都能说完美的英语。我认为你复制了代码此处错误。if(c==…
后面的行的状态==OUT;
应该使用=
而不是=
。此问题似乎与主题无关,因为更正错误可以解决问题。抱歉,键入错误,(更正的脚本)但是我真的很想知道这个脚本,请不要把它从主题上划掉……不是答案,但是把一个C源文件称为脚本是相当混乱的……还有,你确定你的问题的第1部分在编辑后仍然有效吗?请不要因为OP的语言问题而抨击他。不是每个人都能说完美的英语。我认为你复制了代码此处错误。if(c==…
后面的行的状态==OUT;
应该使用=
而不是=
。此问题似乎与主题无关,因为更正错误可以解决问题。抱歉,键入错误,(更正的脚本)但是我真的很想知道这个脚本,请不要把它从主题上划掉……不是答案,但是把一个C源文件称为脚本是相当混乱的……还有,你确定你的问题的第1部分在编辑后仍然有效吗?请不要因为OP的语言问题而抨击他。不是每个人都能说完美的英语。我认为你复制了代码此处错误。if(c==…
后面的行的状态==OUT;
应该使用=
而不是=
。此问题似乎与主题无关,因为更正错误可以解决问题。抱歉,键入错误,(更正的脚本)但是我真的很想知道这个脚本,请不要把它从主题上划掉……不是答案,但是把一个C源文件称为脚本是相当令人困惑的……还有,你确定你的问题的第一部分在编辑后仍然有效吗?请不要因为他的语言问题抨击OP。不是每个人都能说一口流利的英语。谢谢,但是你能打电话给我吗l告诉我state=in如何吃掉字符??我也很抱歉造成混乱……@ArchKudo不客气。对我来说,没有问题。:getchar()
是吃掉字符的人。Whenstate