-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfix_to_postfix.php
215 lines (195 loc) · 6.33 KB
/
infix_to_postfix.php
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
<?php
/**
* Infix to postfix generator and evaluator
*
* @author https://www.rune-server.ee/members/funke/
* @author Simon Mitchell <[email protected]>
* @see https://www.rune-server.ee/programming/website-development/446325-php-calculator-infix-postfix-eval.html
*/
/**
* Association enumerator
*/
enum Association
{
case NONE;
case LEFT;
case RIGHT;
}
/**
* Postfix Utilities Class
*/
final class PostfixUtils
{
/**
* @var array[] Array of operators with attributes.
* Each array item MUST have three attributes:
* <code>
* [
* (integer operator precedence - bigger number, higher precedence),
* Association,
* (bool flag indicating if operator is unary)
* ]
* ...
* </code>
*/
private static array $operators = [
'^' => [9, Association::RIGHT, false],
'*' => [8, Association::LEFT, false],
'/' => [8, Association::LEFT, false],
'%' => [8, Association::LEFT, false],
'+' => [5, Association::LEFT, false],
'-' => [5, Association::LEFT, false],
'(' => [0, Association::NONE, false],
')' => [0, Association::NONE, false],
] ;
/**
* Get precedence for specific operator
* @param string $opchar Operator character
* @return int Operator precedence
*/
private static function precedence(string $opchar): int
{
return self::$operators[$opchar][0];
}
/**
* Get associativity for specific operator
* @param string $opchar Operator character
* @return Association Operator associativity
*/
private static function associativity(string $opchar): Association
{
return self::$operators[$opchar][1];
}
/**
* Determine if operator is unary
* @param string $opchar Operator character
* @return bool Unary flag
*/
private static function unary(string $opchar): bool
{
return self::$operators[$opchar][2];
}
/**
* Determine if character is an operator
* @param string $char
* @return bool
*/
private static function isOperator(string $char): bool
{
return array_key_exists($char, self::$operators);
}
private static function startsWith(string $haystack, string $needle): bool
{
return !strncmp($haystack, $needle, strlen($needle));
}
private static function endsWith(string $haystack, string $needle): bool
{
return str_ends_with($haystack, $needle);
}
private static function arrayPeek(array $stack): string
{
return $stack[count($stack) - 1];
}
/**
* Convert a given expression into Postfix
* @param string $expression An expression to parse into postfix format
* @return string
*/
public static function postfix(string $expression): string
{
// SM: Remove any white space from the expression.
$expression = preg_replace('/\s+/', '', $expression);
// SM: Make sure out expression is encapsulated within parenthesis.
if (!self::startsWith($expression, '(')) {
$expression = '(' . $expression;
}
if (!self::endsWith($expression, ')')) {
$expression .= ')';
}
$stack = [];
$output = '';
$previous = true;
for ($i = 0; $i < strlen($expression); $i++) {
$char = $expression[$i];
if (self::isOperator($char)) {
if ($char == '(') {
$stack[] = $char;
} elseif ($char == ')') {
while (count($stack) > 0 && ($top = self::arrayPeek($stack)) != '(') {
$output .= ' ' . $top;
array_pop($stack);
}
array_pop($stack);
} else {
while (count($stack) > 0) {
$peek = self::arrayPeek($stack);
if (
(
self::associativity($char) == Association::LEFT
&& self::precedence($char) <= self::precedence($peek)
)
||
(
self::associativity($char) == Association::RIGHT
&& self::precedence($char) < self::precedence($peek)
)
) {
$output .= ' ' . self::arrayPeek($stack);
array_pop($stack);
} else {
break;
}
}
$stack[] = $char;
}
$previous = true;
} else {
$output .= ($previous ? ' ' : '') . $char;
$previous = false;
}
}
while (count($stack) > 0) {
if (self::arrayPeek($stack) == '(') {
array_pop($stack);
} else {
$output .= ' ' . array_pop($stack);
}
}
return $output;
}
/**
* @param string $postfix Postfix expression to evaluate.
* @return string
*/
public static function postfixEval(string $postfix): string
{
$stack = [];
$num = '';
for ($i = 0; $i < strlen($postfix); $i++) {
$char = $postfix[$i];
if (self::isOperator($char)) {
$second = array_pop($stack);
if ($char == '^') {
$stack[] = pow(array_pop($stack), $second);
} else {
$stack[] = eval("return " . array_pop($stack) . " $char $second;");
}
} else {
if ($char == ' ') {
if (strlen($num) > 0) {
$stack[] = $num;
}
$num = '';
} else {
$num .= $char;
}
}
}
return array_pop($stack);
}
}
$expression = '(32*3)+(4/5-(6^9/(4%8)))';
echo('Expression: ' . $expression . PHP_EOL);
$postfix = PostfixUtils::postfix($expression);
echo('Postfix: ' . $postfix . PHP_EOL);
echo('Evaluation: ' . PostfixUtils::postfixEval($postfix) . PHP_EOL);