++ndigit[c-'0'];
#include <stdio.h>
void main()
{
int c,i,nwhite,nother;
int ndigit[10];
nwhite=nother=0;
for(i=0;i <10;++i)
ndigit[i]=0;
while((c=getchar())!=EOF)
if(c> = '0 ' && c <= '9 ')
++ndigit[c- '0 '];/*这个地方是做什么用的。为什么可以那样写呢?*/
else if(c== ' '||c== '\n '||c== '\t ')
++nwhite;
else
++nother;
printf( "digits= ");
for(i=0;i <10;++i)
printf( "%d ",ndigit[i]);
printf( ", white space=%d,other=%d\n ",nwhite,nother);
}
谢谢您了~
[解决办法]
这是K&R里的一道程序吧!
++ndigit[c- '0 '];/*这个地方是做什么用的。为什么可以那样写呢?*/
这样来看 ++ndigit[c- '0 '] :首先计算ndigit[c- '0 '](运算符的优先级,[] 比 ++高)得到一个下标值,比如c是‘5’得到的就是ndigit[5]这个元素,然后执行++ndigit[c- '0 '],也就是使这个元素值加1。在这起到了统计5这个数出现的次数。
++ndigit[c- '0 ']是用来统计0-9中每个数字出现的次数。