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

++ndigit[c-'0'];该如何解决

2012-03-19 
++ndigit[c-0]#include stdio.hvoidmain(){intc,i,nwhite,notherintndigit[10]nwhitenother0for

++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中每个数字出现的次数。

热点排行