gpt4 book ai didi

c - 如何用ncurses绘制直方图?

转载 作者:太空宇宙 更新时间:2023-11-04 03:58:10 24 4
gpt4 key购买 nike

我有一个代码可以从输入中读取、计算字母并绘制直方图作为 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");
}
}
}

最佳答案

使用CDK Histogram来自 Curses 开发工具包。这非常容易; here是演示用法的示例代码(取自 Debian libcdk 包)。

关于c - 如何用ncurses绘制直方图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14547903/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com