【问题标题】:How to draw a histogram with ncurses?如何用ncurses绘制直方图?
【发布时间】:2013-01-10 22:58:33
【问题描述】:

我有一个代码,可以从输入中读取、计算字母并绘制直方图作为 ASCII 艺术。我想做同样的事情,但使用 ncurses。怎么做?

#include <stdio.h>
int main(void) {
  int c, i, j;
  int chars[256];
  // a counter for every character in the ASCII set
  for (i = 0; i < 256; ++i) {
    chars[i] = 0;
  }
  // check each input and increment the relative element
  while ((c = getchar()) != '0') {
    ++chars[c];
  }
  // print only those characters that were received
  for (i = 0; i < 256; ++i) {
    // go through every element in chars
    if (chars[i] > 0) {
      // print headers
      if (i == ' ')
        printf(" Space: ");
      else if (i == '\n')
        printf("    \\n: ");
      else if (i == '\t')
        printf("   tab: ");
      else
        printf("%6c: ", i);
      for (j = 0; j < chars[i]; ++j)
        // print a # for every tally of each element; chars[i] is the tally
        putchar('#');
        // and we need to go through each from 0 to the final tally of that element
      printf("\n");
    }
  }
}

【问题讨论】:

  • 旁注:ASCII 范围是 0..127
  • 你想从诅咒中得到什么?您可以使用相同的格式,尽管窗口中可能有太多行无法容纳。直方图的条形可以绘制为字符或线条。

标签: c histogram ncurses


【解决方案1】:

使用 Curses 开发工具包中的 CDK Histogram。超级简单; here 是演示用法的示例代码(取自 Debian libcdk 包)。

【讨论】:

  • 呃,这个例子展示了如何绘制 3 个独立的条。 CDK 直方图并不是真正的直方图。即跨越分箱间隔的分布图。它是一种基于值绘制条形的机制,但不执行任何“分箱”或分配逻辑。它甚至不是具有多个值的条形图。它只是一个栏小部件。
猜你喜欢
  • 2015-08-26
  • 1970-01-01
  • 2020-12-02
  • 1970-01-01
  • 2012-04-13
  • 2019-03-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多