-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11.38b.cpp
61 lines (56 loc) · 1.31 KB
/
11.38b.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
#include <unordered_map>
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
#include <stdexcept>
using std::unordered_map;
using std::string;
using std::ifstream;
using std::getline;
using std::runtime_error;
using std::istringstream;
using std::cout;
using std::endl;
unordered_map<string, string> transMap(ifstream &rules) {
unordered_map<string, string> trans_map;
string key, value;
while (rules >> key && getline(rules, value)) {
if (value.size() > 1) {
trans_map[key] = value.substr(1);
} else {
throw runtime_error("No rule for " + key);
}
}
return trans_map;
}
const string &transform(const string &s, const unordered_map<string, string> &m) {
unordered_map<string, string>::const_iterator it = m.find(s);
if (it != m.cend()) {
return it->second;
} else {
return s;
}
}
void word_transform(ifstream &rules, ifstream &input) {
unordered_map<string, string> trans_map = transMap(rules);
string line;
while (getline(input, line)) {
istringstream stream(line);
string word;
bool firstword = true;
while (stream >> word) {
if (firstword)
firstword = false;
else
cout << ' ';
cout << transform(word, trans_map);
}
cout << endl;
}
}
int main() {
ifstream rules("11.38_rules.txt"), source("11.38_source.txt");
word_transform(rules, source);
return 0;
}