Postfix Calculator
Postfix is an expression in which the operands are placed before their operators. There are no precedence rules, no parentheses needed. For example, 11 9 – is equal to 11 – 9 in the infix notation. It’s much easier to calculate postfix expression by using the stack data structure.
For this assignment you will be using your own implementation of the generic StackClass from the in-class exercise (fill in all the methods in the attached StackClass.java template). Your calculator program should support float numbers and the following operators: + – * / ^ (all floating point operators).
Your program should have a method that accepts a string of operators and operands separated by spaces (no parentheses). Another function will process a file containing a number of expressions and invoke the calculation method for each line. Your program should should print the final result or output an error if the input is invalid. See below for more details on the input and expected output.
Hints:
There should be exactly one element in the stack, i.e., the result of the expression.
^ operator will not work on floating point numbers, so you may want to import the math library.
If you have an operator but not sufficient operands on the stack, the input expression is invalid. If your stack ends up with more than 1 operand (but no operators) on the stack, the input expression is also invalid.
You may import external libraries for file related operations
The algorithm is given in the following pseudocode:
for each char c in the string:
if c is operand:
stack.push(c)
else if c is operator:
pop operands to x, y
result = x (oper) y
stack.push(result)
Input:
You will read from a file input. Each expression will appear on a line by itself. There will be no empty lines. Operators and operands will be separated by whitespaces. Assume all operators are binary to make parsing easy.
Output:
You are to print the calulation with the expression and the result separated by an equal sign as each expression is evaluated. For example, after evaluating the expression “11 9 -“, your program will print “11 9 – = 2”. If the input expression is invalid, print =ERR=.
Also dont use complicated code
Collepals.com Plagiarism Free Papers
Are you looking for custom essay writing service or even dissertation writing services? Just request for our write my paper service, and we'll match you with the best essay writer in your subject! With an exceptional team of professional academic experts in a wide range of subjects, we can guarantee you an unrivaled quality of custom-written papers.
Get ZERO PLAGIARISM, HUMAN WRITTEN ESSAYS
Why Hire Collepals.com writers to do your paper?
Quality- We are experienced and have access to ample research materials.
We write plagiarism Free Content
Confidential- We never share or sell your personal information to third parties.
Support-Chat with us today! We are always waiting to answer all your questions.