-
Notifications
You must be signed in to change notification settings - Fork 800
/
exercise11_38.cpp
64 lines (57 loc) · 1.29 KB
/
exercise11_38.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
#include <iostream>
#include <unordered_map>
#include <fstream>
#include <sstream>
using namespace std;
void word_transform(ifstream&, ifstream&);
unordered_map<string, string> buildMap(ifstream&);
string transform(const string&, unordered_map<string, string>&);
int main()
{
ifstream ifs_rules("H:/code/C++/Cpp_Primer_Answers/data/transform_rules.txt");
ifstream ifs_txt("H:/code/C++/Cpp_Primer_Answers/data/for_transform.txt");
word_transform(ifs_rules, ifs_txt);
return 0;
}
void word_transform(ifstream& rule_file, ifstream& input)
{
auto rule_map = buildMap(rule_file);
string text;
while (getline(input, text))
{
istringstream stream(text);
string word;
bool firstword = true;
while (stream >> word)
{
if (firstword)
firstword = false;
else
cout << " ";
cout << transform(word, rule_map);
}
cout << endl;
}
}
unordered_map<string, string> buildMap(ifstream& rule_file)
{
unordered_map<string, string> m;
string key;
string value;
while (rule_file >> key && getline(rule_file, value))
{
if (value.size() > 1)
m[key] = value.substr(1);
else
throw runtime_error("no rule for " + key);
}
return m;
}
string transform(const string& s, unordered_map<string, string>& m)
{
auto it = m.find(s);
if (it != m.cend())
return it->second;
else
return s;
}