英文文章单词统计
功能
统计一篇英文文章,按单词出现次数输入,按单词字典序输出,按单词逆序输出
思路
先拿到文件中的单词,利用split函数分隔,原理是string的find_of_first函数,第二个参数很好用,可以根据分号逗号等等任意分隔。将分隔的一个一个单词存储到vector中。
然后遍历vector,存储在multimpa中,使用键值对方式,健是string,值是int,使用find函数,如果map中有则值加一,没有则放入。multimap中的会按照第一个元素排序输出,
即string的字典序输出。如何按照int从大到小输出?将map的值放到vector中,使用自定义排序写一个cmp函数,即可、
然后遍历vector,存储在multimpa中,使用键值对方式,健是string,值是int,使用find函数,如果map中有则值加一,没有则放入。multimap中的会按照第一个元素排序输出,
即string的字典序输出。如何按照int从大到小输出?将map的值放到vector中,使用自定义排序写一个cmp函数,即可、
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
#include <algorithm> #include <iostream> #include <cstring> #include<fstream> #include <vector> #include <map> using namespace std; //存储被切割后的string的容器 vector<string> res; // vector<pair<string,int>> vec; //匹配逗号空格等等!正则表达式 string Delema=" ,.\""; fstream datafile; string filename = "a.txt",wordline; typedef multimap<string,int> map_std; map_std::iterator it; map_std mp; //分割字符函数,不用细看 void split(string& s,vector<string>&res,string& delema1){ string::size_type start=s.find_first_not_of(delema1,0);//找到第一个不为逗号的下标 string::size_type pose=s.find_first_of(delema1,start);//找到第一个逗号的下标 while(string::npos!=start||string::npos!=pose){//当即没有逗号也没有字符的时候结束 res.push_back(s.substr(start,pose-start)); start=s.find_first_not_of(delema1,pose);//更新start 从pose开始 pose=s.find_first_of(delema1,start);//更新pos,从start开始 } } void get_file(){ cout<<"请输入要处理的英文附件!"<<endl; cin>>filename; datafile.open(filename.c_str(),fstream::in); if(datafile.is_open() == false){ cout<<"文件打开失败!"<<endl; }else{ while(getline(datafile,wordline)){ split(wordline,res,Delema); } } // for(auto& i:res) // cout<<i<<endl; //给res的单词放在map中 vector<string>::iterator first = res.begin(); vector<string>::iterator last = res.end(); int num =1; string word="a"; while(first!=last){ word = *first; //现在mp中找找有没有word // cout<<*first++<<endl; first++; it = mp.find(word); if(it == mp.end()){ //mp中没有这个单词 mp.insert(make_pair(word,num)); }else{ //mp中有这个单词! it->second++; } } } bool cmp(pair<string,int> a,pair<string,int> b){ return a.second > b.second; } void task1(){ for(map_std::iterator p=mp.begin();p!=mp.end();p++){ vec.push_back(make_pair(p->first,p->second)); } sort(vec.begin(), vec.end(), cmp); for (auto i = 0; i < vec.size(); ++i) cout << vec[i].first << " " << vec[i].second << endl; } void task2(){ map_std::iterator p = mp.begin(); for(p = mp.begin();p!=mp.end();p++){ cout<<p->first<<" "<<p->second<<endl; } } void task3(){ // for(auto& i:res) // cout<<i<<endl; vector<string>::iterator first = res.begin(); vector<string>::iterator last = res.end(); while(first !=last) cout<<*--last<<endl; } void print(){ printf("\t1.按单词个数输出\n"); printf("\t2.按单词顺序输出\n"); printf("\t3.逆序输出\n"); } void menu(){ system("pause"); system("cls"); system("color 4E");//黑框界面颜色 printf("************欢迎进入系统***********\n"); } int main() { get_file();//先拿到txt文件中的单词 // int num; // string word; // // cin>>word; // num=1; // mp.insert(make_pair(word,num)); // cin>>word; // mp.insert(make_pair(word,num)); // cin>>word; // mp.insert(make_pair(word,num)); // // it = mp.find("aa"); // if(it == mp.end()){ // cout<<"0000000000"<<endl; // }else{ // cout<<"zhaodao le !!!\n"; // cout<<it->first<<" "<<it->second<<endl; // it->second++; // cout<<it->first<<" "<<it->second<<endl; // } // // map_std::iterator p = mp.begin(); // // for(p = mp.begin();p!=mp.end();p++){ // cout<<p->first<<" "<<p->second<<endl; // } // while(1){ menu(); print(); int choice ; scanf("%d",&choice); if(choice == 1){ task1(); }else if(choice ==2){ task2(); }else if(choice ==3){ task3(); }else if(choice ==4){ break; }else{ cout<<"输入有误,请重新输入!"<<endl; } } return 0; } |