Chinaunix

标题: 编写一个程序,打印输入中单词长度的直方图 [打印本页]

作者: lyf362345    时间: 2013-12-26 15:52
标题: 编写一个程序,打印输入中单词长度的直方图
k&r 里的练习题 1-13,  我自己写的:
  1. #include <stdio.h>

  2. #define MAXLEN 15

  3. int main()
  4. {
  5.         int c, len, ovflow;
  6.         int chars[MAXLEN];

  7.         len = ovflow = 0;

  8.         for (int i = 0; i < MAXLEN; ++i) {
  9.                 chars[i] = 0;
  10.         }

  11.     while ((c = getchar()) != EOF) {
  12.             if (c == ' ' || c == '\n' || c == '\t') {
  13.                     ++chars[len];
  14.                     len = 0;
  15.             }
  16.             else {
  17.                     ++len;

  18.                     if (len > MAXLEN) {
  19.                             ++ovflow;
  20.                             len = 0;
  21.                     }
  22.             }
  23.     }

  24.     for (int i = 1; i < MAXLEN; ++i) {
  25.             printf("%02d | ", i);
  26.             for (int j = 0; j < chars[i]; ++j) {
  27.                     printf("*");
  28.             }
  29.             printf("\n");
  30.     }

  31.     printf("OF | %d\n", ovflow);
  32. }
复制代码
完事我看了下习题解答, 他是这样的写的:
  1. #define MAXHIST 15
  2. #define MAXWORD 11
  3. #define IN 1
  4. #define OUT 0

  5. int main()
  6. {
  7.     int c, nc, state, len, ovflow, maxvalue;
  8.     int wl[MAXWORD];

  9.     // nc 单词长度
  10.     // ovflow 大于最大字符长度(MAXWORD)的单词书
  11.     // maxvalue wl下最长的一个
  12.     nc = len = ovflow = maxvalue =0;
  13.     state = OUT;

  14.     for (int i = 0; i < MAXWORD; ++i) {
  15.         wl[i] = 0;
  16.     }

  17.     // 收集全部输入
  18.     while ((c = getchar()) != EOF) {
  19.         if (c == ' ' | c == '\n' || c == '\t') {
  20.             state = OUT;

  21.             if (nc > 0) {
  22.                 if (nc > MAXWORD) {
  23.                     ++ovflow;
  24.                 }
  25.                 else {
  26.                     ++wl[nc];
  27.                 }
  28.             }

  29.             nc = 0;
  30.         }
  31.         else if (state == IN) {
  32.             state = IN;
  33.             nc = 1;
  34.         }
  35.         else {
  36.             ++nc;
  37.         }
  38.     }

  39.     // 找出最长的单词
  40.     for (int i = 1; i < MAXWORD; ++i) {
  41.         if (wl[i] > maxvalue) {
  42.             maxvalue = wl[i];
  43.         }
  44.     }

  45.     for (int i = 1; i < MAXWORD; ++i) {
  46.         printf("%5d - %5d : ", i, wl[i]);
  47.         if (wl[i] > 0) {
  48.             if ((len = wl[i] * MAXHIST / maxvalue) <= 0) {
  49.                 len = 1;
  50.             }
  51.         }
  52.         else {
  53.             len = 0;
  54.         }

  55.         while (len > 0) {
  56.             putchar('*');
  57.             --len;
  58.         }

  59.         putchar('\n');
  60.     }
  61. }
复制代码
那种合适呢?
作者: 柠檬树庄园    时间: 2016-07-11 17:28
第二个例子,第一行少了一个include
  1. #include <stdio.h>
复制代码
23行有一个typo,应该是
  1.         if (c == ' ' || c == '\n' || c == '\t') {
复制代码





欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2