【问题标题】:What ways (using stdio) can I print a vertical histogram我可以通过哪些方式(使用 stdio)打印垂直直方图
【发布时间】:2013-06-27 17:23:05
【问题描述】:

我正在做所有的 K&R 练习,我终于能够打印水平直方图了。它看起来也很糟糕,但我会让你判断它。我无法垂直打印其输入中单词长度的直方图。

如何修改我的程序来做到这一点?

问题:编写一个程序来打印单词长度的直方图 在它的输入中。用条形图很容易绘制直方图 水平的;垂直方向更具挑战性。

直方图.c

#include <stdio.h>
#define MAX 10
#define IN 1
#define OUT 0

int main()
{
    int c, len, state;
    int nlength[MAX];
    c = len = 0;
    state = 1;

    for(int i = 0; i < 10; ++i) 
        nlength[i] = 0;

    while ((c = getchar()) != EOF) {
        ++len;
        if (c == ' ' || c == '\n' || c == '\t') {
            --len;
            state = OUT;
        }
            if(state == OUT) {
                if(len != 0 && len <= MAX)
                    ++nlength[len];

            len = 0;
            state = IN;
        }
    }
    for (int i = 0; i <= MAX; ++i) {
        printf("%d ", i);
        for (int a = 0; a < nlength[i]; ++a)
            printf("*");

        printf("\n");
        }
    return 0;
}



OUTPUT:
./histogram < histogram.c
0 
1 *************************************
2 *************************
3 **************
4 ************
5 *****
6 ******
7 ****
8 **
9 *
10 ***

【问题讨论】:

  • 在当前程序中使用printf("%2d ", i);左对齐*s。
  • @meaning-matters 我不知道,谢谢。

标签: c arrays printf histogram


【解决方案1】:

首先你需要知道直方图的高度是最大值。然后打印每一行,然后根据值决定放置*

int h_max = 0;
for (int a = 0; a < MAX; a++) {
  if (h_max <= nlength[a]) h_max = nlength[a];
}

for (int i = h_max; i >= 0; i--) {
    for (int a = 0; a < MAX; ++a) {
      if (nlength[a] > i) {
        printf("*"); 
      } else {
        printf(" ");
      }
    }
    printf("\n");
}

另一种解决方案是水平打印成一个数组,然后按你想要的方向打印数组。

【讨论】:

  • +1。我没有想到你的第二点,我会尝试实施挑战!
【解决方案2】:

让行数等于最高的 bin 值(或 bin 值的一些其他合适的函数)。一次打印一行直方图,对于每一列,根据与该列对应的 bin 值来决定是否在该行的该列中打印*

【讨论】:

  • 我在我最早的 BASIC 程序之一中采用了这种方法。我为自己能够弄清楚这一点而感到太自豪了。 :D
【解决方案3】:
//vertical HISTOGRAM2
#include<stdio.h>
#include<stdlib.h>
int main()
{
int c,i,j,arr[10],height=0;
system("clear");

for(i=0 ; i<10 ; i++)
    arr[i]=0;

 while( ( c=getchar() ) != EOF)
  {
     if(c >= '0' || c <='9')
     ++arr[c-'0'];
     if( arr[c-'0'] > height )
      {
      height = arr[c-'0'];
      } 
  }
printf("\n");
for(j=height ; j>0 ; j--)     // row
 {
  printf("%2d|",j);
  for ( i=0 ; i<=9 ; i++)  // column

    {
    if( j == arr[i] )
     {
     printf(" *|");
     arr[i]--;
     }
     else
         printf("  |");
    }

    printf("\n");
}
  printf("  |");
 for ( i=0 ; i<=9 ; i++)
     printf(" %d|",i);
     printf("\n  ------------DIGITS-------------");
     printf("\n");
return(0);
} 

【讨论】:

    【解决方案4】:
    #include <stdio.h>
    #define MAX 10
    #define IN 1
    #define OUT 0
    
    int main(void){
        int c, len, state;
        int nlength[MAX];
    
        c = len = 0;
        state = IN;
    
        for(int i = 0; i < MAX; ++i) 
            nlength[i] = 0;
    
        while ((c = getchar()) != EOF) {
            ++len;
            if (c == ' ' || c == '\n' || c == '\t') {
                --len;
                state = OUT;
            }
            if(state == OUT) {
                if(len != 0 && len <= MAX)
                    ++nlength[len-1];
    
                len = 0;
                state = IN;
            }
        }
        int max = 0;
        //horizontal
        for (int i = 0; i < MAX; ++i) {
            if(max < nlength[i]) max = nlength[i];
            printf("%2d ", i+1);
            for (int a = 0; a < nlength[i]; ++a)
                printf("*");
    
            printf("\n");
        }
        printf("\n");
        //vertical
        for (int i = max; i > 0; --i){
            for (int j = 0; j < MAX; ++j)
                if(nlength[j]>=i)
                    printf("%c ", '*');
                else
                    printf("%c ", ' ');
            printf("\n");
        }
        for(int i=1;i<=MAX;++i)
            printf("%-2d", i);
    
        return 0;
    }
    

    【讨论】:

      【解决方案5】:
      #include <stdio.h>
      
      #define MAX_WORDS   10
      
      //Prints a histogram of the lengths of words in its input
      int main()
      {
          int c, numberOfCharacters, numberOfWords, wordLengthsIndex, largestWordLength;
          c = numberOfCharacters = numberOfWords = wordLengthsIndex = largestWordLength = 0;
          int wordLengths[MAX_WORDS];
      
          //Read the words
          while ( ((c = getchar()) != EOF) && numberOfWords < MAX_WORDS)
          {
              if (c == ' ' || c == '\n' || c == '\t') //no longer in a word
              {
                  if (numberOfCharacters > largestWordLength)
                  {
                      largestWordLength = numberOfCharacters;
                  }
                  wordLengths[wordLengthsIndex] = numberOfCharacters;
                  numberOfCharacters = 0; //reset for the next word
                  numberOfWords++;
                  wordLengthsIndex++;
              }
              else //treat anything else as a character in a word
              {
                  numberOfCharacters++;
              }
              
          }
      
          //Print the histogram horizontally
          for (int numberOfWordsIndex = 0; numberOfWordsIndex < numberOfWords; ++numberOfWordsIndex)
          {
              for (int numberOfCharactersIndex = 0; numberOfCharactersIndex < wordLengths[numberOfWordsIndex]; ++numberOfCharactersIndex)
              {
                  putchar('*');
              }
              putchar('\n');
              putchar('\n');
          }
      
          //Print the histogram vertically
         for (int character = largestWordLength; character > 0; --character) //cycle each row
         {
             for (int word = 0; word < numberOfWords; ++word) //cycle each column
             {
                 if (wordLengths[word] >= character) //this particular word length falls within the current row
                 {
                     putchar('*');
                     putchar('\t');
                 }
                 else
                 {
                     putchar(' ');
                     putchar('\t');
                 }           
             }
             putchar ('\n');
         }
      
          return 0;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-12-08
        • 1970-01-01
        • 2016-02-03
        • 2020-11-02
        • 1970-01-01
        • 2011-04-15
        • 2021-03-17
        • 1970-01-01
        相关资源
        最近更新 更多