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