c - 有关 C 编程语言 (K&R) 的问题 - 练习 1-13

标签 c arrays

我是编程新手。

我的 C 编程语言 (K&R) 练习 1-13 的解决方案代码中有 6 个问题。任务是编写一个程序来打印单词长度的直方图。如图所示。

我粘贴了下面的代码以及我所做的注释以帮助我理解。

我的问题位于我有疑问的代码的相应部分旁边。提前致谢!

int main ()
{

int c, i, nc, state;
int len;                      /* length of each bar           */
int maxvalue;                 /* maximum value for wl[]       */
int ovflow;                   /* number of overflow words     */
int wl[MAXWORD];              /* word length counters. Defining an int array with 11 character variables. This is the same as saying int wl[11]           */


state = OUT;
nc = 0;                       /* number of chars in a word    */
ovflow = 0;                   /* number of words              */
  1. 下面这个 for 循环的目的是什么?它会初始化数组吗?
    for (i = 0; i < MAXWORD; ++i)
      wl[i] = 0;


while ((c = getchar()) != EOF) //get a character
{
  if (c == ' ' || c == '\n' || c == '\t') // if c is a blank, new line or tab
  {
      state = OUT;  // we are outside a word. 
  • 下面,我们有(nc > 0)虽然上面声明了 nc,但 nc 是如何声明的?改变了?比如用户如何更改它? nc 的输入在哪里?
  •       if (nc > 0) 
    
              if (nc < MAXWORD) //if the number of characters is less than the maxword length counter.
              {
                  ++wl[nc]; // increase the number of elements/number of characters in the array by 1.  
              }
    
  • ++wl[nc] 中的增量运算符是什么?增加1?是否增加数组wlnc有什么意义?在++wl[nc]

              else
              {
                  ++ovflow; // if nc is greater than the maxword however, increase the overflow by 1
              } 
    
          nc = 0;
      } 
    
  • 上面,nc = 0重置字符数?

  •   // The beginning of a new word
    
      else if (state == OUT) {  //if state is out or better put, if we are not in a word
          state = IN;             // In that case, upon accepting a new character, we are at the start of a new word hence state is in.
          nc = 1;                 // and upon accepting a character, the number of characters is 1.
      }
    
      // inside a word
      else 
      {
          ++nc;                   // if not outside a word or in a word, increase the number of characters by 1
    
      }
    
    }
    
  • 在下面的行中,我们初始化 maxvalue

    maxvalue = 0 ;
    for (i = 1; i < MAXWORD; ++i) //Run a for loop until i is 10. Since MAXWORD length counter is 11.
      {
          if (wl[i] > maxvalue) //if the elements in the array is greater than the max number of elements allowed in the array
                  maxvalue = wl[i]; //the max amount of elements allowed in the array is the same as the number of elements in the array. 
          }
    
  • 我不明白这部分代码。有人可以给我解释一下吗?

  • for (i = 1; i < MAXWORD; ++i)
    {
      printf("%5d - %5d : ", i, wl[i]); //i refers to the word length while wl[i] is the frequency of its occurence.
      if (wl[i] > 0) {
          if ((len = wl[i] * MAXHIST / maxvalue) <= 0)
              len = 1;
    
      }
    
      else
          len = 0;
      while (len > 0) 
      {
          putchar ('*');
          --len;
    
      }
      putchar('\n');
    
    }
    if (ovflow > 0)
      printf("There are %d words >= %d\n", ovflow, MAXWORD);
    

    最佳答案

    1. 更改为else if (state == OUT)

    2. wl 数组中的第 nc 元素递增(array[1] 是数组中的第二个元素,因此第 nc 个元素是实际上是 nc + 1...)

    3. for 循环打印单词计数器(是哪个单词)及其长度。

    如果特定单词的长度大于 0,则 len 获取值 wl[i] * MAXHIST/maxvalue,然后检查 0。如果它等于 0,则 len 变为 1。

    否则:len 变为 1

    在这之后,程序打印 len 次 '*',然后是换行符

    关于c - 有关 C 编程语言 (K&R) 的问题 - 练习 1-13,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38169170/

    相关文章:

    c - 在程序集中正确存储 8 字节值

    c++ - 在 VC++ Express 中包含和访问二进制数据的最干净的方法是什么?

    c - malloc 错误,我无法理解使用具有单独链接的哈希表

    java - 引用数组引发与扫描仪有关的错误

    python - 使用 NumPy 在 Python 中的数组上运行矢量化函数

    java - 如何解决这段代码中的 ArrayIndexOutOfBoundsException?

    c - IP header ,OpenBSD 上 C 中的原始套接字

    c - 系统调用超时?

    arrays - iOS swift : How to find unique members of arrays of different types based on specific attributes

    c# - 将二进制文件或 byte[] 从 c# 传递到 java