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: 666103 • 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.

Commented Code Please , Running screen shot if u can

Explanation / Answer

/* program to convert infix notation to postfix notation */

import java.io.*;

class stack

{

            char stack1[]=new char[20];

            int top;

            void push(char ch)

            {

                        top++;

                        stack1[top]=ch;

            }

            char pop()

            {

                        char ch;

                        ch=stack1[top];

                        top--;

                        return ch;

            }

            int pre(char ch)

            {

                        switch(ch)

                        {

                                    case '-':return 1;

                                    case '+':return 1;

                                    case '*':return 2;

                                    case '/':return 2;

                        }

                        return 0;

            }

            boolean operator(char ch)

            {

                        if(ch=='/'||ch=='*'||ch=='+'||ch=='-')

                                    return true;

                        else

                                    return false;

            }

            boolean isAlpha(char ch)

            {

                        if(ch>='a'&&ch<='z'||ch>='0'&&ch=='9')

                                    return true;

                        else

                                    return false;

            }

            void postfix(String str)

            {

                        char output[]=new char[str.length()];

                        char ch;

                        int p=0,i;

                        for(i=0;i<str.length();i++)

                        {

                                    ch=str.charAt(i);         

                                    if(ch=='(')

                                    {

                                                push(ch);

                                    }

                                    else if(isAlpha(ch))

                                    {

                                                output[p++]=ch;

                                    }

                                    else if(operator(ch))

                                    {

                                                if(stack1[top]==0||(pre(ch)>pre(stack1[top]))||stack1[top]=='(')

                                    {

                                                push(ch);

                                    }

                                    }

                                    else if(pre(ch)<=pre(stack1[top]))

                                    {

                                                output[p++]=pop();

                                                push(ch);

                                    }

                                    else if(ch=='(')

                                    {

                                                while((ch=pop())!='(')

                                                {

                                                            output[p++]=ch;

                                                }

                                    }

                        }

                        while(top!=0)

                        {

                                    output[p++]=pop();

                        }

                        for(int j=0;j<str.length();j++)

                        {

                                    System.out.print(output[j]);   

                        }

            }

}

class intopost

{

            public static void main(String[] args)throws Exception

            {

                        String s;

                        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

                        stack b=new stack();

                        System.out.println("Enter input string");

                        s=br.readLine();

                        System.out.println("Input String:"+s);

                        System.out.println("Output String:");

                        b.postfix(s);

            }

}

Output:

Enter input string
a+b*c
Input String:a+b*c
Output String:
abc*+

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