|
ADS
Algorithms and Data Structures
|
Arithmetic expressions with a STACK, beyond the book. More...
Files | |
| file | ADS/stack/Expressions.h |
| Arithmetic expressions with a STACK [see ADT.h], beyond the book: the operands are nonnegative integers, the operators the binary +, -, * and / (the division truncated toward zero), and the tokens of an expression may be separated by spaces. | |
Functions | |
| long | ADS::evalPostfix (std::string_view e) |
| the value of the postfix expression \( e \) | |
| std::string | ADS::toPostfixParenthesized (std::string_view e) |
| the postfix form of the fully parenthesized infix expression \( e \), its tokens separated by one space | |
| std::string | ADS::toPostfix (std::string_view e) |
| the postfix form of the infix expression \( e \), with the usual precedences and the associativity to the left, its tokens separated by one space | |
Arithmetic expressions with a STACK, beyond the book.
The definitions, the invariants and the costs of the area are in Theory of stacks.
| long ADS::evalPostfix | ( | std::string_view | e | ) |
the value of the postfix expression \( e \)
In pseudocode (Ćukasiewicz, beyond the book), each operator replacing its two operands on the stack with its result:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{evalPostfix}( e ) \cr 2 & \quad S \gets \text{an empty stack} \cr 3 & \quad \textbf{foreach}\ \text{token } x \text{ of } e\ \textbf{do} \cr 4 & \qquad \textbf{if}\ x \text{ is an operand}\ \textbf{then} \ \mathrm{push}( S , x ) \cr 5 & \qquad \textbf{else}\ t \gets \mathrm{pop}( S );\ s \gets \mathrm{pop}( S );\ \mathrm{push}( S , s \mathbin{x} t ) \cr 6 & \quad \textbf{return}\ \mathrm{pop}( S ) \end{array}} \]
| std::string ADS::toPostfix | ( | std::string_view | e | ) |
the postfix form of the infix expression \( e \), with the usual precedences and the associativity to the left, its tokens separated by one space
In pseudocode (the shunting-yard algorithm of Dijkstra, beyond the book), \( \pi \) the precedence, 2 for * and / and 1 for + and -:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{toPostfix}( e ) \cr 2 & \quad S \gets \text{an empty stack};\ \mathit{out} \gets \text{the empty sequence} \cr 3 & \quad \textbf{foreach}\ \text{token } x \text{ of } e\ \textbf{do} \cr 4 & \qquad \textbf{if}\ x \text{ is an operand}\ \textbf{then} \ \text{append } x \text{ to } \mathit{out} \cr 5 & \qquad \textbf{else if}\ x \text{ is an open parenthesis} \ \textbf{then}\ \mathrm{push}( S , x ) \cr 6 & \qquad \textbf{else if}\ x \text{ is a closed parenthesis} \ \textbf{then} \cr 7 & \qquad\quad \textbf{while}\ \mathrm{top}( S ) \text{ is not an open } \text{parenthesis}\ \textbf{do}\ \text{append } \mathrm{pop}( S ) \text{ to } \mathit{out} \cr 8 & \qquad\quad \mathrm{pop}( S )\quad \triangleright\ \text{the open } \text{parenthesis} \cr 9 & \qquad \textbf{else} \cr 10 & \qquad\quad \textbf{while}\ S \ne \emptyset\ \textbf{and} \ \mathrm{top}( S ) \text{ is an operator}\ \textbf{and}\ \pi( x ) \le \pi( \mathrm{top}( S ) )\ \textbf{do} \cr 11 & \qquad\qquad \text{append } \mathrm{pop}( S ) \text{ to } \mathit{out} \cr 12 & \qquad\quad \mathrm{push}( S , x ) \cr 13 & \quad \textbf{while}\ S \ne \emptyset\ \textbf{do} \ \text{append } \mathrm{pop}( S ) \text{ to } \mathit{out} \cr 14 & \quad \textbf{return}\ \mathit{out} \end{array}} \]
| std::string ADS::toPostfixParenthesized | ( | std::string_view | e | ) |
the postfix form of the fully parenthesized infix expression \( e \), its tokens separated by one space
In pseudocode (Sedgewick, section 4.3, beyond the book), each operator written at the closed parenthesis of its pair:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{toPostfixParenthesized}( e ) \cr 2 & \quad S \gets \text{an empty stack};\ \mathit{out} \gets \text{the empty sequence} \cr 3 & \quad \textbf{foreach}\ \text{token } x \text{ of } e\ \textbf{do} \cr 4 & \qquad \textbf{if}\ x \text{ is an operand}\ \textbf{then} \ \text{append } x \text{ to } \mathit{out} \cr 5 & \qquad \textbf{else if}\ x \text{ is an operator}\ \textbf{then} \ \mathrm{push}( S , x ) \cr 6 & \qquad \textbf{else if}\ x \text{ is a closed parenthesis} \ \textbf{then}\ \text{append } \mathrm{pop}( S ) \text{ to } \mathit{out} \cr 7 & \quad \textbf{return}\ \mathit{out} \end{array}} \]