主题:单词计数程序求助
代码如下
#include <stdio.h>
#define IN 1 //在单词内
#define OUT 0 //在单词外
int main (void)
{
int c,nl,nw,nc,state;
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("%d %d %d\n",nl,nw,nc);
getchar();
}
答案说在输入单字符单词时,结果应该为“1 1 2”,在输入两个同行单字符时(如“a a”)结果应该为“2 2 4”。可我在VS2010中输入进去后,结果分别为“0 1 2”和“0 1 4”,这是为什么?
#include <stdio.h>
#define IN 1 //在单词内
#define OUT 0 //在单词外
int main (void)
{
int c,nl,nw,nc,state;
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("%d %d %d\n",nl,nw,nc);
getchar();
}
答案说在输入单字符单词时,结果应该为“1 1 2”,在输入两个同行单字符时(如“a a”)结果应该为“2 2 4”。可我在VS2010中输入进去后,结果分别为“0 1 2”和“0 1 4”,这是为什么?