



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Main operations •^
Push^ –
by one
-^
Pop^ –
one
Other operations •^
…
addition
makes older items inaccessible
Top[S]=
Top[S]=
Top[S]=
New item added on top of stack, size of stack increasesTop-item removed from stack, decreasing stack size by Initialize, Empty, Size, Top, Stack_Top, Display,
New
test_stack.adb
stack defined a priori and cannot be changedstack causes an implementation-specificexception
my_stack.adsmy_stack.adb
[^
]
A program can evaluate postfix expressions for
I in 1 .. length
loop
If
Is_Number(expr(I)) = true
then
push expr(I)
If
Is_Operator(expr(I))
then
pop two numbers from the stackperform operationpush result onto stack
result is on top of stack
one for numbers and one
for operators for
I in 1 .. length
loop
If
Is_Number(expr(I)) = true
then
push expr(I) onto operand_stack If
Is_Operator(expr(I))
then
push expr(I) onto operator_stack If
expr(I) = ‘)’
then
pop 2 numbers from operand_stackpop an operator from the operator_stackperform operationpush the result onto the operand_stack
my_expression_converter.adsmy_expression_converter.adbconverter_test.adb^ use these for this weeks PSET
by reading the expression from left to right end loop How is a bad postfix expression indicated?
Need two stacks, Infix Expressions end loop -- The top of stack contains the result.
Corresponding Postfix
(b * b – 4 * a * c) / (2 * a)
b b * 4 a * c * - 2 a * /
Done := False loop
If
Is_Empty(Op_Stack)
or
next_op is ‘(‘,
set Done to True Elsif
precedence(next_op) > precedence(top_operator) Push next_op onto Op_stack--
ensures higher precedence operators evaluated first Set Done to True Else
Pop the operator_stack If
operator popped is ‘(‘
set Done to True
Else
Done = True
push next_op onto Op_Stack
append operator popped to post_fix string
exit when end loop