Program Diary-1
打印输入各个字符出现频度的直方图:
-
#include <stdio.h>
-
#include <ctype.h>
-
-
#define MAXHIST 15
-
#define MAXCHAR 128
-
-
int main(void)
-
{
-
int c, i;
-
int len; /* length of histogram */
-
int maxvalue; /* max different characters */
-
int cc[MAXCHAR];
-
-
for(i = 0; i < MAXCHAR; ++i)
-
cc[i] = 0;
-
while((c = getchar()) != EOF)
-
if (c < MAXCHAR)
-
++cc[c];
-
maxvalue = 0;
-
for(i = 1; i < MAXCHAR; i++)
-
if (cc[i] > maxvalue)
-
maxvalue = cc[i];
-
-
for(i = 1; i < MAXCHAR; ++i){
-
if (isprint(i))
-
else
-
if (cc[i] > 0) {
-
if ((len = cc[i] * MAXHIST / maxvalue) <= 0)
-
len = 1;
-
} else
-
len = 0;
-
while (len > 0) {
-
putchar('*');
-
len--;
-
}
-
putchar('\n');
-
}
-
exit(1);
-
-
}