改了一下代码,实现了所有的功能
排序输出前十个怎么实现:把map里面的拿到vecror里,vector可以sort,参考了链接http://blog.csdn.net/iicy266/article/details/11906189
输出格式控制怎么实现:cout可以setw(x),其中x是你想要的宽度。例:
cout <<setw(10)<<name_number_vec[i].first<<setw(6)<<name_number_vec[i].second<< endl;
单词设成宽度10了,因为:尽管有长度比10个字母长的单词,不过出现频率最高的单词不太可能是它们。
数字设成6,因为五位数已经挺大了,可以表示到99999。
怎样处理连词符和I’m这种:
首先把数组扩大到40,因为20的时候程序异常退出了,猜想是连词符连起来太长导致数组溢出。(没验证)
然后判断的时候加俩条件就好了
if (isalpha(temp[i])||temp[i]=='-'||temp[i]=='\'')
是字母,或者是连词符,或者是上撇号,就插入到当前词后面。
本来可以早一些完成,拿一个很高的分,不过当时懒了,懒了就是懒了。就像上周到的哑铃和球过了这么久也只玩了一次一样。种下一棵树最好的时间是十年前,其次是今天。
让我们开始种大树吧!
以下是我的代码:
#include <iostream> #include <cstdio> #include <fstream> #include <string> #include <vector> #include <map> #include <cctype> #include <cstring> #include <io.h> #include <algorithm> #include <utility> #include <iomanip> using namespace std; typedef pair<string, int> PAIR; struct CmpByValue { bool operator()(const PAIR& lhs, const PAIR& rhs) { return lhs.second > rhs.second; } }; void display_map(map<string,int> &wmap); void file_get_map(string Arg); void file_txt_get_map(char* A); void console_get_single(); void file_info_get_map(char* fileinfo); void one_backslash_become_two_backslash(char* Path); int main(int argc ,char** argv) {ios::sync_with_stdio(false); if(argc==1) {console_get_single(); return 0; } if(argc==3) { string arg=argv[2]; file_get_map(arg); } char a[200]="",b[20]="*.txt",c[20]="\\",d[20]="-s"; if(strcmp(d,argv[1])==0) { console_get_single(); return 0; } char buf[80]; getcwd(buf,sizeof(buf)); strcat(a,buf); strcat(a,c); strcat(a,argv[1]); strcat(a,c); strcat(a,b); one_backslash_become_two_backslash(a); long Handle; struct _finddata_t FileInfo; if((Handle=_findfirst(a,&FileInfo))==-1L) { char aa[200]; strcpy(aa,argv[1]); file_txt_get_map(aa); } else { file_info_get_map(FileInfo.name); while(_findnext(Handle,&FileInfo)==0) file_info_get_map(FileInfo.name); _findclose(Handle); } return 0; } void display_map(map<string,int> &wmap) { map<string,int>::const_iterator map_it; int tot=0; for(map_it=wmap.begin(); map_it != wmap.end(); map_it ++) tot++; cout<<"total "<<tot<<endl; vector<PAIR> name_number_vec(wmap.begin(), wmap.end()); sort(name_number_vec.begin(), name_number_vec.end(), CmpByValue()); for (int i = 0; i != name_number_vec.size(); ++i) { if(i>9) break; cout <<setw(10)<<name_number_vec[i].first<<setw(6)<<name_number_vec[i].second<< endl; } } void file_txt_get_map(char* A) { string filename; filename=A; filename+=".txt"; ifstream fin(filename.c_str()); string temp; map<string,int> wmap; while(fin>>temp) { int len=temp.size(); char tmp[40]=""; int j=0; for(int i=0; i<len; i++) { if (isalpha(temp[i])||temp[i]=='-'||temp[i]=='\'') { tmp[j]=temp[i]; j++; } } string tmp2(tmp); wmap[tmp2]++; } display_map(wmap); fin.close(); } void file_get_map(string Arg) { string filename; filename=Arg; ifstream fin(filename.c_str()); string temp; map<string,int> wmap; while(fin>>temp) { int len=temp.size(); char tmp[40]=""; int j=0; for(int i=0; i<len; i++) { if (isalpha(temp[i])||temp[i]=='-'||temp[i]=='\'') { tmp[j]=temp[i]; j++; } } string tmp2(tmp); wmap[tmp2]++; } display_map(wmap); fin.close(); } void console_get_single() { string temp; map<string,int> wmap; while(cin>>temp) { int len=temp.size(); char tmp[40]=""; int j=0; for(int i=0; i<len; i++) { if (isalpha(temp[i])||temp[i]=='-'||temp[i]=='\'') { tmp[j]=temp[i]; j++; } } string tmp2(tmp); wmap[tmp2]++; } display_map(wmap); } void one_backslash_become_two_backslash(char* Path) { int len=strlen(Path); char Path2[100]; for(int i=0,j=0; i<len; i++) { if(Path[i]=='\\') { Path2[j++]='\\'; Path2[j++]='\\'; } else Path2[j++]=Path[i]; } strcpy(Path2,Path); } void file_info_get_map(char* fileinfo) { string filename=fileinfo; ifstream fin(filename.c_str()); string temp; map<string,int> wmap; while(fin>>temp) { int len=temp.size(); char tmp[40]=""; int j=0; for(int i=0; i<len; i++) { if (isalpha(temp[i])||temp[i]=='-'||temp[i]=='\'') { tmp[j]=temp[i]; j++; } } string tmp2(tmp); wmap[tmp2]++; } display_map(wmap); fin.close(); }