-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHistogramOfWordLength_exe1_13_page25.c
94 lines (79 loc) · 2.05 KB
/
HistogramOfWordLength_exe1_13_page25.c
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/* print a histogram of the lengths of words in its input, vertical orientation histogram is more challenging than horizontal
* page 25 of The C Programing Language
* by Jean 20170624
*/
#include <stdio.h>
#define MAXLENGTH 10
int maxvalue( int A[], int n );
int main()
{ /* read stdin, calculate every word length, census the accounts of each length, and draw a histogram,
* the x coordinate is the words' length like 1,2,3... 10; the y coordinate is the statistic accounts */
typedef long Y_axis;
typedef int X_axis;
Y_axis wd_freq[ MAXLENGTH ];
X_axis wd_length; // the range of wd_length should be within 0 to MAXLENGTH
int inspace;
int wd_bg;
int c;
int i;
for( i = 0; i < MAXLENGTH + 1 ; ++i) wd_freq[i] = 0;
wd_length = 0;
inspace = 0; wd_bg = 0;
while( (c = getchar() ) != EOF ){
/* read char */
if( c == ' ' || c == '\n' || c == '\t' ){
++wd_freq[0];
wd_bg = 0;
if( inspace == 0 ){
inspace = 1;
if(wd_length >0) ++wd_freq[wd_length];
wd_length = 0;
}else{
;
}//if
}else{
inspace = 0;
if( wd_bg == 0 ){
wd_bg = 1;
++wd_length;
}else{
++wd_length;
}//if
if( wd_length >= MAXLENGTH ) wd_length = MAXLENGTH;
}//if
}//while
/* draw the histogram, x: words length, y: appearance frequency */
int xvalue, yvalue;
for( yvalue = maxvalue( wd_freq, MAXLENGTH ); yvalue > 0; yvalue--){
printf("%8d |", yvalue);
for( xvalue = 0; xvalue <= MAXLENGTH; xvalue++){
if( yvalue > wd_freq[ xvalue ] ){
printf(" ");
}else{
printf(" *");
}
}//for
printf("\n");
}//for
printf(" +");
for( xvalue = 0; xvalue <= MAXLENGTH; xvalue++ ){
printf("--");
} //for
printf("\n ");
for( xvalue = 0; xvalue <= MAXLENGTH; xvalue++ ){
printf("%2d",xvalue);
} //for
printf("\n");
} //main
/* calculate the max number of an array */
int maxvalue( int A[], int n ){
int i, a = 0;
//int max_colums = sizeof( A[] ) / sizeof( int );
for( i=0; i <= n; i++){
if( a < *(A+i)){
a = *(A+i);
} //if
printf( "%d\t%d\n", i, A[i]);
} //for
return a;
} //maxvalue