2 qq 40173922 qq_40173922 于 2017.09.18 23:27 提问

C语言求单词长度直方图

请前辈们帮我看看是哪里出错了呀,我的代码得不到我想要的东西。

 #include <stdio.h>
#define OUT 0
#define IN 1
#define ML 11 /*max world length*/
#define HL 20 /*avoid overfloor*/
int main()
{
    int nc = 0;
    int c;
    int overfloor = 0;
    int state = OUT;
    int a[ML];
    while ((c = getchar()) != EOF)
    {

            if (c == ' ' || c == '\t' || c == '\n')
            {
                state = OUT;

                if (nc < ML)
                    ++a[nc];
                else ++overfloor;

                nc = 0;
            }
            else if (state == OUT)
            {
                state = IN;
                nc = 1;

            }
            else if (state == IN)
                ++nc;



         int i;
         for (i = 1; i <= ML; i++)
         {
             int len = a[i];
             if (len <= HL)
                 while (len > 0)
             {
                 putchar('*');
                 len--;
             }
             else
             {
                 printf("溢出的单词个数为%d\n", overfloor);
             }
             printf("\n");
         }

    }
}

1个回答

lz0905
lz0905   2017.09.19 09:54

if (c == ' ' || c == '\t' || c == '\n')
{
state = OUT;

            if (nc < ML)
                ++a[nc];       
                                    ...
                                }
                                ++a[nc]是什么意思
qq_40173922
qq_40173922 单词长度为nc所对应的数组a[nc]加一
2 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片