控制台不显示直方图

标签 c

我运行了以下代码,但控制台上没有任何结果......

#include <stdio.h>

#define MAXWORDLEN 10

int main(void)
{
  int c;
  int inspace = 0;
  long lengtharr[MAXWORDLEN + 1];
  int wordlen = 0;

  int firstletter = 1;
  long thisval = 0;
  long maxval = 0;
  int thisidx = 0;
  int done = 0;

  for(thisidx = 0; thisidx <= MAXWORDLEN; thisidx++)
  {
    lengtharr[thisidx] = 0;
  }

  while(done == 0)
  {
    c = getchar();

    if(c == ' ' || c == '\t' || c == '\n' || c == EOF)
    {
      if(inspace == 0)
      {
        firstletter = 0;
        inspace = 1;

        if(wordlen <= MAXWORDLEN)
        {
          if(wordlen > 0)
          {
            thisval = ++lengtharr[wordlen - 1];
            if(thisval > maxval)
            {
              maxval = thisval;
            }
          }
        }
        else
        {
          thisval = ++lengtharr[MAXWORDLEN];
          if(thisval > maxval)
          {
            maxval = thisval;
          }
        }
      }
      if(c == EOF)
      {
        done = 1;
      }
    }
    else
    {
      if(inspace == 1 || firstletter == 1)
      {
        wordlen = 0;
        firstletter = 0;
        inspace = 0;
      }
      ++wordlen;
    }
  }

  for(thisval = maxval; thisval > 0; thisval--)
  {
    printf("%4d  | ", thisval);
    for(thisidx = 0; thisidx <= MAXWORDLEN; thisidx++)
    {
      if(lengtharr[thisidx] >= thisval)
      {
        printf("*  ");
      }
      else
      {
        printf("   ");
      }
    }
    printf("\n");
  }
  printf("      +");
  for(thisidx = 0; thisidx <= MAXWORDLEN; thisidx++)
  {
    printf("---");
  }
  printf("\n       ");
  for(thisidx = 0; thisidx < MAXWORDLEN; thisidx++)
  {
    printf("%2d ", thisidx + 1);
  }
  printf(">%d\n", MAXWORDLEN);

  return 0;
}

最佳答案

getchar 仍然是一个 int POSIX and C standards 。您是否忘记包含 stdio.h 或包含重新定义它的内容?这个例子对我有用:

#include <stdio.h>
int main()
{
  int c;
  c = getchar();
}

关于控制台不显示直方图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1413725/

相关文章:

c - printf 证明多行输出

c - 如何退出简单的生产者-消费者问题

c - C 中的通用指针和内存分配

c - "warning: ' 结构矩阵 ' declared inside parameter list [enabled by default]"和错误 : conflicting types for 'scanToken'

c - 我的C代码太尴尬了

c - 在 bash 脚本中自动引用 .h 文件

c - 如何按值传递 char ** 指针?

c++ - 由于 memcpy 问题,无法使用 VS 进行优化编译;被视为内在函数

c - 在C中切换变量的值

java - 从 C/C++ 代码通知 java 对象?