-
Notifications
You must be signed in to change notification settings - Fork 16
/
shuntingyard.cpp
697 lines (573 loc) · 17.7 KB
/
shuntingyard.cpp
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
#include "shuntingyard.h"
#include "protocolsupport.h"
#include <math.h>
#include <list>
/*!
* Replace "pi" or "e" in the string with the numeric values. This replacement
* does not check to determine if "pi" or "e" are part of some other word.
* \param input is the input string, which will be modified
* \return a reference to input.
*/
std::string& ShuntingYard::replacePie(std::string& input)
{
replaceinplace(input, "pi", "3.14159265358979323846");
replaceinplace(input, "e" , "2.71828182845904523536");
return input;
}// ShuntingYard::replacePie
/*!
* Given a raw (untokenized) mathematical expression in infix notation, compute
* the result. Allowable operators are " ( ) + - * / ^ ".
* \param infix is the infix expresions to compute
* \param ok is set to true if the computation is good. ok can point to NULL.
* \return the computational result, or 0 if the computation cannot be performed.
*/
double ShuntingYard::computeInfix(const std::string& infix, bool* ok)
{
bool inputok;
std::string postfix = infixToPostfix(infix, &inputok);
if(inputok)
return computePostfix(postfix, ok);
else
{
if(ok != 0)
*ok = false;
return 0;
}
}
/*!
* Given a raw (untokenized) mathematical expression in infix notation, create
* the equalivalent postfix notation with spaces separating the tokens.
* Allowable operators are " ( ) + - * / ^ ".
* \param infix is the infix expresions to convert
* \param ok is set to true if the conversions does not have problems. ok can point to NULL.
* \return the equivalent postfix expression
*/
std::string ShuntingYard::infixToPostfix(const std::string& infix, bool* ok)
{
bool Return = true;
std::list<std::string> operatorStack;
std::string postfix;
// split the string by the separators
std::vector<std::string> list = split(tokenize(infix), " ");
for(std::size_t i = 0; i < list.size(); i++)
{
std::string o1 = list.at(i);
if(o1.empty())
continue;
if(isNumber(o1))
{
postfix += o1;
postfix += " ";
}
else if(o1.find('(') != std::string::npos)
operatorStack.push_back(list.at(i));
else if(o1.find(')') != std::string::npos)
{
std::string o2;
// pop the stack until we hit the left paren. Notice neither
// the left nor the right paren ends up in the output.
while(operatorStack.size() > 0)
{
o2 = operatorStack.back();
operatorStack.pop_back();
if(o2.find('(') != std::string::npos)
break;
else
postfix += o2 + " ";
}// while operators to pop
// If we never find the left paren then the input is malformed
if(o2.find('(') == std::string::npos)
Return = false;
}
else if(isOperator(o1))
{
while(operatorStack.size() > 0)
{
// o2 is not yet popped here
std::string o2 = operatorStack.back();
// Although this is the "operator" stack, o2 could be a parenthesis
if(isOperator(o2))
{
if(isLeftAssociative(o1) && (precedence(o1) <= precedence(o2)))
{
postfix += operatorStack.back() + " ";
operatorStack.pop_back();
}
else if(isRightAssociative(o1) && (precedence(o1) < precedence(o2)))
{
postfix += operatorStack.back() + " ";
operatorStack.pop_back();
}
else
break;
}
else
break;
}// while operators to pop
operatorStack.push_back(o1);
}// if new operator
else
{
// A character we don't recognize
Return = false;
}
}// for all input tokens
// Finally pop the remaining operators off the stack
while(operatorStack.size() > 0)
{
std::string o2 = operatorStack.back();
operatorStack.pop_back();
// There can be no more parenthesis unless the input was bad
if(isParen(o2.at(0)))
Return = false;
else
postfix += o2 + " ";
}
if(ok != 0)
*ok = Return;
// Clean off any white space and return the result
return trimm(postfix);
}// ShuntingYard::infixToPostfix
/*!
* Given a postfix expression with spaces delimiting the tokens, compute the
* result. computePostfix() is usually fed the output of infoxToPostfix().
* Allowable operators are " + - * / ^ ".
* \param postfix is the properly tokenzed postfix string
* \param ok is set to true if the computation is god. ok can point to NULL.
* \return the computational result, or 0 if the computation cannot be performed.
*/
double ShuntingYard::computePostfix(const std::string& postfix, bool* ok)
{
std::list<double> arguments;
// split the string by the separators
std::vector<std::string> list = split(postfix, " ");
for(std::size_t i = 0; i < list.size(); i++)
{
double arg;
bool argok;
std::string o1 = list.at(i);
if(o1.empty())
continue;
// The argument is either an operator or a number
arg = toNumber(o1, &argok);
if(argok)
arguments.push_back(arg);
else if(arguments.size() >= 2)
{
// the rightmost argument is the top of the stack
double arg2 = arguments.back();
arguments.pop_back();
double arg1 = arguments.back();
arguments.pop_back();
if(o1.find('^') != std::string::npos)
{
arguments.push_back(pow(arg1, arg2));
}
else if(o1.find('*') != std::string::npos)
{
arguments.push_back(arg1*arg2);
}
else if(o1.find('/') != std::string::npos)
{
arguments.push_back(arg1/arg2);
}
else if(o1.find('+') != std::string::npos)
{
arguments.push_back(arg1+arg2);
}
else if(o1.find('-') != std::string::npos)
{
arguments.push_back(arg1-arg2);
}
}// if token is operator
else
{
// Something wrong here
if(ok != 0)
*ok = false;
return 0;
}
}// for tokens
// there should be one value left on the stack
if(arguments.size() == 1)
{
// All is good
if(ok != 0)
*ok = true;
return arguments.back();
}
else
{
// Something wrong here
if(ok != 0)
*ok = false;
return 0;
}
}// ShuntingYard::computePostfix
/*!
* Return the operator precedence. Higher is greater precedence.
* \param op is the operator to test
* \return the precedence of the operator, from 4 (for exponentiation) to 2
* (for addition or subtraction). 0 if the operator is not recognized.
*/
int ShuntingYard::precedence(const std::string& op)
{
if(op.find('^') != std::string::npos)
return 4;
else if(op.find('*') != std::string::npos)
return 3;
else if(op.find('/') != std::string::npos)
return 3;
else if(op.find('+') != std::string::npos)
return 2;
else if(op.find('-') != std::string::npos)
return 2;
else
return 0;
}
/*!
* \param op is the operator string to test
* \return true if the operator is right associative
*/
bool ShuntingYard::isRightAssociative(const std::string& op)
{
return !isLeftAssociative(op);
}
/*!
* \param op is the operator string to test
* \return true if the operator is left associative
*/
bool ShuntingYard::isLeftAssociative(const std::string& op)
{
if(op.find('^') != std::string::npos)
return false;
else
return true;
}
/*!
* Convert an input string to an integer.
* \param input is the string to convert. It can be a binary ("0b") or hex ("0x")
* or a decimal number.
* \param ok will be set to true if the conversion is successful, else it will
* be set to false
* \return the converted value, or 0 if the conversion fails
*/
int64_t ShuntingYard::toInt(std::string input, bool* ok)
{
int64_t value = 0;
int base = 10;
std::string set;
if(startsWith(input, "0b"))
{
base = 2;
input.erase(0, 2);
set = "01";
}
else if(startsWith(input, "0x"))
{
base = 16;
input.erase(0, 2);
set = "0123456789aAbBcCdDeEfF";
}
else
set = "-0123456789";
// Test if the number has any characters we do not support
std::size_t index = input.find_first_not_of(set);
if(index < input.size())
{
if(ok != nullptr)
(*ok) = false;
}
else
{
if(ok != nullptr)
(*ok) = true;
try
{
value = (int64_t)std::stoll(input, nullptr, base);
}
catch (...)
{
value = 0;
if(ok != nullptr)
(*ok) = false;
}
}
return value;
}// ShuntingYard::toInt
/*!
* Convert an input string to an unsigned integer.
* \param input is the string to convert. It can be a binary ("0b") or hex ("0x")
* or a decimal number.
* \param ok will be set to true if the conversion is successful, else it will
* be set to false
* \return the converted value, or 0 if the conversion fails
*/
uint64_t ShuntingYard::toUint(std::string input, bool* ok)
{
uint64_t value = 0;
int base = 10;
std::string set;
if(startsWith(input, "0b"))
{
base = 2;
input.erase(0, 2);
set = "01";
}
else if(startsWith(input, "0x"))
{
base = 16;
input.erase(0, 2);
set = "0123456789aAbBcCdDeEfF";
}
else
set = "0123456789";
// Test if the number has any characters we do not support
std::size_t index = input.find_first_not_of(set);
if(index < input.size())
{
if(ok != nullptr)
(*ok) = false;
}
else
{
if(ok != nullptr)
(*ok) = true;
try
{
value = (uint64_t)std::stoull(input, nullptr, base);
}
catch (...)
{
value = 0;
if(ok != nullptr)
(*ok) = false;
}
}
return value;
}// ShuntingYard::toUint
/*!
* \param input is the string to test. It can be a binary ("0b") or hex ("0x")
* or an integer number
* \return true if we can convert input to an integer
*/
bool ShuntingYard::isInt(const std::string& input)
{
bool ok = false;
toInt(input, &ok);
return ok;
}
/*!
* Convert an input string to a number.
* \param input is the string to convert. It can be a binary ("0b") or hex ("0x")
* or a decimal number.
* \param ok will be set to true if the conversion is successful, else it will
* be set to false
* \return the converted value, or 0 if the conversion fails
*/
double ShuntingYard::toNumber(std::string input, bool* ok)
{
double value = 0;
if(startsWith(input, "0b") || startsWith(input, "0x"))
return (double)toInt(input, ok);
// Test if the number has any characters we do not support
std::size_t index = input.find_first_not_of("-.0123456789");
if(index < input.size())
{
if(ok != nullptr)
(*ok) = false;
}
else
{
if(ok != nullptr)
(*ok) = true;
try
{
value = std::stod(input);
}
catch (...)
{
value = 0;
if(ok != nullptr)
(*ok) = false;
}
}
return value;
}// ShuntingYard::toNumber
/*!
* \param input is the string to test. It can be a binary ("0b") or hex ("0x")
* or a decimal number
* \return true if we can convert input to a number
*/
bool ShuntingYard::isNumber(const std::string& input)
{
bool ok = false;
toNumber(input, &ok);
return ok;
}
/*!
* \param input is the character to test
* \param hex should be true to look for hexadecimal characters oly (not counting 'x')
* \param binary should be true to look for binary characters only (not counting 'b')
* \return true if the input is in the set ".0123456789" for decimal or "01"
* for binary or "0123456789aAbBcCdDeEfF" for hexadecimal.
*/
bool ShuntingYard::isNumber(char input, bool hex, bool binary)
{
std::string set;
if(binary)
set = "01";
else if(hex)
set = "0123456789aAbBcCdDeEfF";
else
set = ".0123456789";
return (set.find(input) != std::string::npos);
}
/*!
* \param input is the string to test
* \return true if the first character of the string is an operator from the set " + - * / ^ "
*/
bool ShuntingYard::isOperator(const std::string& input)
{
return isOperator(input.at(0));
}
/*!
* \param input is the character to test
* \return true if the char is an operator from the set " + - * / ^ "
*/
bool ShuntingYard::isOperator(char input)
{
return (std::string("+-*/^").find(input) != std::string::npos);
}
/*!
* \param input is the character to test
* \return true if the char is a parenthesis
*/
bool ShuntingYard::isParen(char input)
{
if(input == '(')
return true;
else if(input == ')')
return true;
else
return false;
}
/*!
* Given an input string make sure that separators are applied between tokens
* \param raw is the input string
* \return the output string with space separators between tokens
*/
std::string ShuntingYard::tokenize(const std::string& raw)
{
typedef enum
{
operation,
number,
numtokentypes
}tokentypes;
std::string output;
std::string input = raw;
tokentypes lasttoken = operation;
char lastcharacter = ' ';
// These flags track if we are parsing a binary or hex number, they can only be set by seeing the 0x or 0b prefix
bool binary = false;
bool hex = false;
// Handle special numbers.
replacePie(input);
for(std::size_t i = 0; i < input.size(); i++)
{
char character = input.at(i);
// this first case is hard, some numbers contain leading negative signs, but not hex or binary numbers.
if(isNumber(character, hex, binary) || ((character == '-') && (lasttoken == operation) && !hex && !binary))
{
// Add a separator if the previous value was an operator. The goal is to keep numerals together
if(lasttoken == operation)
output += ' ';
// remember the type
lasttoken = number;
// And output the character
output += character;
}
else if(((character == 'x') || (character == 'X') || (character == 'b') || (character == 'B')) && (lastcharacter == '0'))
{
// Another hard case, we want to support hexadecimal and binary numbers,
// but we need to preserve the "0x" or "0b" prefix
// Treat this as a number, and note that last character must also have been a number
lasttoken = number;
// Output the character, no separator
output += character;
if((character == 'x') || (character == 'X'))
{
binary = false;
hex = true;
}
else
{
binary = true;
hex = false;
}
}
else if(isOperator(character) || isParen(character))
{
// Whether following operator or number, add a separator
output += ' ';
// remember the type
lasttoken = operation;
// And output the character
output += character;
// Clear the binary and hex flags
binary = false;
hex = false;
}
else
{
// Add a separator
output += ' ';
// And output the character, this is going to be a failure
output += character;
// Clear the binary and hex flags
binary = false;
hex = false;
}
// Remember this for next pass
lastcharacter = character;
}// for all input characters
return output;
}// ShuntingYard::tokenize
/*!
* Test the shunting yard class
* \return true if tests pass, else false
*/
bool ShuntingYard::test(void)
{
bool ok;
// Notice that in the test below the exponents are "stacked", and applied
// from right to left, which means that (1-5) is raised to the 8th power
double test = computeInfix("3+4*2/(1-5)^2^3");
if(fabs(test - 3.0001220703125) > 0.0000000000001)
return false;
// Leading negatie is another strange one
test = computeInfix("-3+4*2/(1-5)^2^3");
if(fabs(test + 2.9998779296875) > 0.0000000000001)
return false;
test = computeInfix("300-262144/((1-5)^3)^3");
if(fabs(test - 301) > 0.0000000000001)
return false;
// Negative exponential
test = computeInfix("-300-1/((1-5)^3)^-3");
if(fabs(test - 261844) > 0.0000000000001)
return false;
// A simple number
test = computeInfix("-3.14159");
if(fabs(test + 3.14159) > 0.0000000000001)
return false;
// Test pi
test = computeInfix("360/(2*-pi)");
if(fabs(test + 180.0/3.14159265358979323846) > 0.0000000000001)
return false;
// Test bad expression
test = computeInfix("360/(2*-pi", &ok);
if((fabs(test ) > 0.0000000000001) || (ok == true))
return false;
return true;
}