题目:
Write a program to print a histogram of the frequencies of different characters in its input.
#include <stdio.h>
#define REPEAT 1
#define UNIQUE 0
#define MAX_CHAR_COUNT 10
#define MAX_CHAR_FREQ 10
void main(){
int c,state,appear_time,count;
int characters[MAX_CHAR_COUNT],frequencies[MAX_CHAR_FREQ];
c = state = appear_time = count = 0;
for(int i = 0;i< MAX_CHAR_FREQ;i++){
frequencies[i] = 1;
}
while((c = getchar()) != EOF){
if(c != ' '&&c != '\n'&&c != '\t'){
for(int i = 0;i < count;i++){
if(characters[i] == c ){
frequencies[i]++;
state = REPEAT;
break;
}
else{
state = UNIQUE;
}
}
if(state == UNIQUE){
characters[count] = c;
count++;
}
}
}
for(int i = 0;i< count;i++){
putchar(characters[i]);
putchar(':');
for(int j = 0;j< frequencies[i];j++){
putchar('*');
}
putchar('\n');
}
}
要 打印不同的字符直方图
首选考虑要取得不同字符,其次是统计其出现的次数
其算法是,遍历输入的字符,如果它与之前的一个字符有相同,则标记为重复,次数+1;若不重复,则标记为新字符,在字符数组后面补上它,然后自增循环