Chinaunix首页 | 论坛 | 博客
  • 博客访问: 267806
  • 博文数量: 45
  • 博客积分: 930
  • 博客等级: 准尉
  • 技术积分: 553
  • 用 户 组: 普通用户
  • 注册时间: 2012-01-22 17:53
文章分类

全部博文(45)

文章存档

2013年(5)

2012年(40)

分类: C/C++

2012-05-31 16:51:32


点击(此处)折叠或打开

  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <set>
  5. #include <map>
  6. #include <vector>
  7. #include<sstream>

  8. using namespace std;

  9. class TextQuery{
  10. public:
  11.     typedef std::vector<std::string>::size_type line_no;
  12.     void read_file(std::ifstream & is)
  13.     { store_file(is);build_map();}
  14.     std::set<line_no> run_query(const string & ) const;
  15.     std::string text_line(line_no) const;
  16. private:
  17.     void store_file(std::ifstream &);
  18.     void build_map();
  19.     std::vector<std::string> lines_of_text;
  20.     std::map< string, std::set<line_no> > word_map;
  21. };

  22. ifstream & open_file(ifstream & in,const string & file)
  23. {
  24.     in.close();
  25.     in.clear();
  26.     in.open(file.c_str());
  27.     return in;
  28. }

  29. string make_plural(size_t ctr, const string & word,const string &ending)
  30. {
  31.     return (ctr == 1) ? word: word+ending;
  32. }
  33. void print_results(const set<TextQuery::line_no> & locs,const string & sought,const TextQuery & file)
  34. {
  35.     typedef set<TextQuery::line_no> line_nums;
  36.     line_nums::size_type size = locs.size();
  37.     cout<<"\n"<<sought<<" occurs "
  38.         <<size<<" "
  39.         <<make_plural(size,"time","s")<<endl;

  40.     line_nums::const_iterator it = locs.begin();
  41.     for (; it != locs.end();++it)
  42.     {
  43.         cout<<"\t (line "
  44.             <<(*it) + 1 << ") "
  45.             <<file.text_line(*it) <<endl;
  46.     }
  47. }

  48. void TextQuery::store_file(ifstream & is)
  49. {
  50.     string textline;
  51.     while (getline(is,textline))
  52.         lines_of_text.push_back(textline);
  53. }

  54. void TextQuery::build_map()
  55. {
  56.     for (line_no line_num = 0;line_num != lines_of_text.size();++line_num)
  57.     {
  58.         istringstream line(lines_of_text[line_num]);
  59.         string word;
  60.         while (line >> word)
  61.             word_map[word].insert(line_num);
  62.     }
  63. }

  64. set<TextQuery::line_no>
  65. TextQuery::run_query(const string & query_word) const
  66. {
  67.     map<string, set<line_no> >::const_iterator
  68.         loc = word_map.find(query_word);
  69.     if(loc == word_map.end())
  70.         return set<line_no>();
  71.     else
  72.         return loc->second;
  73. }

  74. string TextQuery::text_line(line_no line) const
  75. {
  76.     if (line < lines_of_text.size())
  77.         return lines_of_text[line];
  78.     throw std::out_of_range("line number out of range");
  79. }
  80. int main(int argc,char ** argv)
  81. {
  82.     ifstream infile;
  83.     if (argc < 2 || !open_file(infile,argv[1]))
  84.     {
  85.         cerr<<"No input file!" <<endl;
  86.         return EXIT_FAILURE;
  87.     }
  88.     TextQuery tq;
  89.     tq.read_file(infile);
  90.     while(true){
  91.         cout<<"enter word to look for, or q to quit: ";
  92.         string s;
  93.         cin >> s;

  94.         if (!cin || s == "q") break;
  95.         set<TextQuery::line_no> locs = tq.run_query(s);
  96.         print_results(locs,s,tq);
  97.     }
  98.     return 0;
  99. }

阅读(1333) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~