C语言统计各个数字、空白字符(空格、制表和换行)及所有其它字符出现的次数,程序正确求解释。高手来!

程序 只运行了一次,图中红圈中程序应该只打印出0的数字个数总和,为什么会打印0-9的个数总和呢,也就是说,打印时它在缓存中应该只运行一次,为什么会运行10次呢?注释是我自己写的,不对请指正,程序和图如下。
/*
1.6数组统计各个数字、空白字符(空格、制表和换行)及所有其它字符出现的次数。
*/
#include <stdio.h>
main()
{
int c,i,nwhite,nother; /*定义整数型内存c(接收1个字符) i(统计数字) nwhite(三种空白字符 白色) nother(其它字符 妈妈)*/
int ndigit[10]; /*定义整数数组ndigit,它的成员共有10个*/

nwhite = nother = 0; /*nwhite和nother内存清零*/
for (i = 0; i < 10; ++i) /* i先初始化清零,以后如果i小于10那么,i就自加1 */
ndigit[i]= 0 ; /* 如果i小于10的情况下,数组[i]就清零 */

while ((c = getchar()) != EOF) /* 以下统计0-9每个数字的个数,如果C接收的字符不是结束符 */
if (c >= '0' && c <= '9') /* 如果C接收的字符包括0至9的数字,0-9它们ASCII值48-57 */
++ndigit[c-'0']; /* 用它们的字符值减去0的ASCII值48,例如1的ASCII值49-48=1,也就是说让ndigit[1]自加1。 */
else if ( c == ' ' || c == '\n' || c =='\t') /* ,以下统计空白字符的个数。否则如果 接收到的是空格或换行或制表符 */
++nwhite; /* 那么nwhite就自加1 */
else /* 以下统计其它字符的个数,那么nother就自加1 */
++nother;

printf("digist ="); /* 打印整理的结果是= */
for (i = 0;i < 10;++i) /* 如果i小于10,就自加1 */
printf(" %d",ndigit[i]); /* 在小于10的情况下就打印数组ndigit[i]的值*/

printf(",white space = %d,other = %d\n",
nwhite,nother); /* 打印十进制的空格和换行符*/

}

#include <stdio.h>
#include <Windows.h>
int main()
{
    int c, i, numWhite = 0, numOther = 0;
    //int ndigit[10] = {0};
    int numDigit = 0;

    while ((c = getchar()) != EOF){
        if (c >= '0' && c <= '9')
            ++numDigit;//++ndigit[c-'0'];
        else if (c == ' ' || c == '\n' || c =='\t')
            ++numWhite;
        else
            ++numOther;
    }
    //printf("digist =");
    //for (i = 0;i < 10;++i)
        printf("numDigit = %d, ", numDigit);//printf(" %d",ndigit[i]);

    printf("white space = %d, other = %d\n", numWhite, numOther);
    Sleep(3000);
    return 0;
}

按照你的意思,在你的源代码上改了下,可以看下,有疑问咱们再讨论。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-08-05
就你的题目统计各字符个数而言,那么你的程序中,while模块的范围应该到else,缺‘{}’,给while这个模块加上‘{}’本回答被网友采纳
第2个回答  推荐于2017-10-17
别逗了。。。。。。。。。。追问

自学C语言,网上没找到答案,C语言吧的大神根本不鸟我们新手,C新手吧根本没有人。。。我又写注释又查资料又做图容易吗?您还逗我,我整个人都不好了。。。。。。

追答

for (i = 0;i < 10;++i) printf(" %d",ndigit[i]);

本回答被提问者采纳
相似回答