-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshunting_yard_algorithm.cpp
More file actions
143 lines (116 loc) · 3.43 KB
/
shunting_yard_algorithm.cpp
File metadata and controls
143 lines (116 loc) · 3.43 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
#include "shunting_yard_algorithm.h"
std::vector<std::string> shunting_yard_algorithm::parse_to_RPN(std::string infix_expression)
{
// Shunting-yard algorithm takes an infix expression and produces a postfix expression
std::vector<std::string> operator_stack;
std::vector<std::string> output;
std::string temp_str;
while (infix_expression.size() > 0)
{
// constant (1/2)
// (temp_str is used to store multiple-digit numbers)
if (math::is_number(infix_expression.front()) || infix_expression.front() == '.')
{
temp_str.push_back(infix_expression.front());
infix_expression.erase(infix_expression.begin());
continue;
}
// constant (2/2)
// (when encountering a non-number, push temp_str to output if it is containing values)
if (!(math::is_number(infix_expression.front())) && temp_str.size() > 0)
{
output.push_back(temp_str);
temp_str.clear();
// no continue-keyword here because other actions are required
}
// variable (x)
if (infix_expression.front() == 'x')
{
output.push_back("x");
infix_expression.erase(infix_expression.begin());
continue;
}
// operator
if (math::return_op_precedence(infix_expression.front()))
{
if (operator_stack.size() == 0)
{
std::string temp_str;
temp_str.push_back(infix_expression.front());
operator_stack.push_back(temp_str);
infix_expression.erase(infix_expression.begin());
continue;
}
if (operator_stack.back() != "^" && infix_expression.front() != '^')
{
while (operator_stack.size() > 0 && math::return_op_precedence(operator_stack.back().back()) >= math::return_op_precedence(infix_expression.front()))
{
output.push_back(operator_stack.back());
operator_stack.pop_back();
}
}
std::string temp_str;
temp_str.push_back(infix_expression.front());
operator_stack.push_back(temp_str);
infix_expression.erase(infix_expression.begin());
continue;
}
// '('
if (infix_expression.front() == '(')
{
std::string temp_str;
temp_str.push_back(infix_expression.front());
operator_stack.push_back(temp_str);
infix_expression.erase(infix_expression.begin());
continue;
}
// ')'
if (infix_expression.front() == ')')
{
while (operator_stack.size() > 0 && operator_stack.back() != "(")
{
output.push_back(operator_stack.back());
operator_stack.pop_back();
}
if (operator_stack.size() > 0 && operator_stack.back() == "(")
{
operator_stack.pop_back();
}
infix_expression.erase(infix_expression.begin());
continue;
}
// function
for (std::string function : math::function_list)
{
// If there is an occurance of one of the functions at the front of the expression
if (infix_expression.find(function) == 0 && infix_expression.find_first_of(function) != std::string::npos)
{
std::string temp_str;
for (unsigned int i = 0; i < function.length(); i++)
{
temp_str += infix_expression.front();
infix_expression.erase(infix_expression.begin());
}
operator_stack.push_back(temp_str);
break;
}
}
continue;
}
// if last element in infix expression is a number, push temp_str to output
if (temp_str.size() > 0)
{
output.push_back(temp_str);
temp_str.clear();
}
// if there are still elements in operator stack, pop them to the output
if (operator_stack.size() > 0)
{
while (operator_stack.size() > 0)
{
output.push_back(operator_stack.back());
operator_stack.pop_back();
}
}
return output;
}