Statistics on the frequency of English characters, histogram output

Source: Internet
Author: User

Problem

Statistics on the frequency of the English characters in the specified file are not case-sensitive (both in uppercase statistics), and non-letters are ignored. And the frequency histogram is used to display it.

Source

Bar Friends Questions

Code

#include <iostream>#include<cctype>#include<cstdio>#include<cassert>using namespacestd;classLettercount {Private:        enum{letters_sum= -};//constants: 26 Letters in English            stringFile_path;//file path        intTotal_letters;//The total number of letters to be counted        intLetter_count[letters_sum];//save occurrences of each word        DoubleLetter_frequence[letters_sum];//Save the frequency of occurrences of each word//not allowed to useLettercount (Constlettercount& le);//copy functionlettercount&operator=(Constlettercount& le);//Assignment Function                          Public:        voidSetFilePath (Const Char*path) {file_path = path;}//set the path of a file inline function    voidAnalyse ();//Statistical Functions    voidShowgraph ()Const;//Print a chartLettercount (); Lettercount (Const Char*path); ~Lettercount (); }; Lettercount::lettercount (): File_path (), Total_letters (0){     for(intI=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(intI=0; i<letters_sum;++i) {Letter_count[i]=0; Letter_frequence[i]=0.0; }}lettercount::~Lettercount () {// do nothing}voidLettercount::analyse () {FILE*FP = fopen (File_path.c_str (),"R");//Open FileASSERT (fp!=NULL); Charch;  while((CH=FGETC (fp))! =EOF) {        if(Isalpha (CH))//if it is not an English letter, ignore{//Ignore case, all uppercase statisticsletter_count[toupper (CH)-'A']++; Total_letters++; }} fclose (FP); //Close File//Calculate the frequency of a word     for(intI=0; i<letters_sum;++i) {Letter_frequence[i]= (letter_count[i]*1.0)/total_letters; }     }          voidLettercount::showgraph ()Const{        /**************** Sample Map 100| *               | * *               | * * * 0 |-----------A B C ... *********************/             for(intR= -; r!=0;--r)//the chart has 100 lines representing the hundred percent    {            if(r%Ten==0)//prints the integer percentage of the table for easy observation.printf"%-3d", R); Elseprintf ("   "); printf ("|");  for(CharCh='A'; ch<='Z';++ch) {            if(int(((letter_frequence[ch-'A']* -) +0.5)) >=r) {printf (" *"); }            Else{printf ("  "); } printf (" "); } printf ("\ n"); } printf ("%-3d",0);  for(CharCh='A'; ch<='Z';++ch) {printf ("----"); } printf ("\ n");p rintf ("%-3c",' ');  for(CharCh='A'; ch<='Z';++ch) {printf ("%c", CH); } printf ("\ n");}intMain () {lettercount LC ("G:\\data.txt");        Lc.analyse ();            Lc.showgraph (); return 0;}

Effect

Statistics on the frequency of English characters, histogram output

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.