-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparenthesis_handler.cpp
More file actions
108 lines (85 loc) · 3.38 KB
/
parenthesis_handler.cpp
File metadata and controls
108 lines (85 loc) · 3.38 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
#include "parenthesis_handler.h"
#include "calculator_utils.h"
#include "operation_handler.h"
void ParenthesisHandler::handle_open_parenthesis(const size_t i) {
if (!current_operation.empty()) {
if(!CalculatorUtils::is_valid_operator(current_operation.back())) {
current_operation += PARENTHESIS_MULTIPLICATION_TOKEN;
}
if (parentheses_stack.empty()) {
output_operation_stack.push_back(current_operation);
current_operation = "";
}else {
nested_operation_stack.push_back(current_operation);
current_operation = "";
}
}
if (i > 0 && input[i - 1] == ')') {
nested_operation_stack.push_back(PARENTHESIS_MULTIPLICATION_TOKEN);
}
parentheses_stack.emplace_back("(");
nested_operation_stack.emplace_back("(");
}
void ParenthesisHandler::handle_closed_parenthesis(const size_t i) {
std::string _operation, _operator;
while (true) {
if (nested_operation_stack.empty()) break;
const std::string nested_operation = nested_operation_stack.back();
if (nested_operation.empty()) continue;
if (nested_operation == "(") {
parentheses_stack.pop_back();
nested_operation_stack.pop_back();
if (!nested_operation_stack.empty() && CalculatorUtils::is_valid_operator(nested_operation_stack.back())) {
_operator = nested_operation_stack.back();
nested_operation_stack.pop_back();
}
break;
}
_operation = nested_operation;
nested_operation_stack.pop_back();
}
if (!current_operation.empty()) {
if (!_operator.empty() && !working_operation.empty()) {
working_operation = OperationHandler::simplify_operators(current_operation) + _operator + working_operation;
working_operation = OperationHandler::simplify_operators(working_operation);
}else {
working_operation += current_operation;
}
current_operation = "";
}
if (!_operation.empty()) {
working_operation = _operation + working_operation;
}
working_operation = OperationHandler::simplify_operators(working_operation);
if (parentheses_stack.empty()) {
nested_operation_stack.clear();
output_operation_stack.push_back(working_operation);
working_operation = "";
if (i < input.length() - 1 && !CalculatorUtils::is_valid_operator(input[i + 1])) {
output_operation_stack.push_back(PARENTHESIS_MULTIPLICATION_TOKEN);
}
}
}
ParenthesisHandler::StringVector ParenthesisHandler::process() {
for(int i = 0; i < input.length(); i++) {
std::string curr_char(1, input[i]);
if (CalculatorUtils::is_valid_operator_or_digit(curr_char)) {
current_operation += curr_char;
continue;
}
if (curr_char == "(") {
handle_open_parenthesis(i);
}else if(curr_char == ")") {
handle_closed_parenthesis(i);
}
}
if (!parentheses_stack.empty()) {
throw std::runtime_error("Syntax error: unclosed parenthesis");
}
if (!current_operation.empty()) {
output_operation_stack.push_back(current_operation);
current_operation = "";
}
output_operation_stack = OperationHandler::simplify_operators(output_operation_stack);
return output_operation_stack;
}