首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

简单的输出有关问题

2012-03-04 
简单的输出问题#includestdio.hvoid main(){int state,nl,nc,nw,cnlncnw0state0while((cgetchar

简单的输出问题
#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

热点排行
Bad Request.