querymain.cpp
上传用户:qdkongtiao
上传日期:2022-06-29
资源大小:356k
文件大小:2k
源码类别:

书籍源码

开发平台:

Visual C++

  1. /*
  2.  * This file contains code from "C++ Primer, Fourth Edition", by Stanley B.
  3.  * Lippman, Jose Lajoie, and Barbara E. Moo, and is covered under the
  4.  * copyright and warranty notices given in that book:
  5.  * 
  6.  * "Copyright (c) 2005 by Objectwrite, Inc., Jose Lajoie, and Barbara E. Moo."
  7.  * 
  8.  * 
  9.  * "The authors and publisher have taken care in the preparation of this book,
  10.  * but make no expressed or implied warranty of any kind and assume no
  11.  * responsibility for errors or omissions. No liability is assumed for
  12.  * incidental or consequential damages in connection with or arising out of the
  13.  * use of the information or programs contained herein."
  14.  * 
  15.  * Permission is granted for this code to be used for educational purposes in
  16.  * association with the book, given proper citation if and when posted or
  17.  * reproduced.Any commercial use of this code requires the explicit written
  18.  * permission of the publisher, Addison-Wesley Professional, a division of
  19.  * Pearson Education, Inc. Send your request for permission, stating clearly
  20.  * what code you would like to use, and in what specific way, to the following
  21.  * address: 
  22.  * 
  23.  *  Pearson Education, Inc.
  24.  *  Rights and Contracts Department
  25.  *  75 Arlington Street, Suite 300
  26.  *  Boston, MA 02216
  27.  *  Fax: (617) 848-7047
  28. */ 
  29. #include "Query.h"
  30. #include "TextQuery.h"
  31. #include <string>
  32. #include <vector>
  33. #include <map>
  34. #include <set>
  35. #include <iostream>
  36. #include <fstream>
  37. #include <cctype>
  38. #include <cstring>
  39. using std::set;
  40. using std::string;
  41. using std::map;
  42. using std::vector;
  43. using std::cerr;
  44. using std::cout;
  45. using std::cin;
  46. using std::ifstream;
  47. using std::endl;
  48. ifstream& open_file(ifstream&);
  49. int main(int, char **argv)
  50. {
  51.     TextQuery file = build_textfile(argv[1]);
  52.     // iterate with the user: prompt for a word to find and print results
  53.     do {
  54.         string sought;
  55.         if (!get_word(sought)) break;
  56.         // find all the occurrences of the requested string
  57.         // define synonym for the line_no set 
  58.         typedef set<TextQuery::line_no> line_nums; 
  59.         Query name(sought);
  60.         const line_nums &locs = name.eval(file);
  61.         cout << "nExecuted Query for: " << name << endl;
  62.         // report no matches
  63.         print_results(locs, file);
  64.     } while (true);  // loop indefinitely; the exit is inside the loop
  65.     return 0;
  66. }