1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| #include <stdio.h>
#define MAX_WORD_LENGHT 20
int main(void)
{
int iChar, iWordLenght, i, j, iMaxValue, bDone;
int iWords[(MAX_WORD_LENGHT+1)] = {0}; /* iWords[0] est utilisé pour les mots > 20 */
int iColumn[(MAX_WORD_LENGHT+1)] = {0};
/* Cette partie est utilisée pour l'entrée de données */
iWordLenght = 0;
bDone = 0;
while(!bDone) {
iChar = getchar();
if(iChar == ' ' || iChar == '\t' || iChar == '\n' || iChar == EOF) {
if(iWordLenght > 0 && iWordLenght <= MAX_WORD_LENGHT)
iWords[iWordLenght]++;
if(iWordLenght > 0 && iWordLenght > MAX_WORD_LENGHT)
iWords[0]++;
if(iChar == EOF)
bDone = 1;
iWordLenght = 0;
}
else
iWordLenght++;
}
/* Cette partie est utilisée pour l'affichage graphique de l'histogramme */
iMaxValue = 0;
for(i = 0; i <= MAX_WORD_LENGHT; i++)
if(iWords[i] > iMaxValue)
iMaxValue = iWords[i];
printf("\n");
for(i = iMaxValue; i > 0; i--) {
printf("%2d |", i);
for(j = 0; j <= MAX_WORD_LENGHT; j++)
if(iWords[j] >= i)
iColumn[j] = '*';
else
iColumn[j] = ' ';
for(j = 1; j <= MAX_WORD_LENGHT; j++)
{
printf("%2c ", iColumn[j]);
}
printf("%2c|\n", iColumn[0]);
}
printf(" +--------------------------------------------------------------+\n");
printf(" |01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 ++|\n");
return 0;
} |