Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In class, we have discussed how a stack can be used to convert an infix expressi

ID: 666119 • Letter: I

Question

In class, we have discussed how a stack can be used to convert an infix expression to a postfix expression. Once you get a postfix expression you can use another stack to evaluate that postfix expression (provided the postfix expression you created in the first step is correct).

Write a Java program that converts a given infix expression (input) to the corresponding postfix expression (output) and then evaluates the postfix expression to return the answer (output).

Programming: You must use stack to convert to postfix expression and then evaluate it. You may use your own stack class that you might have already created. You are allowed to use the Infix-to-Postfix algorithm supplied on BlazeVIEW. The main() method is responsible to capture the input (infix expression), scan it, check for its correctness, and produce output. Program should not suffer from runtime errors. All exceptions should be handled properly. Especially, if the user has entered an infix expression with wrong number and/or nesting of parentheses your program should show an error message (you may use a stack for this check). You can assume that there will not be any { or [ used in the expression and only +, -, *, / are used as operations.

Example of what your final code should product :

Sample input: (4 * (2 + 3)) / (8 * 5)

Corresponding Output: Postfix expression: 4 2 3 + * 8 5 * / Evaluated value: 0.5

Commented Code Please , Running screen shot if u can

Explanation / Answer

public static int evaluate() {
        int oprnd1;
        int oprnd2;
        int result;
        int finalResult;

        intStack intStack = new intStack(output.length());

        for (int i = 0; i < output.length(); i++)
    {
            char ch = output.charAt(i);

            if (ch >= 48 && ch <= 57) {
                intStack.push(ch);
            }

            else if (ch == '+')
      {
                oprnd1 = intStack.pop() - 48;
                oprnd2 = intStack.pop() - 48;
                result = oprnd1 + oprnd2;
                intStack.push(result);
            }       

            else if (ch == '-')
       {
                oprnd1 = intStack.pop() - 48;
                oprnd2 = intStack.pop() - 48;
                result = oprnd2 - oprnd1;
                intStack.push(result);
            }

            else if (ch == '*')
       {
                oprnd1 = intStack.pop() - 48;
                oprnd2 = intStack.pop() - 48;
                result = oprnd1 * oprnd2;
                intStack.push(result);
            }

            else if (ch == '/') {
                oprnd1 = intStack.pop() - 48;
                oprnd2 = intStack.pop() - 48;
                result = oprnd2 / oprnd1;
                intStack.push(result);
            }
        }

        finalResult = intStack.pop();
        System.out.println(finalResult);
        return finalResult;
    }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote