刚开始学C,有很菜的问题

luoxix 2001-11-05 09:53:58
#include <stdio.h>
#define in 1
#define out 0

mian()
{
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;
}
}
orintf("%d %d %d\n", nl, nw, nc);
}

在 the c programming language 第二版中有一个例子
不知道是不是我的理解有问题,我怎么看不懂呢
是每打一个字符nl自加,还是没打一个单词才自加
state的作用在哪啊?
...全文
48 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
heimeng 2001-11-06
  • 打赏
  • 举报
回复
..
liner1 2001-11-05
  • 打赏
  • 举报
回复
这个例子很简单,nl,nw,nc,
nl计算行数,
nw计算单词数,
nc计算字符数。
state:每碰到空格,回车,和TAB键,就表示一个单词结束。
love_snoopy 2001-11-05
  • 打赏
  • 举报
回复
恩!同意!
richielee 2001-11-05
  • 打赏
  • 举报
回复
state表示的是 当前是在单词内还是单词外
最初的状态是state=out,当遇到一个非' ', '\n', '\t'的字符时
状态变成state=in
如果state==in时,遇到' ', '\n', '\t'字符,单词数加一

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧