Jak stworzyć z tego histogram pionowy - C

0

Mam pytanie. Chciałabym stworzyć, to co jest na wyjściu, bardziej podobne do histogramu. Program wyprowadza histogram długości słów. Dziękuje za wszelkie odpowiedzi ;)

 
#include <stdio.h>

#define IN 1 /* wewnatrz slowa */
#define OUT 0 /* poza slowem */

/* histogram dlugosci slow w danych wejsciowych */

int main()
{
    int c, i, j, state = OUT;
    int wordlength[50]; /* dlugosc slowa po kolei */
    int histogram[50]; /* czestosc wystepowania slowa o podanej liczbie liter */

    for(i = 0; i < 50; ++i){
        wordlength[i] = 0;
        histogram[i] = 0;
    }

    i = 0;

    while((c = getchar()) != EOF){ /* pobieranie slow, zliczanie liter */
        if (c == ' ' || c == '\n' || c == '\t'){
            if (state == IN){
                state = OUT;
                i++; /* zmienia na nastepny indeks bo nastepne slowo */
            }
        }else if (state == OUT){
                state = IN; /* poczatek slowa */
                ++wordlength[i];
        }else
            ++wordlength[i]; /* wewnatrz slowa */
    }
    /*
    for(i = 0; i < 50; ++i)
        printf(" %d", wordlength[i]);

    printf("\n");
    */
    for(i = 0; i < 50; ++i){ /* przerzucanie do nastepnej tablicy czestosci liter */
        for(j = 0; j < 50; ++j){
            if((wordlength[i] == j) && (wordlength[i] != 0))
                ++histogram[j];
        }
    }
    /*
    for(i = 0; i < 50; ++i)
        printf(" %d", histogram[i]);

    printf("\n");
    */
    /* drukowanie histogramu */

    for (i = 1; i < 11; ++i){
        printf("%d", i);

        for(j = histogram[i]; j > 0; --j)
        {
            printf("|");
        }
        printf("\n");
}

    return 0;
}

0
#include <stdio.h>
#include <string.h>

#define MAX_WORD_LEN 10 /* -1 */
#define PRINT_SPACE 2

#define STR_HELPER(x) #x
#define STR(x) STR_HELPER(x)

int getwordlength(){
	int len = 0;
	char c;
	while((c = getchar()) != EOF){
		if(c == ' ' || c == '\n' || c == '\t'){
			if(len) break;
			continue;
		}
		++len;
	}
	return len;
}

void print_pad(char pad, int count){
	while(count--) putchar(pad);
}

void print_histogram_h(const int * histogram, int count){
	int i;
	for(i=1; i<count; ++i){
		printf("%5d: ", i);
		print_pad('|', histogram[i]);
		printf("\n");
	}
}

void print_histogram_v(const int * histogram, int count){
	int i, j;
	int max_count = 0;
	for(i=1; i<count; ++i){
		printf(" %"STR(PRINT_SPACE)"d", i);
		if(histogram[i] > max_count)
			max_count = histogram[i];
	}
	printf("\n");
	
	for(i=0; i<max_count; ++i){
		for(j=1; j<count; ++j)
			printf("%"STR(PRINT_SPACE)"s%c", "", histogram[j] > i ? '#' : ' ');
		printf("\n");
	}
}

int main(){
	int len;
	int histogram[MAX_WORD_LEN];
	memset(histogram, 0, sizeof(histogram));
	
	while(len = getwordlength()){
		if(len < MAX_WORD_LEN)
			++histogram[len];
	}
	
	//print_histogram_h(histogram, MAX_WORD_LEN);
	
	print_histogram_v(histogram, MAX_WORD_LEN);
	return 0;
}

$ make && make test
gcc     main.c   -o main
cat test.txt | ./main
  1  2  3  4  5  6  7  8  9
  #     #     #     #      
  #                 #      
                    #      
                    #     

1 użytkowników online, w tym zalogowanych: 0, gości: 1