JAVA LANGUAGE ONLY
Implement a postfix calculator in Java. I will supply someexamples with correct answers. The goal of the program is to learnhow to create and use a stack data structure not to build a parser.You may not use any of the Java Library routine for building ormanipulating linked lists or stacks. You will need to support somesimple commands, clear the stack (clearing an empty stack is not anerror), print the stack (without making any changes), push anelement onto the stack, pop an element off the stack. Your programwill only need to handle simple integers (unsigned, no floatingpoint, no scientific notation). The only operators we will use aregoing to be +, -, *, and /. Each token will be surrounded by whitespace.
Any integer will be pushed onto the stack.
Any operator will pop the top 2 elements, perform the operation,and push the result back onto the stack.
It is difficult to get Java to distinguish line endings on inputdata so your program will not need to do that. All of my exampleswill start by clearing the stack.
The character c will be used to clear the stack, d will be usedto dump the stack, a will be used to print only the top of thestack..
For example:
5 - 3 is the same as 5 3 -
first push 5
then push 3
perform the subtraction (yielding 2)
push 2 onto the stack.
4 3 + 5 * is 35. (4+3)*5
4 3 5 * + is 19. 4+(3*5)
If we code the examples above:
c 5 3 2 a
c 4 3 + 5 * a
c 4 3 5 * + a
Which your program will see as c 5 3 2 a c 4 3 + 5 * a c 4 3 5 *+ a
Hints:
If you use an array to hold your stack make sure you understandwhat top of stack is.
When you print the stack, mark which end is the top.
If you want to test your answers find a simulator for an HP-10Ccalculator.
You will never need to put operators on the stack, onlyintegers.
JAVA LANGUAGE ONLY Implement a postfix calculator in Java. I will supply some examples with correct answers. The goal of
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am