Usetutoringspotscode to get 8% OFF on your first order!

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

You can leave a response, or trackback from your own site.

Leave a Reply

Powered by WordPress | Designed by: Premium WordPress Themes | Thanks to Themes Gallery, Bromoney and Wordpress Themes