-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
124 lines (101 loc) · 3.2 KB
/
main.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
#include <iostream>
#include <string>
#include <fstream>
#include <utility>
#include <tuple>
#include <iomanip>
enum Direction {
Left = -1,
Right = +1,
None = 0,
};
void convert(std::istream& in_, std::ostream& out_);
bool isComment(const std::string& string_);
bool isEmpty(const std::string& string_);
void removeSpace(std::string& string_);
char convertChar(const char c_);
std::pair<char, std::string> parseLeftPart(std::string leftPart_);
std::tuple<char, std::string, Direction> parseRightPart(std::string rightPart_);
int main(int argc_, char** argv_) {
std::string inputFileName;
std::cout << "input filename: ";
std::cin >> inputFileName;
std::string outputFilename;
std::cout << "output filename: ";
std::cin >> outputFilename;
std::ifstream in(inputFileName);
if (!in.is_open()) {
std::cout << "incorect input filename!\n";
return 0;
}
std::ofstream out(outputFilename);
if (!out.is_open()) {
std::cout << "incorect output filename!\n";
return 0;
}
convert(in, out);
return EXIT_SUCCESS;
}
void convert(std::istream& in_, std::ostream& out_) {
std::string str;
while (std::getline(in_, str)) {
if (isComment(str)) {
out_ << str << '\n';
} else if (isEmpty(str)) {
out_ << str << '\n';
} else {
const size_t midIndex = str.find("->");
const std::string leftPartOfRule = str.substr(0, midIndex);
const std::string rightPartOfRule = str.substr(midIndex + 2);
char inC, outC;
Direction direction;
std::string inState, outState;
std::tie(inC, inState) = parseLeftPart(leftPartOfRule);
std::tie(outC, outState, direction) = parseRightPart(rightPartOfRule);
out_ << inState << ", " << inC << " -> " << outState << ", " << outC << ", ";
if (direction == Right) {
out_ << std::showpos << direction << '\n';
} else {
out_ << std::noshowpos << direction << '\n';
}
}
}
}
bool isComment(const std::string& string_) {
return string_.find("//") == 0;
}
bool isEmpty(const std::string& string_) {
for (const char& c: string_) {
if (c != ' ')
return false;
}
return true;
}
void removeSpace(std::string& string_) {
int index = 0;
while ((index = string_.find(' ')) != std::string::npos)
string_.erase(index);
}
char convertChar(const char c_) {
if (c_ == 'B') {
return '^';
}
return c_;
}
std::pair<char, std::string> parseLeftPart(std::string leftPart_) {
removeSpace(leftPart_);
return {convertChar(leftPart_.front()), leftPart_.substr(1) };
}
std::tuple<char, std::string, Direction> parseRightPart(std::string rightPart_) {
removeSpace(rightPart_);
const char last = rightPart_.back();
Direction direction = None;
if (last == 'L') {
direction = Left;
} else if (last == 'R'){
direction = Right;
}
return std::make_tuple(convertChar(rightPart_.front()),
rightPart_.substr(1, rightPart_.size() - 2),
direction);
}