9 |
|
|
10 |
|
#include <stdio.h> |
11 |
|
#include <stdlib.h> |
12 |
+ |
#include <string.h> |
13 |
|
#include <math.h> |
14 |
|
#include <ctype.h> |
15 |
|
|
21 |
|
char *progname; |
22 |
|
|
23 |
|
int cumulative = 0; |
24 |
+ |
int percentile = 0; |
25 |
|
|
26 |
+ |
long outrange[MAXCOL][2]; |
27 |
|
long histo[MAXCOL][MAXDIV]; |
28 |
+ |
long ctotal[MAXCOL]; |
29 |
|
double minv, maxv; |
30 |
|
int ndiv; |
31 |
|
|
32 |
|
int ncols; |
33 |
|
|
34 |
|
|
35 |
< |
main(argc, argv) |
36 |
< |
int argc; |
33 |
< |
char *argv[]; |
35 |
> |
static void |
36 |
> |
readinp(void) /* gather statistics on input */ |
37 |
|
{ |
35 |
– |
progname = argv[0]; |
36 |
– |
if (argc > 1 && !strcmp(argv[1], "-c")) { |
37 |
– |
cumulative++; |
38 |
– |
argc--; argv++; |
39 |
– |
} |
40 |
– |
if (argc < 3) |
41 |
– |
goto userr; |
42 |
– |
minv = atof(argv[1]); |
43 |
– |
maxv = atof(argv[2]); |
44 |
– |
if (argc == 4) |
45 |
– |
ndiv = atoi(argv[3]); |
46 |
– |
else { |
47 |
– |
if (argc > 4 || !isint(minv) || !isint(maxv)) |
48 |
– |
goto userr; |
49 |
– |
maxv += 0.5; |
50 |
– |
minv -= 0.5; |
51 |
– |
ndiv = maxv - minv + 0.5; |
52 |
– |
} |
53 |
– |
if (minv >= maxv | ndiv <= 0) |
54 |
– |
goto userr; |
55 |
– |
if (ndiv > MAXDIV) { |
56 |
– |
fprintf(stderr, "%s: maximum number of divisions: %d\n", |
57 |
– |
progname, MAXDIV); |
58 |
– |
goto userr; |
59 |
– |
} |
60 |
– |
readinp(); |
61 |
– |
if (cumulative) |
62 |
– |
printcumul(); |
63 |
– |
else |
64 |
– |
printhisto(); |
65 |
– |
exit(0); |
66 |
– |
userr: |
67 |
– |
fprintf(stderr, "Usage: %s [-c] min max n\n", progname); |
68 |
– |
fprintf(stderr, " Or: %s [-c] imin imax\n", progname); |
69 |
– |
exit(1); |
70 |
– |
} |
71 |
– |
|
72 |
– |
|
73 |
– |
readinp() /* gather statistics on input */ |
74 |
– |
{ |
38 |
|
char buf[16*MAXCOL]; |
39 |
|
double d; |
40 |
+ |
int i; |
41 |
|
register int c; |
42 |
|
register char *cp; |
43 |
|
|
50 |
|
d = atof(cp); |
51 |
|
while (*cp && !isspace(*cp)) |
52 |
|
cp++; |
53 |
< |
if (d >= minv && d < maxv) |
53 |
> |
if (d <= minv) |
54 |
> |
outrange[c][0]++; |
55 |
> |
else if (d >= maxv) |
56 |
> |
outrange[c][1]++; |
57 |
> |
else |
58 |
|
histo[c][(int)(ndiv*(d-minv)/(maxv-minv))]++; |
59 |
|
} |
60 |
|
if (c > ncols) |
61 |
|
ncols = c; |
62 |
|
} |
63 |
+ |
for (c = 0; c < ncols; c++) { |
64 |
+ |
ctotal[c] += outrange[c][0] + outrange[c][1]; |
65 |
+ |
for (i = 0; i < ndiv; i++) |
66 |
+ |
ctotal[c] += histo[c][i]; |
67 |
+ |
} |
68 |
|
} |
69 |
|
|
70 |
|
|
71 |
< |
printcumul() /* print cumulative histogram results */ |
71 |
> |
static void |
72 |
> |
printcumul( /* print cumulative histogram results */ |
73 |
> |
int pctl |
74 |
> |
) |
75 |
|
{ |
76 |
< |
long ctot[MAXCOL]; |
76 |
> |
long csum[MAXCOL]; |
77 |
|
register int i, c; |
78 |
|
|
79 |
|
for (c = ncols; c--; ) |
80 |
< |
ctot[c] = 0L; |
80 |
> |
csum[c] = outrange[c][0]; |
81 |
|
|
82 |
< |
for (i = 0; i < ndiv; i++) { |
83 |
< |
printf("%g", minv + (maxv-minv)*(i+1)/ndiv); |
82 |
> |
for (i = 0; i <= ndiv; i++) { |
83 |
> |
printf("%g", minv + (maxv-minv)*i/ndiv); |
84 |
|
for (c = 0; c < ncols; c++) { |
85 |
< |
ctot[c] += histo[c][i]; |
86 |
< |
printf("\t%ld", ctot[c]); |
85 |
> |
if (pctl) |
86 |
> |
printf("\t%f", 100.*csum[c]/ctotal[c]); |
87 |
> |
else |
88 |
> |
printf("\t%ld", csum[c]); |
89 |
> |
if (i < ndiv) |
90 |
> |
csum[c] += histo[c][i]; |
91 |
|
} |
92 |
|
putchar('\n'); |
93 |
|
} |
94 |
|
} |
95 |
|
|
96 |
|
|
97 |
< |
printhisto() /* print histogram results */ |
97 |
> |
static void |
98 |
> |
printhisto( /* print histogram results */ |
99 |
> |
int pctl |
100 |
> |
) |
101 |
|
{ |
102 |
|
register int i, c; |
103 |
|
|
104 |
|
for (i = 0; i < ndiv; i++) { |
105 |
|
printf("%g", minv + (maxv-minv)*(i+.5)/ndiv); |
106 |
|
for (c = 0; c < ncols; c++) |
107 |
< |
printf("\t%ld", histo[c][i]); |
107 |
> |
if (pctl) |
108 |
> |
printf("\t%f", 100.*histo[c][i]/ctotal[c]); |
109 |
> |
else |
110 |
> |
printf("\t%ld", histo[c][i]); |
111 |
|
putchar('\n'); |
112 |
|
} |
113 |
|
} |
114 |
+ |
|
115 |
+ |
|
116 |
+ |
int |
117 |
+ |
main( |
118 |
+ |
int argc, |
119 |
+ |
char *argv[] |
120 |
+ |
) |
121 |
+ |
{ |
122 |
+ |
progname = argv[0]; |
123 |
+ |
while (argc > 1 && argv[1][0] == '-') { |
124 |
+ |
if (argv[1][1] == 'c') |
125 |
+ |
cumulative++; |
126 |
+ |
else if (argv[1][1] == 'p') |
127 |
+ |
percentile++; |
128 |
+ |
else |
129 |
+ |
break; |
130 |
+ |
argc--; argv++; |
131 |
+ |
} |
132 |
+ |
if (argc < 3) |
133 |
+ |
goto userr; |
134 |
+ |
minv = atof(argv[1]); |
135 |
+ |
maxv = atof(argv[2]); |
136 |
+ |
if (argc == 4) |
137 |
+ |
ndiv = atoi(argv[3]); |
138 |
+ |
else { |
139 |
+ |
if (argc > 4 || !isint(minv) || !isint(maxv)) |
140 |
+ |
goto userr; |
141 |
+ |
maxv += 0.5; |
142 |
+ |
minv -= 0.5; |
143 |
+ |
ndiv = maxv - minv + 0.5; |
144 |
+ |
} |
145 |
+ |
if ((minv >= maxv) | (ndiv <= 0)) |
146 |
+ |
goto userr; |
147 |
+ |
if (ndiv > MAXDIV) { |
148 |
+ |
fprintf(stderr, "%s: maximum number of divisions: %d\n", |
149 |
+ |
progname, MAXDIV); |
150 |
+ |
goto userr; |
151 |
+ |
} |
152 |
+ |
readinp(); |
153 |
+ |
if (cumulative) |
154 |
+ |
printcumul(percentile); |
155 |
+ |
else |
156 |
+ |
printhisto(percentile); |
157 |
+ |
exit(0); |
158 |
+ |
userr: |
159 |
+ |
fprintf(stderr, "Usage: %s [-c][-p] min max n\n", progname); |
160 |
+ |
fprintf(stderr, " Or: %s [-c][-p] imin imax\n", progname); |
161 |
+ |
exit(1); |
162 |
+ |
} |
163 |
+ |
|
164 |
+ |
|