The concept of stack is extremely important in computer science and is used in a
ID: 3601974 • Letter: T
Question
The concept of stack is extremely important in computer science and is used in a wide variety of problems. This assignment requires you to write a program that can be used to evaluate ordinary arithmetic expressions that contains any of the five arithmetic operators (+, -, *, /, %).
This exercise requires three distinct steps, namely:-
Verify that the infix arithmetic expression (the original expression), that may contain regular parentheses, is properly formed as far as parentheses are concerned.
If the parenthesized expression is properly formed, convert the expression from an infix expression to its equivalent postfix expression, called Reverse Polish Notation (RPN) named after the Polish Mathematician J. Lukasiewics.
Evaluate the postfix expression, and print the result.
Step 1 - Verify that the expression
Given an arithmetic expression, called an infixed expression, to verify that it is properly formed as far as parentheses are concerned, do the following:
Create an empty stack to hold left parenthesis ONLY.
Scanned the arithmetic expression is from left to right, one character at a time.
While the are more characters in the arithmetic expression
{
If the character is a left parenthesis ‘(‘, push it on to the stack. However if the character is a right parenthesis, ‘)’, visit the stack and pop the top element from off the stack.
}
If the stack contains any element at the end of reading the arithmetic expression, then the expression was not properly formed.
Step 2 - Convert infixed expression to postfix
Given that an arithmetic expression is properly form with respect to parentheses, do the following:
Create an empty stack to hold any arithmetic operators and left parenthesis, ONLY.
A string variable to contain the postfix expression (i.e., the output from this conversion).
Scan the arithmetic expression from left to right.
While there are more symbols in the arithmetic expression,
{
After a symbol is scanned, there are four (4) basic rules to observe and apply accordingly:
If the symbol is an operand (i.e., a number), write it to the output string.
If the symbol is an operator and if the stack is empty, push the symbol on the stack.
Otherwise, if the symbol is either ‘(‘ or ‘)’, check for the following conditions:
If the symbol is ‘(‘, push on to the stack,
Otherwise
If the symbol is ‘)’
{
Pop everything from the operator stack down to the first ‘(‘. Write each item
popped from the stack to the output string. Do not write either of the parentheses onto the output string. Discard them.
}
If the symbol scanned is an arithmetic operator, check for the following and apply accordingly:
If the operator on the top of the stack has higher or equal precedence than the one that was currently read, pop that operator from off the stack, and write it to the output string. This process continues until one of two things happen:
Either the first ‘(‘ is encountered. When this occurs, the ‘(‘ is removed from the stack and is discarded, and the recently scanned symbol is placed on the stack
OR
Push the recently scanned symbol onto the stack.
}
4. When the arithmetic expression is exhausted, any operator is remaining on the stack must be popped from off and is written to the output string.
Step 3 - Evaluate the post fixed expression
Initialize an empty stack that will store operands only.
Scan the postfix string one token (i.e., numbers) at a time.
While there are more tokens in the postfix string
{
If the token is an operand, push it onto the stack.
If the token is an operator
{
Pop the two topmost values from the stack, and store them in the order t1, the topmost, and t2 the second value.
Calculate the partial result in the following order t2 operator t1
Push the result of this calculation onto the stack.
NOTE: If the stack does not have two operands, a malformed postfix expression has occurred, and evaluation should be terminated.
}
}
When the end of the input string is encountered, the result of the expression is popped from the stack.
NOTE: If the stack is empty or if it has more than one operand remaining, the result is unreliable.
Extend this algorithm to include square brackets and curly braces. For example, expressions of the following kind should be considered:
2 + { 2 * ( 10 – 4 ) / [ { 4 * 2 / ( 3 + 4) } + 2 ] – 9 }
2 + } 2 * ( 10 – 4 ) / [ { 4 * 2 / ( 3 + 4) } + 2 ] – 9 {
Only the following are considered valid nestings:
( )
{ }
[ ]
{ ( ) }
[ ( ) ]
[ { } ]
[ { ( ) } ]
Implement the above two algorithms for the following binary operators: addition +, subtraction -, multiplication *, division /, and modulus operation %. All operations are integer operations. To keep things simple, place at least one blank space between each token in the input string.
Explanation / Answer
Step 1 - Verify that the expression
1. Create stack bracket
2. Create string infix
3. infix=arithmatix expression
4. Iterate infix from element 1 till end and store the fetched element in 'e1'(loop starts)
5. if e1= [ or { or ( then push e1 into stack bracket and go to step 4
6. if e1= ) or } or ] then
e2 = pop stack bracket
7. if e1=e2 then go to step 4
else print "Provided expression is not a proper infix expression" . Exit (End of loop)
8. if bracket is empty then print "Provided expression is a proper infix expression"
else "Provided expression is not a proper infix expression"
Explanation : In the above algorithm basically we are checking there are closing bracket to each opening bracket and in the proper order. If the closing bracket is missing or bracket closing order is not correct then the expression is not an infix expression.
Step 2 - Convert infixed expression to postfix
1. Create stack operator
2. Create string infix and postfix
3. infix=arithmatix expression
4. Iterate infix from element 1 till end and store the fetched element in 'e1'(loop starts)
5. if e1=operand then add e1 to the string postfix
6. if e1=[ or { or ( then push e1 into stack operator
7. if e1=) or } or ] then
a. loop e2=pop operator(inner loop starts)
b. if e2=e1 then go to step 4
c. else if e2 is not a bracket then add e2 to the string postfix(inner loop ends)
8. if e1= any operator (+,-,*,/,%) and its precedence > top of the stack operator then push e1 into the stack operator
9. if e1= any operator (+,-,*,/,%) and its precedence = top of the stack operator then use association. If the association is left to right, pop top of the stack operator , add the pop element into teh string postfix and then push the incoming operator in to the stack operator. If the association is right to left, push the incoming operator in to the stack operator.
10. 9. if e1= any operator (+,-,*,/,%) and its precedence < top of the stack operator then pop the stack operator and print the top operator. Then test the incoming operator against the new top of stack.
11. If the stack is empty or contains a [ or { or ( parenthesis on top, push the incoming operator onto the stack operator.(Loop end)
12. Pop all operators from the stack and add into the string postfix excleding any brackets.
Step 3 - Evaluate the post fixed expression
1. Create stack operand
2. Iterate postfix from element 1 till end and store the fetched element in 'e'(loop starts)
3. if e=operand then push e into the stack operand and go to step 2
4. if e=operator then t1=pop operand and t2=pop operand
5. result= t1 e t2 (perform the arithmatic operation)
6. Push result into the stack operand go to step 2. (End loop)
7. Final result = pop operand
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.