zoukankan      html  css  js  c++  java
  • 【题目】英文字符进行频率的统计,直方图输出

    问题

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

    来源

    贴吧吧友提问

    代码

    #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("
    ");
        }
        
        printf("%-3d",0);
        
        for(char ch='A';ch<='Z';++ch)
        {
            printf("----");
        }
        printf("
    ");printf("%-3c",' ');
        for(char ch='A';ch<='Z';++ch)
        {
            printf("  %c",ch);
        }
    
        printf("
    ");
    }
    
    
    
    int main()
    {
        LetterCount lc("G:\data.txt");
        
        lc.analyse();
        
        lc.showGraph();
        
        
        return 0;
    }

    效果

  • 相关阅读:
    Quarts 执行定时任务失败(.job.entity.ScheduleJobEntity cannot be cast to com.)
    Map与String互相转化
    weui中的picker滑动报错
    weui中的picker使用js进行动态绑定数据
    ajax跨域问题解决方案(jsonp的使用)
    pdm文件打开方式
    删除静态页面的html
    js同时获取多个共同class内容标签内容集合
    内外网同时使用
    win10重装系统修改信息
  • 原文地址:https://www.cnblogs.com/lulipro/p/6246473.html
Copyright © 2011-2022 走看看