求大神指点,这两个代码有何不同。
同样是计数器,计算输入的字符、单词、行数。求大神指点不同之处,拜谢拜谢!这是书上的:
/* Note:Your choice is C IDE */
#include "stdio.h"
#define IN 1
#define OUT 0
int main()
{
int c,nl,nw,nc,state;
/*nc为字符数,nl为行数,nw为单词数*/
state = OUT;
nl = nw = nc = 0;
while((c = getchar())!=EOF)
{
++nc;
if( c == '\n')
++nl;
if( c == ' ' || c == '\t' || c == '\n')
state = OUT;
else if( state == OUT )
{
state = IN;
++nw;
}
}
printf('%d %d %d\n",nl,nw,nc);
}
这是我自己写的:
/* Note:Your choice is C IDE */
#include "stdio.h"
int main()
{
int cc=1,cr=1,cw=1,a;
/*cc为字符数,cr为行数,cw为单词数*/
while((a = getchar())!=EOF)
{
cc++;
if(a == '\n' )
cr++;
if(a == '\n' || a == '\t' || a == '\ ')
cw++;
}
printf("cc=%d,cr=%d,cw=%d",cc,cr,cw);
}