-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathOneLoneCoder_ShuntingYardAlgo.cpp
More file actions
256 lines (214 loc) · 6.69 KB
/
Copy pathOneLoneCoder_ShuntingYardAlgo.cpp
File metadata and controls
256 lines (214 loc) · 6.69 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
DIY Programming Language #1
Expressions To Solutions
The Shunting Yard Algorithm
"Francis, have a rest buddy, then get your guitar" - javidx9
License (OLC-3)
~~~~~~~~~~~~~~~
Copyright 2018-2024 OneLoneCoder.com
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions or derivations of source code must retain the above
copyright notice, this list of conditions and the following disclaimer.
2. Redistributions or derivative works in binary form must reproduce
the above copyright notice. This list of conditions and the following
disclaimer must be reproduced in the documentation and/or other
materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Relevant Video: https://youtu.be/unh6aK8WMwM
Links
~~~~~
YouTube: https://www.youtube.com/javidx9
https://www.youtube.com/javidx9extra
Discord: https://discord.gg/WhwHUMV
Twitter: https://www.twitter.com/javidx9
Twitch: https://www.twitch.tv/javidx9
GitHub: https://www.github.com/onelonecoder
Patreon: https://www.patreon.com/javidx9
Homepage: https://www.onelonecoder.com
Author
~~~~~~
David Barr, aka javidx9, ©OneLoneCoder 2024
*/
#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
#include <deque>
int main()
{
struct sOperator
{
uint8_t precedence = 0;
uint8_t arguments = 0;
};
std::unordered_map<char, sOperator> mapOps;
mapOps['/'] = { 2, 2 };
mapOps['*'] = { 2, 2 };
mapOps['+'] = { 1, 2 };
mapOps['-'] = { 1, 2 };
std::string sExpression = "-((1+2)/((6*-7)+(7*-4)/2)-3)";
struct sSymbol
{
std::string symbol = "";
enum class Type : uint8_t
{
Unknown,
Literal_Numeric,
Operator,
Parenthesis_Open,
Parenthesis_Close
} type = Type::Unknown;
sOperator op;
};
std::deque<sSymbol> stkHolding;
std::deque<sSymbol> stkOutput;
sSymbol symPrevious = { "0", sSymbol::Type::Literal_Numeric, 0, 0 };
int pass = 0;
for (const char c : sExpression)
{
if (std::isdigit(c))
{
// Push literals straight to output, they are already in order
stkOutput.push_back({ std::string(1, c), sSymbol::Type::Literal_Numeric });
symPrevious = stkOutput.back();
}
else if (c == '(')
{
// Push to holding stack, it acts as a stopper when we back track
stkHolding.push_front({ std::string(1, c), sSymbol::Type::Parenthesis_Open });
symPrevious = stkHolding.front();
}
else if (c == ')')
{
// Backflush holding stack into output until open parenthesis
while (!stkHolding.empty() && stkHolding.front().type != sSymbol::Type::Parenthesis_Open)
{
stkOutput.push_back(stkHolding.front());
stkHolding.pop_front();
}
if (stkHolding.empty())
{
std::cout << "!!!! ERROR! Unexpected parenthesis '" << std::string(1, c) << "'\n";
return 0;
}
// Remove corresponding open parenthesis from holding stack
if (!stkHolding.empty() && stkHolding.front().type == sSymbol::Type::Parenthesis_Open)
{
stkHolding.pop_front();
}
symPrevious = { std::string(1, c), sSymbol::Type::Parenthesis_Close };
}
else if (mapOps.contains(c))
{
// Symbol is operator
sOperator new_op = mapOps[c];
if (c == '-' || c == '+')
{
if ((symPrevious.type != sSymbol::Type::Literal_Numeric
&& symPrevious.type != sSymbol::Type::Parenthesis_Close) || pass == 0)
{
new_op.arguments = 1;
new_op.precedence = 100;
}
}
while (!stkHolding.empty() && stkHolding.front().type != sSymbol::Type::Parenthesis_Open)
{
// Ensure holding stack front is an operator (it might not be later...)
if (stkHolding.front().type == sSymbol::Type::Operator)
{
const auto& holding_stack_op = stkHolding.front().op;
if (holding_stack_op.precedence >= new_op.precedence)
{
stkOutput.push_back(stkHolding.front());
stkHolding.pop_front();
}
else
break;
}
}
// Push the new operator onto the holding stack
stkHolding.push_front({ std::string(1, c), sSymbol::Type::Operator, new_op });
symPrevious = stkHolding.front();
}
else
{
std::cout << "Bad Symbol: '" << std::string(1, c) << "'\n";
return 0;
}
pass++;
}
// Drain the holding stack
while (!stkHolding.empty())
{
stkOutput.push_back(stkHolding.front());
stkHolding.pop_front();
}
std::cout << "Expression:= " << sExpression << "\n";
std::cout << "RPN := ";
for (const auto& s : stkOutput)
{
std::cout << s.symbol;
}
std::cout << "\n";
// Solver
std::deque<double> stkSolve;
for (const auto& inst : stkOutput)
{
switch (inst.type)
{
case sSymbol::Type::Literal_Numeric:
{
stkSolve.push_front(std::stod(inst.symbol));
}
break;
case sSymbol::Type::Operator:
{
std::vector<double> mem(inst.op.arguments);
for (uint8_t a = 0; a < inst.op.arguments; a++)
{
if (stkSolve.empty())
{
std::cout << "!!! ERROR! Bad Expression\n";
}
else
{
mem[a] = stkSolve[0];
stkSolve.pop_front();
}
}
double result = 0.0;
if (inst.op.arguments == 2)
{
if (inst.symbol[0] == '/') result = mem[1] / mem[0];
if (inst.symbol[0] == '*') result = mem[1] * mem[0];
if (inst.symbol[0] == '+') result = mem[1] + mem[0];
if (inst.symbol[0] == '-') result = mem[1] - mem[0];
}
if (inst.op.arguments == 1)
{
if (inst.symbol[0] == '+') result = +mem[0];
if (inst.symbol[0] == '-') result = -mem[0];
}
stkSolve.push_front(result);
}
break;
}
}
std::cout << "Result := " << std::to_string(stkSolve[0]) << "\n";
return 0;
}