Requirements: Read the content from an English txt to achieve word frequency statistics.
Now complete: Basic ability is probably completed, due to poor programming foundation, the file operation part is not very skilled, I found from the file to extract the string stream read into the program of the string object, will be all the space filter out, resulting in the inability to count the word frequency, there is no way to find a solution, can only enter the article manually. ORZ ...
To learn Java well, for the time being, dealing with strings and other problems is still a mature solution for Java.
1 /** 2 * For a section of English word frequency statistics 3 4*/ 5#include <iostream>6#include <string>7 8 using namespacestd; 9 /** 10 * Word object one by one*/ A structWord - { -Word (): Str (""), Count (0) the {} - stringStr; - intCount; - + /** 20 * exchange words (for sorting) * Word Exchange Words object*/ at voidExchange (Word &word) - { - stringTstr =Word. STR; - intTCount =Word. Count; -Word. STR =Str; -Word. Count =Count; inSTR =Tstr; -Count =TCount; to } + }; - the /** 35 * Statistic word count * Words words array PNS * Newword words content * Size number of words*/ + voidCalccount (Word * words,string&newword,intsize) A { the inti =0; + for(; i < size; i++) - { $ if(Words[i]. STR = =Newword) $ { -Words[i]. count++; - return; the } - Else if(Words[i]. STR = ="") Wuyi Break; the } -Words[i]. STR =Newword; WuWords[i]. Count =1; - } About $ - voidSortworddown (Word * words,intsize) - { - for(inti =0; i < size; i++) A { + for(intj =0; J < size-1; J + +) the { - if(Words[j]. Count < words[j+1]. Count) $ { theWords[j].exchange (words[j+1]); the } the } the } - } in the intMain () the { AboutWord *words; the stringcontent; thecout <<"enter a paragraph in English:"; thegetline (CIN, content); + - //calculate the total number of words the intWcount =1; Bayi for(unsignedinti =0; I < content.length (); i++) the { the if(Content[i] = =' ') -wcount++; - } theWords =NewWord[wcount]; the the string:: Size_type offset = content.find (' ');//words are separated by a space the while(Offset! =string:: NPOs) - { the stringWSTR = Content.substr (0, offset); theContent.erase (0, offset+1); theCalccount (words, WSTR, wcount);94offset = Content.find (' '); the } theCalccount (words, content, wcount);//calculate the Last word the 98Sortworddown (words, wcount); About intPrintCount =Wcount; - 101 for(inti =0; i < PrintCount; i++)102 {103cout << Words[i]. Str <<"\ t"<< Words[i]. Count <<Endl;104 } the 106 Delete[] words;107 return 0;108}
C + + implements Word frequency statistics