-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1-13.c
62 lines (55 loc) · 1.26 KB
/
1-13.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
/* Exercise 1-13. Write a program to print a histogram of the lengths of words
in its input. It is easy to draw the histogram with the bars horizontal;
a vertical orientation is more challenging. */
#include <stdio.h>
#define IN 1 /* inside a word */
#define OUT 0 /* outside a word */
#define LIMIT 20
int main()
{
int current, c, state;
int i;
float nwords[LIMIT], max;
/* clear variables */
current = max = 0;
state = OUT;
for (i = 0; i < LIMIT; ++i)
nwords[i] = 0;
/* collect data */
while ((c = getchar()) != EOF) {
if (c == ' ' || c == '\n' || c == '\t') {
if (state == IN) {
if (current > LIMIT)
current = LIMIT;
--current;
++nwords[current];
current = 0;
}
state = OUT;
}
else if (state == OUT) {
state = IN;
}
if (state == IN)
++current;
}
/* find maximum */
for (i = 0; i < LIMIT; ++i)
if (nwords[i] > max)
max = nwords[i];
max = max / 66.0;
/* print output */
for (i = 0; i < LIMIT; ++i) {
if (i < LIMIT-1)
printf("%3d: (%5.0f) ", i + 1, nwords[i]);
else
printf(">%d: (%5.0f) ", LIMIT, nwords[i]);
for (nwords[i] = nwords[i] / max; nwords[i] > 1.0; --nwords[i])
putchar('|');
if (nwords[i] > 0.665)
putchar(':');
else if (nwords[i] > 0.332)
putchar('.');
putchar('\n');
}
}