-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwlh.c
98 lines (87 loc) · 2.29 KB
/
wlh.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
95
96
97
/* print a histogram of the lengths of words in its inputs */
#include <stdio.h>
#define IN 1 // inside a word
#define OUT 0 // outside a word
#define ML 20 // max length
#define STYLE '_' // histogram style
#define DIST 4 // group distance of histogram
//
// HOW TO PARAMETERIZE THE FROMAT STRING IN `printf`??????????????????????????????????????????
//#define PRINTF0(dist) "%%-%dd", dist
//#define PRINTF(printf0, n) printf(printf0, #n)
int main() {
int lw[ML];
int state = OUT;
int c;
int l = 0;
int i, j, k;
int max;
int lw_[ML];
for (i = 0; i < ML; ++i)
lw[i] = 0;
/* count lengths of words in inputs */
printf("Inputs:\n");
while ((c = getchar()) != EOF) {
if (c == ' ' || c == '\n' || c == '\t') {
if (state == IN) {
if (l >= ML) {
++lw[0]; // count the number of word that the length of which exceed ML
l = 0;
}
else {
++lw[l];
l = 0;
}
}
state = OUT;
}
else if (state == OUT) {
++l;
state = IN;
}
else if (state == IN)
++l;
}
/* find the length that has the max frequency of occurrence in inputs */
max = 0;
for (i = 1; i < ML; ++i) {
if (lw[i] > lw[max]) {
max = i;
}
}
/* print histogram */
for (i = 0; i < ML; ++i) {
lw_[i] = lw[i];
}
for (i = lw[max]; i > 0; --i) {
for (j = 0; j < ML; ++j) {
if (lw_[j] == i) {
putchar(STYLE);
for (k = 1; k < DIST; ++k){
putchar(' ');
}
--lw_[j];
}
else {
for (k = 0; k < DIST; ++k){
putchar(' ');
}
}
}
putchar('\n');
}
/* following code not good enough */
for (i = 0; i < DIST * ML; ++i) {
putchar('-');
}
putchar('>');
putchar('\n');
for (i = 0; i < ML; ++i) {
printf("%-4d", lw[i]); // waiting for better solution
}
printf("count\n");
for (i = 0; i < ML; ++i) {
printf("%-4d", i);
}
printf("length\n");
}