简体   繁体   English

C中的简单垂直直方图

[英]Simple Vertical Histogram in C

The exercise was to print a histogram of the different characters in the input. 练习是在输入中打印不同字符的直方图。 At the bottom is my code where I break the different characters into 3 categories a,b, or other, to extrapolate to more categories of characters if the code is successful. 在代码的底部,我将不同的字符分为a,b或其他3类,如果代码成功,则可以推断出更多的字符类别。 But it doesn't have the output I want, for example, for input ab cd I expect 但是它没有我想要的输出,例如,我希望输入ab cd

  x
xxx
---
abo
  t
  h
  e
  r

but all I get is 但我得到的只是

---
abo
  t
  h
  e
  r

Here's my attempt: 这是我的尝试:

#include<stdio.h>

main()
{
int i,c,b, max;
int array[3] = {0,0,0};


while( (c = getchar()) != EOF){
    if(c== 'a')
    ++array[0];
    else if(c== 'b')
    ++array[1];
    else if(c=='\n' ||c=='\t' || c==' '){}
    else ++array[2];
    }

array[0]=max;
i=0;

 while(i<3){
    if(max>=array[i])
        {}
    else array[i]=max;

    ++i;
 }

i=0;
max=b;
while(i<b){
    if(array[0]>=max){
        putchar('x');}
    else putchar(' ');


    if(array[1]>=max) {
        putchar('x');}
    else putchar(' ');


    if (array[2]>=max){
        putchar('x\n');}
    else putchar(' \n');
    --max;
    ++i;


    }

printf("---\nabo\n  t\n  h\n  e\n  r");


}

There's a good bit wrong with your code. 您的代码有很多错误。

  1. Never do - main() 永不做main()

    You should be using int main(void){...} see this answer . 您应该使用int main(void){...} 查看此答案

  2. You're hard coding numbers into your code. 您正在将数字硬编码到代码中。 Provide constant variables instead. 请提供常量变量。 There are logical exceptions to this but numbers generally have meaning so state it. 对此有逻辑上的例外,但是数字通常具有含义,因此请声明。
    unsigned const int LETTERS = 3;

  3. You take the time to increment array[0] counting the occurrences of 'a' , but then assign it the value of max directly afterwards without using that value or storing it somewhere else. 您花时间使array[0]递增,以计数'a'的出现,但之后直接为其分配max的值,而无需使用该值或将其存储在其他位置。

  4. You assign array[0] after the first while(){...} but max has not been initialized, so it's "garbage". 您在第一个while(){...}之后分配了array[0] ,但是max尚未初始化,因此它是“垃圾”。

  5. Here - if(max>=array[i]){} you don't do anything in the body?? 在这里- if(max>=array[i]){}您在体内什么都不做?

  6. You assign max the value of b - again "garbage"! 您将max的值赋给b 再次 “垃圾”!

  7. You should return ...; 您应该return ...; from your main function. 从您的main功能。 You'll have to see for yourself the options you have there. 您必须亲自查看那里的选项。 Note: If you follow "1." 注意:如果您遵循“ 1”。 you'll have no choice but to follow this one. 您别无选择,只能遵循这一步。

  8. Your formatting, if not in your "real" code, in the post is less than desirable (ie; it's not very easily read. 帖子中的格式设置(如果没有在“真实”代码中使用)则不太理想(即,它不太容易阅读)。

Fix those things and you'll probably fix the problem. 解决这些问题,您可能会解决问题。

Change 更改

  • array[0] = max to max = array[0] (otherwise max will take some garbage value) array[0] = maxmax = array[0] (否则max会占用一些垃圾值)

  • max = b to b = max (otherwise b will take some garbage value) max = bb = max (否则b会取一些垃圾值)

  • putchar('x\\n') to { putchar('x'); putchar('\\n')} putchar('x\\n'){ putchar('x'); putchar('\\n')} { putchar('x'); putchar('\\n')} . { putchar('x'); putchar('\\n')}
    (It is important to note here, that putchar() prints only one character at once but you used putchar('x\\n') to print two characters x and \\n together!) (在这里需要注意的重要一点是, putchar()一次只打印一个字符,但是您使用putchar('x\\n')一起打印两个字符x\\n !)

then your program will give desired output. 那么您的程序将提供所需的输出。
Here is your working code with changes mentioned above: 这是上面提到的更改的工作代码:

#include<stdio.h>

int main()
{
    int i,c,b, max;
    int array[3] = {0,0,0};


    while( (c = getchar()) != EOF){
       if(c == 'a')
            ++array[0];
       else if(c == 'b')
            ++array[1];
       else if(c =='\n' || c =='\t' || c == ' ')
            ;
       else ++array[2];
    }

 max = array[0];
 i=0;

 while(i<3){
    if(max < array[i])
        max = array[i];
i++;        
}

i=0;
b = max;
while(i < b){
    if(array[0] >= max)
        putchar('x');
    else 
        putchar(' ');


    if(array[1] >= max)
        putchar('x');
    else 
        putchar(' ');


    if (array[2] >= max)
    {
        putchar('x');
        putchar('\n');
    }
    else 
        putchar('\n');
    --max;
     ++i;


  }

printf("---\nabo\n  t\n  h\n  e\n  r");

return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM