1. 程式人生 > >改了一下代碼,實現了所有的功能

改了一下代碼,實現了所有的功能

return 表示 color ext truct emp data strcat .net

#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 console_get_name(); 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(); 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); //printf("%s\n",argv[0]); one_backslash_become_two_backslash(a); // strcat(a,b); //printf("%s\n",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); } // get_map(); 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; // for(map_it=wmap.begin(); map_it != wmap.end(); map_it ++) // { // cout << map_it->first <<" "<< map_it->second << endl; // } vector<PAIR> name_number_vec(wmap.begin(), wmap.end()); sort(name_number_vec.begin(), name_number_vec.end(), CmpByValue()); // sort(name_score_vec.begin(), name_score_vec.end(), cmp_by_value); 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; //cin.get(); filename+=".txt"; ifstream fin(filename.c_str()); // read in str[] string temp; map<string,int> wmap; while(fin>>temp) { int len=temp.size(); char tmp[20]=""; int j=0; for(int i=0; i<len; i++) { if (isalpha(temp[i])) { tmp[j]=temp[i]; j++; } } string tmp2(tmp); wmap[tmp2]++; } display_map(wmap); fin.close(); //cin.get(); } void file_get_map(string Arg) { string filename; filename=Arg; // cin.get(); ifstream fin(filename.c_str()); // read in str[] string temp; map<string,int> wmap; while(fin>>temp) { int len=temp.size(); char tmp[20]=""; int j=0; for(int i=0; i<len; i++) { if (isalpha(temp[i])) { tmp[j]=temp[i]; j++; } } string tmp2(tmp); wmap[tmp2]++; } display_map(wmap); fin.close(); //cin.get(); } void console_get_single() { string temp; map<string,int> wmap; while(cin>>temp) { int len=temp.size(); char tmp[20]=""; int j=0; for(int i=0; i<len; i++) { if (isalpha(temp[i])) { tmp[j]=temp[i]; j++; } } string tmp2(tmp); wmap[tmp2]++; if (getchar()==\n) break; } display_map(wmap); //cin.get(); } /*void console_get_name() { string filename; cin>>filename; //cin.get(); filename+=".txt"; ifstream fin(filename.c_str()); // read in str[] string temp; map<string,int> wmap; while(fin>>temp) { int len=temp.size(); char tmp[20]=""; int j=0; for(int i=0; i<len; i++) { if (isalpha(temp[i])) { tmp[j]=temp[i]; j++; } } string tmp2(tmp); wmap[tmp2]++; } display_map(wmap); fin.close(); // cin.get(); }*/ 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; // cin >> filename; // cin.get(); ifstream fin(filename.c_str()); // read in str[] string temp; map<string,int> wmap; while(fin>>temp) { int len=temp.size(); char tmp[20]=""; int j=0; for(int i=0; i<len; i++) { if (isalpha(temp[i])) { tmp[j]=temp[i]; j++; } } string tmp2(tmp); wmap[tmp2]++; } display_map(wmap); fin.close(); // cin.get(); }

排序輸出前十個怎麽實現:把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。

本來可以早一些完成,拿一個很高的分,不過當時懶了,懶了就是懶了。就像上周到的啞鈴和球過了這麽久也只玩了一次一樣。種下一棵樹最好的時間是十年前,其次是今天。

讓我們開始種大樹吧!

改了一下代碼,實現了所有的功能