From 548e4b40401f9c7dfd9950d81c9f519b25fc8293 Mon Sep 17 00:00:00 2001 From: pezy Date: Tue, 27 Jun 2017 22:46:27 +0800 Subject: [PATCH] finished 17.1.1 --- .vscode/settings.json | 66 +++++++++++++++++++++++++++++++- .vscode/tasks.json | 4 +- ch17/README.md | 24 ++++++++++++ ch17/ex17.1.2/main.cpp | 27 ------------- ch17/ex17_03_text_query.cpp | 58 ++++++++++++++++++++++++++++ ch17/ex17_03_text_query.h | 37 ++++++++++++++++++ ch17/ex17_03_text_query_test.cpp | 18 +++++++++ 7 files changed, 205 insertions(+), 29 deletions(-) create mode 100644 ch17/README.md delete mode 100644 ch17/ex17.1.2/main.cpp create mode 100644 ch17/ex17_03_text_query.cpp create mode 100644 ch17/ex17_03_text_query.h create mode 100644 ch17/ex17_03_text_query_test.cpp diff --git a/.vscode/settings.json b/.vscode/settings.json index a3b2b51f..230b7cf9 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,5 +1,69 @@ { "files.associations": { - "vector": "cpp" + "vector": "cpp", + "iosfwd": "cpp", + "random": "cpp", + "xstring": "cpp", + "bitset": "cpp", + "set": "cpp", + "tuple": "cpp", + "algorithm": "cpp", + "cctype": "cpp", + "cmath": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cstring": "cpp", + "ctime": "cpp", + "cwchar": "cpp", + "deque": "cpp", + "exception": "cpp", + "forward_list": "cpp", + "fstream": "cpp", + "functional": "cpp", + "initializer_list": "cpp", + "iomanip": "cpp", + "ios": "cpp", + "iostream": "cpp", + "istream": "cpp", + "iterator": "cpp", + "limits": "cpp", + "list": "cpp", + "locale": "cpp", + "map": "cpp", + "memory": "cpp", + "new": "cpp", + "numeric": "cpp", + "ostream": "cpp", + "regex": "cpp", + "sstream": "cpp", + "stack": "cpp", + "stdexcept": "cpp", + "streambuf": "cpp", + "string": "cpp", + "system_error": "cpp", + "type_traits": "cpp", + "typeinfo": "cpp", + "unordered_map": "cpp", + "unordered_set": "cpp", + "utility": "cpp", + "xfacet": "cpp", + "xfunctional": "cpp", + "xhash": "cpp", + "xiosbase": "cpp", + "xlocale": "cpp", + "xlocbuf": "cpp", + "xlocinfo": "cpp", + "xlocmes": "cpp", + "xlocmon": "cpp", + "xlocnum": "cpp", + "xloctime": "cpp", + "xmemory": "cpp", + "xmemory0": "cpp", + "xstddef": "cpp", + "xtr1common": "cpp", + "xtree": "cpp", + "xutility": "cpp" } } \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 5cbd3ae3..6aadd786 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -8,7 +8,9 @@ "${file}", "-o", "${file}.exe", - "-std=c++11" + "-std=c++11", + "-pedantic", + "-Wall" ], "showOutput": "always", "isShellCommand": true, diff --git a/ch17/README.md b/ch17/README.md new file mode 100644 index 00000000..e211fc39 --- /dev/null +++ b/ch17/README.md @@ -0,0 +1,24 @@ +# Chapter 17. Specialized Library Facilities + +## Exercise 17.1 + +> Define a `tuple` that holds three `int` values and initialize the members to 10, 20, and 30 + +```cpp +tuple threeInt(10, 20, 30); +``` + +## Exercise 17.2 + +> Define a `tuple` that holds a `string`, a `vector`, and a `pair`. + +```cpp +tuple, std::pair t; +``` + +## Exercise 17.3 + +> Rewrite the `TextQuery` programs from 12.3 (p. 484) to use a `tuple` instead of the `QueryResult` class. +Explain which design you think is better and why. + +[TextQuery Definition](ex17_03_text_query.h) | [TextQuery Implementation](ex17_03_text_query.cpp) | [TextQuery Test](ex17_03_text_query_test.cpp) \ No newline at end of file diff --git a/ch17/ex17.1.2/main.cpp b/ch17/ex17.1.2/main.cpp deleted file mode 100644 index 84eb653a..00000000 --- a/ch17/ex17.1.2/main.cpp +++ /dev/null @@ -1,27 +0,0 @@ -/*************************************************************************** - * @file main.cpp - * @author Alan.W - * @date 3 Mar 2014 - * @remark This code is for the exercises from C++ Primer 5th Edition - * @note - ***************************************************************************/ -//! -//! Exercise 17.1: -//! Define a tuple that holds three int values and initialize the members to 10, 20, and 30. -//! -//! Exercise 17.2: -//! Define a tuple that holds a string, a vector, and a pair. -//! - -#include -#include -#include -#include - - -int main() -{ - std::tuple i3 {10,20,30}; - std::tuple, std::pair> t; - -} diff --git a/ch17/ex17_03_text_query.cpp b/ch17/ex17_03_text_query.cpp new file mode 100644 index 00000000..10087868 --- /dev/null +++ b/ch17/ex17_03_text_query.cpp @@ -0,0 +1,58 @@ +#include "ex17_03_text_query.h" + +#include +#include +#include + +namespace EX03 { + +using std::get; + +TextQuery::TextQuery(ifstream& is) : file(new vector) +{ + string txt; + while (getline(is, txt)) { // for each line in the file + file->push_back(txt); // remember this line of text + int n = file->size() - 1; // the current line number + // remove punctuation from the statement + txt.erase(std::remove_if(txt.begin(), txt.end(), ::ispunct), txt.end()); + std::istringstream line(txt); // separate the line into words + string word; + while (line >> word) { // for each word in that line. + // if word isn't already in wm, subscripting adds a new entry + auto& lines = wm[word]; // lines is a shared_ptr + if (!lines) // that pointer is null the first time we see word + lines.reset(new set); // allocate the new + lines->insert(n); // insert this line number + } + } +} + +query_result TextQuery::query(const string& sought) const +{ + // we'll return a pointer to this set if we don't find sought + static shared_ptr> nodata(new set); + // use find and not a subscript to avoid adding words to wm! + auto loc = wm.find(sought); + if (loc == wm.end()) return std::make_tuple(sought, nodata, file); + return std::make_tuple(sought, loc->second, file); +} + +string make_plural(size_t ctr, string const& word, string const& ending) +{ + return (ctr > 1) ? word + ending : word; +} + +ostream& print(ostream& os, const query_result& qr) +{ + // if the word was found, print the count and all occurrences. + os << get<0>(qr) << " occurs " << get<1>(qr)->size() << " " + << make_plural(get<1>(qr)->size(), "time", "s") << std::endl; + // print each line in which the word appeared + for (auto num : *get<1>(qr)) // for every element in the set + // don't confound the user with text lines starting at 0 + os << "\t(line " << num + 1 << ") " << *(get<2>(qr)->begin() + num) + << std::endl; + return os; +} +} \ No newline at end of file diff --git a/ch17/ex17_03_text_query.h b/ch17/ex17_03_text_query.h new file mode 100644 index 00000000..d08d081e --- /dev/null +++ b/ch17/ex17_03_text_query.h @@ -0,0 +1,37 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +using std::string; +using std::vector; +using std::tuple; +using std::shared_ptr; +using std::set; +using std::map; +using std::ifstream; +using std::ostream; + +namespace EX03 { +using line_no = vector::size_type; +using query_result = + tuple>, shared_ptr>>; + +class TextQuery { +public: + // read the input file and build the map of lines to line numbers. + TextQuery(ifstream&); + query_result query(const string&) const; + +private: + shared_ptr> file; // input file + // map of each word to the set of the lines in which that word appears. + map>> wm; +}; + +ostream& print(ostream&, const query_result&); +} \ No newline at end of file diff --git a/ch17/ex17_03_text_query_test.cpp b/ch17/ex17_03_text_query_test.cpp new file mode 100644 index 00000000..bc0ee65e --- /dev/null +++ b/ch17/ex17_03_text_query_test.cpp @@ -0,0 +1,18 @@ +#include "ex17_03_text_query.h" + +void runQueries(ifstream& infile) +{ + EX03::TextQuery tq(infile); + while (true) { + std::cout << "Enter word to look for, or q to quit: "; + string s; + if (!(std::cin >> s) || s == "q") break; + EX03::print(std::cout, tq.query(s)) << std::endl; + } +} + +int main() +{ + ifstream file("../data/storyDataFile.txt"); + runQueries(file); +} \ No newline at end of file