-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1.13.c
89 lines (80 loc) · 2.07 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
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
#include "stdio.h"
#define STAT_IN 0
#define STAT_OUT 1
#define MAX_WORD_LEN 15
#define MAX_HIST_LEN 30
int main() {
int wl[MAX_WORD_LEN];
int over_flow = 0;
int stat = STAT_OUT;
// init
for (int i = 0; i < MAX_WORD_LEN; i++) {
wl[i] = 0;
}
// calculate
int nc = 0;
char c;
while ((c = getchar()) != EOF) {
if (c == ' ' || c == '\t' || c == '\n') {
if (stat == STAT_IN) {
if (nc > MAX_WORD_LEN) {
over_flow++;
} else {
wl[nc] = wl[nc] + 1;
}
}
stat = STAT_OUT;
nc = 0;
} else if (stat == STAT_OUT) {
nc = 1;
stat = STAT_IN;
} else {
nc++;
}
}
// calculate max_word_len
int max_word_len = 0;
for (int i = 0; i < MAX_WORD_LEN; i++) {
if (max_word_len < wl[i]) {
max_word_len = wl[i];
}
}
// print horizontal histogram
int nstar = 0;
for (int l = 1; l <= MAX_WORD_LEN; l++) {
printf(" %5d:%5d ", l, wl[l-1]);
nstar = wl[l-1] * MAX_HIST_LEN / max_word_len;
for (int i = 0; i < nstar && i < MAX_HIST_LEN; i++) {
printf("*");
}
printf("\n");
}
if (over_flow > 0) {
printf(">%5d:%5d ", MAX_WORD_LEN, over_flow);
nstar = over_flow / max_word_len * MAX_HIST_LEN;
for (int i = 0; i < nstar && i < MAX_HIST_LEN; i++) {
printf("*");
}
printf("\n");
}
printf("\n");
// print vertical histogram
for (int r = MAX_HIST_LEN; r >= 0; r--) {
for (int l = 0; l < MAX_WORD_LEN; l++) {
if (wl[l] * MAX_HIST_LEN > max_word_len * r) {
printf(" * ");
} else {
printf(" ");
}
}
printf("\n");
}
for (int l = 0; l < MAX_WORD_LEN; l++) {
printf("%3d", wl[l]);
}
printf("\n");
for (int l = 0; l < MAX_WORD_LEN; l++) {
printf("%3d", l);
}
printf("\n");
}