Escolar Documentos
Profissional Documentos
Cultura Documentos
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:1. Verify that the infix arithmetic expression (the original expression), that may contain
regular parentheses, is properly formed as far as parentheses are concerned.
2. 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.
3. 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:
Scanned the arithmetic expression from left to right, one character at a time.
}
If the stack contains any element at the end of reading the arithmetic expression, then the
expression was not properly formed.
Create an empty stack to hold any arithmetic operators and left parenthesis, ONLY.
A string to contain the postfix expression the output from this conversion.
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 the item ). Discard
it.
}
3. 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, that
operator is popped from off the stack, and is written to the to the output
string. This process is continues until one of two things happen:
(a) 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
(b) The operator on the stack has lower precedence than the one just
scanned, then the recently scanned symbol is pushed onto the stack.
}
4. After 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, if possible
}
}
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 this partial result on 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.
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.