-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser.cpp
More file actions
167 lines (148 loc) · 4.37 KB
/
Parser.cpp
File metadata and controls
167 lines (148 loc) · 4.37 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include "Parser.h"
std::string add_extra_spaces(std::string input, std::vector<std::string> stop_symbols)
{
std::string res;
for(int i = 0; i < input.length(); ++i)
{
std::string symbol = input.substr(i, 1);
bool found = false;
for(auto stop_symbol: stop_symbols)
{
if(symbol == stop_symbol)
{
auto str = ' ' + symbol + ' ';
res += str;
found = true;
}
}
if(!found)
res += symbol;
}
return res;
}
std::vector<std::string> split(std::string S, char splitter = ' ')
{
std::vector<std::string> result;
std::string word;
for(auto symbol: S)
{
if(symbol == splitter)
{
if(word != "")
{
result.push_back(word);
word = "";
}
}
else
{
word += symbol;
}
}
if(word != "")
result.push_back(word);
return result;
}
Parser::Parser(){
this->operators = {
{"+", Operator([](Fraction a, Fraction b){return a + b;}, 3)},
{"-", Operator([](Fraction a, Fraction b){return a - b;}, 3)},
{"*", Operator([](Fraction a, Fraction b){return a * b;}, 2)},
{"/", Operator([](Fraction a, Fraction b){return a / b;}, 2)},
{"(", Operator(-100)},
{")", Operator(100)}
};
this->show_debug_information = false;
}
std::vector<std::string> Parser::get_all_operator_keys()
{
std::vector<std::string> res;
for(auto it = this->operators.begin(); it != this->operators.end(); ++it)
{
res.push_back(it->first);
}
return res;
}
Parser::Parser(bool show_debug_information) : Parser()
{
this->show_debug_information = show_debug_information;
}
template <typename T>
void print(std::vector<T> vector)
{
for(auto &i : vector)
std::cout << i << ' ';
std::cout << std::endl;
}
void Parser::print_debug_information(std::vector<Fraction> numbers, std::vector<std::string> operators)
{
if(!this->show_debug_information)
return;
std::cout << "numbers: ";
print(numbers);
std::cout << "operators: ";
print(operators);
}
// TODO: rewrite this to use Stack instead of std::vector
void Parser::count(std::vector<Fraction> &numbers, std::vector<std::string> &ops, int max_priority)
{
if(this->show_debug_information)
std::cout << std::endl << "start of count with priority: " << max_priority << std::endl << std::endl;
while(!ops.empty() && ops.back() != "(")
{
if(this->show_debug_information)
{
print_debug_information(numbers, ops);
std::cout << "----------------" << std::endl;
}
if(this->operators[ops.back()].get_priority() > max_priority)
break;
Fraction a = numbers.back();
numbers.pop_back();
Fraction b = numbers.back();
numbers.pop_back();
Operator op = this->operators[ops.back()];
ops.pop_back();
numbers.push_back(op.execute(b, a));
}
if(this->show_debug_information)
{
print_debug_information(numbers, ops);
std::cout << "----------------" << std::endl;
std::cout << "end of count" << std::endl;
}
}
Fraction Parser::parse(std::string input)
{
std::vector<Fraction> numbers;
std::vector<std::string> ops;
std::vector<std::string> splitted = split(add_extra_spaces(input, this->get_all_operator_keys()));
bool is_operator = true;
for(auto &word: splitted)
{
if(word == ")")
{
count(numbers, ops);
ops.pop_back();
}
else if (word == "-" && is_operator)
{
ops.push_back("unar-");
numbers.push_back(-1);
}
else if (this->operators.find(word) != this->operators.end()) // if operator exists
{
auto op = this->operators[word];
while(!ops.empty() && op.get_priority() >= this->operators[ops.back()].get_priority() && ops.back() != "(")
count(numbers, ops, this->operators[ops.back()].get_priority());
ops.push_back(word);
is_operator = true;
} else {
numbers.push_back(std::stod(word));
is_operator = false;
}
}
count(numbers, ops);
Fraction res = numbers.back();
return res;
}