读书人

简单的输出有关问题

发布时间: 2012-03-04 11:13:33 作者: rapoo

简单的输出问题
#include<stdio.h>
void main()
{int state,nl,nc,nw,c;
nl=nc=nw=0;
state=0;
while((c=getchar())!=EOF)
{ nc++; /*统计字符个数*/
if(c=='\n')
nl++; /*统计行数*/
if(c==' '||c=='\n'||c=='\t')
state=0; /*判断是否在字母里面*/
else if(state==0)
{ state=1;
nw++; /*统计单词个数*/
}
}
printf("%d\t%d\t%d\n",nl,nc,nw);
}
这个程序为什么不能输出结果???

[解决办法]

C/C++ code
#include<stdio.h>void main(){    int state,nl,nc,nw,c;    nl=nc=nw=0;    state=0;    while((c=getchar())!='\n')    {         nc++; /*统计字符个数*/        if(c=='\n')            nl++; /*统计行数*/        if(c==' '||c=='\n'||c=='\t')            state=0; /*判断是否在字母里面*/        else if(state==0)        {             state=1;            nw++; /*统计单词个数*/        }    }    printf("%d\t%d\t%d\n",nl,nc,nw);}
[解决办法]
EOF 定义为-1
'\n' 定义为10

读书人网 >C语言

热点推荐