Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 需要一些关于如何更整洁地打印直方图的建议吗_C_Io - Fatal编程技术网

C 需要一些关于如何更整洁地打印直方图的建议吗

C 需要一些关于如何更整洁地打印直方图的建议吗,c,io,C,Io,我正在编写一个程序,它将读取输入,然后从K&R-Ex.1.13返回字符计数的直方图 关于如何改进代码有什么建议吗?我是先测试状态处于“状态”还是“退出”重要吗?我注意到在我的示例中,人们首先测试c是空白还是制表符 我想我需要重新查看我的直方图。它并不能真正衡量结果。它只是根据长度绘制一个连字符 我想修改一下,让它更具可读性 //打印输入中单词长度的直方图。 #包括 #在1中定义 #定义出2 #定义最大值99 int main(){ int c;//字符 int countOfLetters=0;

我正在编写一个程序,它将读取输入,然后从K&R-Ex.1.13返回字符计数的直方图

关于如何改进代码有什么建议吗?我是先测试状态处于“状态”还是“退出”重要吗?我注意到在我的示例中,人们首先测试c是空白还是制表符

我想我需要重新查看我的直方图。它并不能真正衡量结果。它只是根据长度绘制一个连字符

我想修改一下,让它更具可读性

//打印输入中单词长度的直方图。
#包括
#在1中定义
#定义出2
#定义最大值99
int main(){
int c;//字符
int countOfLetters=0;
int insideWord=OUT;
长度的整数频率[MAX];
int longestWordCount=0;
int i,j;//计数器
对于(i=0;i最大值){
返回1;
}
++长度频率[字母数];
如果(countOfLetters>=longestWordCount)longestWordCount=countOfLetters;
}
countOfLetters=0;
}
否则{
countOfLetters++;
insideWord=IN;
}
}

对于(i=1;i绝对缩放结果,请查看我的水平缩放直方图

另外,y轴标签也有好处。很难说哪个栏代表哪种字长。我不知道哪个栏代表哪种字长

我在显示直方图之前添加了这段代码,它基本上将每个值减半,这确实会去掉条号标签。你可以找到它

// Iterates and tells us the most frequent word length
int mostFrequent = 0;
for (i = 1; i < MAXWORD; i++)
    if (charCount[i] > mostFrequent)
        mostFrequent = charCount[i];

// If the bar will be too big, cut every value in half
while (mostFrequent > 60) {
    for (i = 1; i < MAXWORD; i++)
        if (charCount[i] > 0) {
            charCount[i] /= 2;
            charCount[i] |= 1;
        }

    // Check again to find the most frequent word length category
    mostFrequent = 0;
    for (i = 1; i < MAXWORD; i++)
        if (charCount[i] > mostFrequent)
            mostFrequent = charCount[i];
}
//迭代并告诉我们最常用的字长
int mostFrequent=0;
对于(i=1;imostFrequent)
mostFrequent=charCount[i];
//如果条太大,将每个值减半
而(mostFrequent>60){
对于(i=1;i0){
字符数[i]/=2;
字符数[i]|=1;
}
//再次检查以查找最常见的字长类别
mostFrequent=0;
对于(i=1;imostFrequent)
mostFrequent=charCount[i];
}
老实说,这些条很难阅读,可能只是使用一行字符,例如█ !

到目前为止,这本书很好,我们实际上是一起读的,而且是在同一页上


干杯

什么是
int\tmain(int-argc,\u-TCHAR*argv[])
意思?我不能编译它,但它被标记为
C
。在访问
字符数
之前,你不需要检查单词长度是否小于
MAXWORD
,你应该这样做。“我如何改进我的代码?”当我摆脱了所有的
\t
唠叨,取而代之的是
\35;包括“stdafx.h”
#include
一起编译,但当我运行它时,它什么也不做。是否应该有某种提示告诉我预期结果?哦,很抱歉,我使用的是visual studio 2013。谢谢。我会尝试一下。如果栏太大,如果频率长度为1怎么办?
// Iterates and tells us the most frequent word length
int mostFrequent = 0;
for (i = 1; i < MAXWORD; i++)
    if (charCount[i] > mostFrequent)
        mostFrequent = charCount[i];

// If the bar will be too big, cut every value in half
while (mostFrequent > 60) {
    for (i = 1; i < MAXWORD; i++)
        if (charCount[i] > 0) {
            charCount[i] /= 2;
            charCount[i] |= 1;
        }

    // Check again to find the most frequent word length category
    mostFrequent = 0;
    for (i = 1; i < MAXWORD; i++)
        if (charCount[i] > mostFrequent)
            mostFrequent = charCount[i];
}