Hey guys, i got a big problem to make a program that evaluates the postfix expre
ID: 3631169 • Letter: H
Question
Hey guys, i got a big problem to make a program that evaluates the postfix expression.the task is as follows:
Postfix notation is a way to write expressions without using brackets.
A postfix expression can be evaluated using a stack. We scan the expression from left to right.
If we see an operand, push to stack. If we see an operator, fetch operands from the stack, evaluate the expression and put result on stack.
Write a program that evaluates the postfix expression. Create your own stack implementation.
/ / The code under the scanner an expression
/ / Fetch the operands and operators from the expression
/ / expression is the string expression
/ / Operandstack is our stack
java.util.StringTokenizer tokens =
new java.util.StringTokenizer (expression, "+-/*%", true);
/ / Phase 1, scan for tokens
while (tokens.hasMoreTokens ()) {
String token = tokens.nextToken (). Trim () / / extract a token
if (token.length () == 0) {/ / characters
continue; / / next token
}
else if (token.charAt (0) == '+' | | token.charAt (0) == '-' | |
token.charAt (0) == '*' | | token.charAt (0) == '/') {
/ / When an operator is found, we can apply it on operands
/ / my code, a call to a method?
}
else {/ / A operand found
/ / Add the stack
}
}
Im sitting here very clueless and could need some help