【题目】英文字符进行频率的统计,直方图输出

问题

对指定文件中的英文字符进行频率的统计,不区分大小写(都按照大写统计),忽略非字母。并使用频率直方图的形式显示出来。

 

来源

贴吧吧友提问

 

代码

#include<iostream>
#include<cctype>
#include<cstdio>
#include<cassert>

using namespace std;


class LetterCount 
{
    private:
        enum{LETTERS_SUM=26};    //常量:个英文字母有26个 
    
        string file_path;                        //文件路径 
        int    total_letters;                    //统计到的字母的总个数 
        int    letter_count[LETTERS_SUM];        //保存每个单词的出现次数 
        double letter_frequence[LETTERS_SUM];    //保存每个单词的出现频率
        
        //不允许使用 
        LetterCount(const LetterCount& le);             //复制函数 
        LetterCount& operator=(const LetterCount& le);  //赋值函数 
        
    
         
    public:
    
    void setFilePath(const char *path){file_path = path;}  //设置文件的路径 inline function
    void analyse();                                        //统计函数 
    void showGraph() const;                                //打印统计图 
    
    LetterCount();
    LetterCount(const char *path);
    ~LetterCount();
    

}; 



LetterCount::LetterCount():file_path(),total_letters(0)
{

    for(int i=0;i<LETTERS_SUM;++i)
    {
        letter_count[i] = 0;
        letter_frequence[i] = 0.0;
    }    
        
}

LetterCount::LetterCount(const char*path):file_path(path),total_letters(0)
{
    for(int i=0;i<LETTERS_SUM;++i)
    {
        letter_count[i] = 0;
        letter_frequence[i] = 0.0;
    }    
}

LetterCount::~LetterCount()
{
    // do nothing
}

void LetterCount::analyse()
{
    FILE*fp = fopen(file_path.c_str(),"r");    //打开文件 
    assert(fp!=NULL);
    
    char ch;
    while((ch=fgetc(fp))!=EOF)
    {
        if(isalpha(ch))   //如果不是英文字母,则忽略 
        {                 //忽略大小写,全部安大写统计 
            letter_count[ toupper(ch) -'A']++;
            total_letters++;
        }
    }

    fclose(fp); //关闭文件 
    
    //计算单词的频率 
    for(int i=0;i<LETTERS_SUM;++i)
    {
        letter_frequence[i] = (letter_count[i]*1.0)/total_letters ;
    } 
    
} 
         
void LetterCount::showGraph() const
{
        /* ***************样图 
            
            100|   *
               | * *
               | * * *
            0  |-----------
                 A B C ...
            
        
        *********************/
    
    
    for(int r=100;r!=0;--r)    //统计图有100行,代表百分百比 
    {
    
        if(r%10==0)           //打印表的整数百分比,便于观察. 
            printf("%-3d",r);
        else 
            printf("   ");
            
        printf("|");
        
                
        for(char ch='A';ch<='Z';++ch)
        {
            if(int(((letter_frequence[ch-'A']*100) +0.5)) >=r )  
            {
                printf(" *");
            }
            else
            {
                printf("  ");
            }
            printf(" ");
        }    
        printf("\n");
    }
    
    printf("%-3d",0);
    
    for(char ch='A';ch<='Z';++ch)
    {
        printf("----");
    }
    printf("\n");printf("%-3c",' ');
    for(char ch='A';ch<='Z';++ch)
    {
        printf("  %c",ch);
    }

    printf("\n");
}



int main()
{
    LetterCount lc("G:\\data.txt");
    
    lc.analyse();
    
    lc.showGraph();
    
    
    return 0;
}

 

 

效果

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值