统计4句英文的每个大写字母的出现频率,并用直方图表示出来

code:

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
using namespace std;
int hash[30];

int main()
{
    char s[80];
    memset(hash, 0, sizeof(hash));
    int maxh=0, t=4;
    while(t--)
    {
        cin.getline(s,80);
        int len = strlen(s);
        for(int i=0; i<len; i++)
        {
            if(s[i]>='A' && s[i]<='Z')
            {
                int a=s[i]-'A';
                hash[a]++;
            }
        }
        for(int i=0; i<26; i++)
        {
            if(maxh<hash[i])
                maxh=hash[i];
        }
     }
     for(int i=maxh; i>0; i--)
    {
        for(int j=0; j<26; j++)
        {
            if(hash[j]==i)
            {
                printf("*");
                hash[j]--;
            }
            else
                printf(" ");
            if(j!=25)
                printf(" ");
        }
        printf("\n");
    }
    printf("A B C D E F G H I J K L M N O P Q R S T U V W X Y Z\n");

    return 0;
}

posted on 2011-02-11 12:22  FreeAquar  阅读(135)  评论(0)    收藏  举报