1、全功能实现,但是6号功能之前没看到,所以....(自己加一下吧)
2、第2点要求没看明白,所以暂时理解为在整个新闻类之中有多少个指定类型的新闻
3、功能逐一测试已经完成,但是为节省时间没太多考究顺序
4、ofstream不是以添加在文本末尾的模式打开的。
5、代码间可能有冗余,毕竟个人能力有限
6、完整代码如下(可能比较长):
#include#include#include#include#include#include#includeusing namespace std;class CNews{public: CNews(){ lineNo = 0; } void append(pair> news); void clear(int lineNo); void findNewsTypeCnt(string newsType); void findKeyWordsNews(string word); void cntWords(int lineNo); void save(string fileName);//ios::model not set void load(string fileName); void display();private: typedef map>>::iterator mapIter; map>> newsGroup;//int->lineNo,string1->newsType,string2->newsContext int lineNo;};void CNews::append(pair> news){ lineNo++; newsGroup.insert(make_pair(lineNo,news));}void CNews::clear(int lineNo){ if(newsGroup.begin()==newsGroup.end()) { newsGroup.clear(); return; } this->lineNo = lineNo-1; mapIter nGbcIter = newsGroup.begin();//point to line for(int i=0;i!=lineNo;i++) nGbcIter++;//lineNo.nextElement map>> tempNews(nGbcIter,newsGroup.end()); newsGroup.erase(--nGbcIter,newsGroup.end()); for(mapIter it=tempNews.begin();it!=tempNews.end();it++) this->append(make_pair(it->second.first,it->second.second));}void CNews::findNewsTypeCnt(string newsType){ int cnt = 0; for(mapIter it=newsGroup.begin();it!=newsGroup.end();it++) { if(it->second.first == newsType) cnt++; } cout << cnt << endl;}void CNews::findKeyWordsNews(string word){ map>> tempNews; for(mapIter it=newsGroup.begin();it!=newsGroup.end();it++) { for(vector::iterator vIt = it->second.second.begin();vIt!= it->second.second.end();vIt++) { if(*vIt==word) { tempNews.insert(*it); break; } } } if(tempNews.size() == 0) { cout << "Not find any news."; return; } for(mapIter it=tempNews.begin();it!=tempNews.end();it++) { cout << it->first << "." << it->second.first << " " << endl; for(vector::iterator vIt=it->second.second.begin();vIt!=it->second.second.end();vIt++) cout << *vIt << " "; cout << endl; }}void CNews::cntWords(int lineNo){ cout << "News " << lineNo << ". have " <}void CNews::save(string fileName){ ofstream outfile(fileName.c_str()); string ar; if(!outfile) { cerr << "error:unable to open input file:" << outfile << endl; return; } //format_save for(mapIter it=newsGroup.begin();it!=newsGroup.end();it++) { outfile << it->first << endl << it->second.first << endl; for(vector::iterator vIt=it->second.second.begin();vIt!=it->second.second.end();vIt++) outfile << *vIt; outfile << endl; } outfile.close();}void CNews::load(string fileName){ ifstream infile(fileName.c_str()); if(!infile) { cerr << "error:unable to open input file:" << infile << endl; return; } string line; string newsType; string words; string word; vector news; bool flags = false; int curPos=0; while(getline(infile,line),!infile.eof()) { getline(infile,newsType); getline(infile,words); for(int i=0;i!=words.length();i++) { if(isalpha(words[i]) && !flags) { curPos = i; flags = true; } if(flags && (isspace(words[i]) || i==words.length()-1)) { for(int j=curPos;j!=i;j++) { word += words[j]; } news.push_back(word); word.clear(); flags = false; } } this->append(make_pair(newsType,news)); news.clear(); } infile.clear(); infile.close();}void CNews::display(){ for(mapIter it=newsGroup.begin();it!=newsGroup.end();it++) { cout<< it->first << "." << it->second.first << " " << endl; for(vector::iterator vIt=it->second.second.begin();vIt!=it->second.second.end();vIt++) cout << *vIt << " "; cout << endl; }}int main(){ cout << "Welcome to the newsGroup-system." << endl << " 1.You can append news in the newGroup." << endl << " 2.You can clear the news which you want." << endl << " 3.You can count of the news type which you want." << endl << " 4.You can search news which you want by keyword." << endl << " 5.You can count of the words in news which you want." << endl << " 6.You can load the newsGroup." << endl << endl; string newsType; string words; string keyword; string filename; string stop = "EoX"; vector news; CNews newsGroup; int line; /* // append test // while(cout << " Input news type(OR stop by ctrl+z):") { while(cin >> newsType) { cout << " Input news(OR eof by \"EoX\"):"; while(cin >> words && words != stop) { news.push_back(words); news.push_back(" "); } newsGroup.append(make_pair(newsType,news)); news.clear(); cout << " Input news type(OR stop by ctrl+z):"; } break; } cin.clear(); // save test // cout << "News save in (filename)?"; cin >> filename; newsGroup.save(filename); // load test // cout << "Input filename to load news data." << endl; cin >> filename; newsGroup.load(filename); newsGroup.display(); // clear test // cout << "Input lineNo to clear." << endl; cin >> line; newsGroup.clear(line); newsGroup.display(); // cntWords test // cin >> line; newsGroup.cntWords(line); // news type test // cin >> newsType; newsGroup.findNewsTypeCnt(newsType); cin >> keyword; newsGroup.findKeyWordsNews(keyword); */ return 0;}
10-02 127阅读
08-08 28阅读
08-01 39阅读
06-24 74阅读
07-31 60阅读
08-20 79阅读
05-04 133阅读
08-15 179阅读
11-07 244阅读
11-04 279阅读