-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11.4.cc
82 lines (61 loc) · 1.64 KB
/
11.4.cc
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
#include <unordered_map>
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
void test1(){
unordered_map<string, int> m;
string word;
while(cin >> word)
++m.insert({word, 0}).first->second;
for(auto & p : m){
cout << p.first << " occurs " << p.second <<
(p.second > 1 ? " times" : " time") << endl;
}
}
unordered_map<string, string> & buildMap(unordered_map<string, string> & trans_map, istream & map_file){
string key;
string value;
while(map_file >> key && getline(map_file, value)){
if(value.size() > 1)
trans_map.insert({key, value.substr(1)});
else
throw runtime_error("no rule for " + key);
}
return trans_map;
}
string transform(const string & s, unordered_map<string, string> & trans_map){
auto it = trans_map.find(s);
if(it == trans_map.end())
return s;
return it->second;
}
void word_transform(istream & map_file, istream & input){
unordered_map<string, string> trans_map;
buildMap(trans_map, map_file);
string text;
string word;
while(getline(input, text)){
istringstream in(text);
bool firstWord = true;
while(in >> word){
if(firstWord)
firstWord = false;
else
cout << " ";
cout << transform(word, trans_map);
}
cout << endl;
}
}
void test2(){
ifstream map_file("regular");
ifstream input("text");
word_transform(map_file, input);
}
int main(int argc, char const *argv[])
{
test2();
return 0;
}