码迷,mamicode.com
首页 > 编程语言 > 详细

利用数组统计输出的数值

时间:2015-01-30 15:19:58      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:

#include <stdio.h>
#include <iostream>
int main(void){
    int c,i,nwhite,nother;
    int ndigit[10];
    nwhite=nother=0;
    
    for(i=0;i<10;++i){
       ndigit[i]=0;
       printf("%d ",ndigit[i]);
    }  
    printf("\n");
    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);
    system("pause");
    return 0;
    }

其中if(c>=‘0‘ && c<=‘9‘)
          ++ndigit[c-‘0‘];
对这两条语句不太理解,当输入为:000 
                                890  时,输出结果如下:
digits=1 0 0 0 0 0 0 0 1 1,white space=2,other=3 

利用数组统计输出的数值

标签:

原文地址:http://www.cnblogs.com/ert79846/p/4262091.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!