Requirements:
(1). Implement a console program, given an English string, to count the occurrences of each English word (4 characters above with 4 characters). Additional requirements: read a text file to count the frequency of the words in the text file.
(2). Performance Analysis:
- Run VS's performance analysis tool for C + + code, identify performance issues and run profiling Tools for Java programs NetBeans IDE 6.0, identify performance issues and optimize them.
Estimated completion time 3h, actual completion time:
GitHub link : https://github.com/TYshadow/
Source code:
#include <iostream>#include<ctype.h>#include<algorithm>#include<string>using namespacestd;structWord//Word structure Body{ stringStr; intCount=0; voidExchange (Word &word)//Word Exchange, for sorting { stringTstr =Word. STR; intTCount =Word. Count; Word. Str=Str; Word. Count=Count; Str=Tstr; Count=TCount; }};voidCalccount (Word *words,string&newword,intSize//Word Frequency Statistics{ inti =0; for(; i < size; i++) { if(Words[i]. STR = =Newword) {Words[i]. Count++; return; } Else if(Words[i]. STR = ="") Break; } Words[i]. Str=Newword; Words[i]. Count=1;}voidSortworddown (Word * words,intSize//Sort by word frequency descending{ for(inti =0; i < size; i++) { for(intj =0; J < Size-1; J + +) { if(Words[j]. Count < Words[j +1]. Count) {Words[j].exchange (words[j+1]); } } }}intMain () {Word*words; stringcontent; cout<<"enter a paragraph in English:"; Getline (CIN, content); intWcount =1;//record the total number of words for(unsignedinti =0; I < content.length (); i++) { if(Isalnum (content[i]) = =0)//Non-alpha-numeric{Wcount++; }} words=NewWord[wcount]; string:: Size_type offset = content.find (' ');//Size_type to save the length of a string object while(Offset! =string:: NPOs) { stringWSTR = Content.substr (0, offset);//String.substr () returns a string that returns the specified length from the specified position if(Wstr.length () <4)//remove words that are less than 4 in length{Wcount--; Content.erase (0, offset +1); Offset= Content.find (' '); Continue; } content.erase (0, offset +1);//string.erase () deletes the specified length of characters from the specified positiontransform (Wstr.begin (), Wstr.end (), Wstr.begin (),:: ToLower); Calccount (words, WSTR, wcount); Offset= Content.find (' '); } if(Content.length () >=4) {transform (Content.begin (), Content.end (), Content.begin (),:: ToLower); Calccount (words, content, wcount); //calculate the Last word } Elsewcount--; for(inti =0; i < Wcount; i++) { if(Words[i]. STR = ="") {Wcount--; }} sortworddown (words, wcount); cout<<"Word Frequency Statistics:"<<Endl; for(inti =0; I < Wcount-1; i++) {cout<< Words[i]. Str <<"Frequency:"<< Words[i]. Count <<"Times"<<Endl; } cin.Get(); Delete[] words; return 0;} results :
Summary: so far there have been unfinished tasks: delimiters currently only space, read a text file is not done, performance test test failed, these also need to gradually improve. This topic used a lot of class library, function, this is not familiar with, so need to check to remember things more, in addition to debugging in the time of various small problems, a little debugging process is very nerve-racking. On the question of separators, originally wanted to use regular expression to solve, but online stroll a circle have not too read, so finally empty down. Submit first, then modify it.
Third job of software