forked from pezy/CppPrimer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex12.29.cpp
125 lines (102 loc) · 3.29 KB
/
ex12.29.cpp
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
/***************************************************************************
* @file The code is for the exercises in C++ Primmer 5th Edition
* @author Alan.W
* @date 28 DEC 2013
* @remark
***************************************************************************/
//!
//! Exercise 12.29:
//! We could have written the loop to manage the interaction with the user as a do
//! while (§ 5.4.4, p. 189) loop. Rewrite the loop to use a do while. Explain which
//! version you prefer and why.
// Do while loop will execute the statement first and then checnk the condition.Thus
// without specfic aim I prefer while.
//!
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <set>
void toVector(std::ifstream &fin, std::vector<std::string> &v);
std::size_t
search(const std::string &kWord, const std::vector<std::string> &v,
std::map<std::string, std::set<int>> &m);
void printLines(const std::string &kWord, const std::vector<std::string> &v,
std::map<std::string, std::set<int>> &m);
int main()
{
std::ifstream fin("test.txt");
std::vector<std::string> v;
//! store the file in the vector
toVector(fin, v);
do
{
//! codes for input
std::cout << "enter:\n";
std::string keyWord;
std::cin >>keyWord;
if(keyWord == "q")
{
std::cout << "\nBye!\n";
break;
}
//! create the map to store the query result and get the count of occurrence
std::map<std::string, std::set<int>> m;
std::size_t count = search(keyWord, v, m);
//! print the key word and count of occurrence.
std::cout << "["
<< keyWord
<< "]:\n"
<< count
<<" appears times in lines:\n";
//! print lines in which the key word appears
printLines(keyWord, v, m);
}
while(true);
return 0;
}
//! store the file in the vector
void toVector(std::ifstream &fin, std::vector<std::string> &v)
{
std::string line;
while(std::getline(fin, line)) v.push_back(line);
}
//! store the query result in the map and return the amout of occurrence.
std::size_t
search(const std::string &kWord, const std::vector<std::string> &v,
std::map<std::string, std::set<int>> &m)
{
std::size_t count = 0;
//! each line
for(std::size_t index = 0; index != v.size(); ++index)
{
std::stringstream lineStream(v[index]);
std::string word;
//! each word
while(lineStream >> word)
if(word == kWord)
{
++count;
m[kWord].insert(index);
}
}
return count;
}
//! print lines in which the key word appears
void printLines(const std::string &kWord, const std::vector<std::string> &v,
std::map<std::string, std::set<int> > &m)
{
//! fetch the set that stores the query result
std::set<int>& set = m[kWord];
//! print each line in which the key word appears
for(const auto &index : set)
{
std::cout << "( Line "
<< index
<<" ) : "
<< v[index]
<<"\n";
}
}