-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResolver.cpp
More file actions
358 lines (307 loc) · 9.6 KB
/
Resolver.cpp
File metadata and controls
358 lines (307 loc) · 9.6 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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*
* Class Resolver
* Karim Sultan, September 11, 2022 - Ported from Python implementation
*
* NOTE:
* The C++ version separates a mathematical resolver and the specialized
* dice case. The DiceResolver is implemented as a derived class.
*
* From Python source:
* +++++++++++++++++++
* Ported from Python 2021 08 12
* Adapted from C# version, DiceResolver.cs by Karim Sultan, 2019.
* Integer based Reverse Polish Notation resolver.
* Converts infix notation to RPN notation.
* Evaluates outcome.
* Enforces order of operations, and handles brackets.
* Does not work with floating point. Although trivial to adjust for floating point,
* the use of integers only was a design decision.
*
* Typical use:
* Math: "(12+2^3)/10*8%5" would resolve to 1.
* Faulty: "(9*7" = 0 or "*oas" = 0 (error always returns 0).
*
* The Resolver class is an infix expression resolver,
* which handles order of operations (Using the Canadian BEDMAS rule
* and not the American PEMDAS rule - although the outcomes are
* necessarily the same under either rule, there is an implemntation
* difference). NOTE: Current implementation doesn't care - uses both.
*
* History (Python)
* KSU 210814 Added '!' (factorial) operator; precedence is debatable,
* since it isn't in the BEDMAS rule, but mathematicians peg
* it between brackets and exponents.
*
* KSU 210814 Added "C" for choose (combinatorics). Set the priority
* to the same as multiplication (via formula expansion).
*
* KSU 210816 Added getHistogram() which provides a heuristic analysis
* of a particular dice roll.
*/
#include "Resolver.h"
#include <string>
#include <stack>
#include <queue>
#include <ctype.h>
#include <math.h>
/*
* Constructor
*
* Clears working data structrures.
*/
gamzia::Resolver::Resolver()
{
while (!s.empty())
s.pop();
while (!q.empty())
q.pop();
error=false;
}
/*
* Converts a valid infix expression(mathematical expression)
* to postfix using Reverse Polish Notation(RPN).Infix exp -
* ression must be valid; this function can not check validi -
* ty.Note that by design, this only supports integer expr -
* ession(no floating point support).FP support can be add -
* ed if while building numbers, the '.' character is accepted.
*
* Example: Expression = "1 + 2 * 3" -- > 7, NOT 9
* RPN = "1 2 3 * +" -- > 7
* Note that the order of operations is preserved in the RPN.
*/
std::string gamzia::Resolver::infixToRPN(std::string expression)
{
// Since a number may be multiple characters, we start with an empty string,
// and while each character is numeric, we append the number until a non -
// numeric value is encountered.
std::string num = "";
// Tokenize expression character by character
for (char c : expression)
{
char token=c;
// Case : we had accumulated a number but this character is not a
// numeric value; so save accumulated number, and reset accumulator.
if (num != "" and !isdigit(token))
{
q.emplace(num);
num = "";
}
// We aren't a number; so handle the token
// '(' start brackets are simply markers of what point to return to when
// # a ')' close bracket is encountered.
if (token=='(')
s.push(token);
// Special case; we look for this first->it means we have to pop all
// previous values off stack into the RPN queue until we find the '('
else if (token==')')
{
// pop up until the bracket
while (s.top() != '(')
{
q.push(std::string(1, s.top()));
s.pop();
}
// pop the bracket / throw it away(it was just a marker, we're done with it)
s.pop();
}
// Case: Operator handling
// We are done handling brackets, check for a valid operator
else if (precedence.find(std::string(1,token)) != precedence.end())
{
while (!s.empty() && precedence[std::string(1, token)] <= precedence[std::string(1, s.top())])
{
q.push (std::string(1, s.top()));
s.pop();
}
s.push(token);
}
// Case: character is numeric.
// Append to accumulator and continue parsing
else if(isdigit(token))
num += token;
} // for
// Did token end on a number ? If so store accumulated number in RPN queue
if (num != "")
q.push(num);
// Now pop items from stack to the queue to cleanup
while (!s.empty())
{
q.push(std::string(1, s.top()));
s.pop();
}
// At this point, we have a valid RPN in the 'q' queue
// (if the infix expression was valid)
// Let's return a string version:
std::queue<std::string> q_copy(q);
std::string rpn = "";
while (!q_copy.empty())
{
rpn += q_copy.front() + " ";
q_copy.pop();
}
return (rpn);
}
/// <summary>
/// Calculates a factorial to x.
/// </summary>
/// <param name="x">Factorial to calculate</param>
/// <returns>The factorial</returns>
long int gamzia::Resolver::factorial(long int x)
{
long int product=1;
if (x<0)
return 1;
else if (x==0 || x==1)
return 1;
while (x>1)
{
product *= x;
x--;
}
return (product);
}
/// <summary>
/// Routine to calculate "choose" (combinatorics)
/// Formula:
/// nCr(n Choose r) = n!/ r!(n - r)!
/// </summary>
/// <param name="n">the number of options</param>
/// <param name="r">the value to choose</param>
/// <returns>The combinatorics result; 0 on error</returns>
long int gamzia::Resolver::choose(long int n, long int r)
{
// Sanity
if (n<r)
return (0);
long int numerator = factorial(n);
long int denominator = factorial(r) * factorial(n-r);
// Sanity
if (denominator == 0)
return (0);
// Finalize
return (numerator / denominator);
}
/// <summary>
/// Given the left hand operand, the right hand operand, and an operator,
/// calculates the result.
/// </summary>
/// <param name="left">The left operand</param>
/// <param name="right">The right operand</param>
/// <param name="op">The operator</param>
/// <returns>The result of the calculation left(op)right</returns>
long int gamzia::Resolver::calculate(long int left, long int right, std::string op)
{
long int x = 0;
char o = op[0];
switch (o)
{
case '+':
x = left + right;
break;
case '-':
x = left - right;
break;
case '*':
x = left * right;
break;
case '/':
x = left / right;
break;
case '^':
x = (long int)pow(left, right);
break;
case '%':
x = left % right;
break;
case '!':
x = factorial(left);
break;
case 'c':
case 'C':
x = choose (left, right);
break;
default:
x=0;
break;
}
return (x);
}
/// <summary>
/// calculateHook is a virtual function that must be overridden by any
/// derived class. It allows the derived class to implement a calculation
/// method, which is called after the main calculations are done.
/// </summary>
/// <param name="left">The left operand</param>
/// <param name="right">The right operand</param>
/// <param name="op">The operator</param>
/// <returns>A long int response to the calculation, 0 on error</returns>
long int gamzia::Resolver::calculateHook (long int left, long int right, std::string op)
{
return (0);
}
/// <summary>
/// Nifty little stack and queue algorithm for evaluating
/// the RPN. Expects a valid RPN expression.
/// </summary>
/// <returns>The integer response</returns>
long int gamzia::Resolver::evaluateRPN ()
{
std::stack<long int> workstack;
// As we pull tokens from the queue, we validate them and if neither
// a number nor an operator, we abort with an exception.
std::queue<std::string> q_copy(q);
std::string t;
long int right, left;
while (!q_copy.empty())
{
t = q_copy.front();
q_copy.pop();
if (precedence.find(t) != precedence.end())
{
// As we work backwards, right value is first, then left
right = workstack.top();
workstack.pop();
// Special case: ! factorial only requires one term
if (t == "!")
left = right;
else
{
left = workstack.top();
workstack.pop();
}
// Return the result of the calculation, plus call the hook method
// (derived class responsibility)
workstack.push (calculate(left, right, t) + calculateHook(left, right, t));
}
else
{
workstack.push (atol(t.c_str()));
}
}
// Answer is now on stack
return (workstack.top());
}
/// <summary>
/// One method to handle it all. Very 'modern c++'.
/// </summary>
/// <param name="expression">A mathematical expression to resolve, in infix notation.</param>
/// <returns>The answer, or 0 on error</returns>
long int gamzia::Resolver::resolve(std::string expression, bool repeat)
{
if (!repeat)
{
// Clear our queue
while (!q.empty())
q.pop();
// Clear the stack
while (!s.empty())
s.pop();
infixToRPN (expression);
return (evaluateRPN());
}
else
// Repeat = true
// This allows repeat dice rolls / calculations, without rebuilding
// the RPN queue each time.
return (evaluateRPN());
}