Infix to Postfix



Program In Java To Convert Infix To Postfix.

The Following Program Reads Infix Operation From User And Works According To Conversion Displays Message Accordingly.

import java.io.*;
class MyStack //Consists Of Stack Functions And Constructors Giving Size To Array
{
    char[] a;
    int top;
    /*
      Constructors
    */
    MyStack() //initialize the stack in a default manner
    {
        a = new char[5];
        top = -1;
    }
    MyStack(int size) //initialize the stack in a said manner
    {
        a = new char[size];
        top = -1;
    }
    /*
      Stack Functions
    */
    boolean empty() {
        return (top == -1) ? true : false;
    }
    void push(char ele) {
        if (top < a.length - 1) a[++top] = ele;
        else System.out.println("\nStack Overflow");
    }
    char pop() {
        if (!empty()) return a[top--];
        else return (char) - 1;
    }
}
class In2Post {
    String expr;
    /*
      Constructors
    */
    In2Post() {
        expr = new String("a+b");
    }
    In2Post(String s) {
        expr = new String(s);
    }
    /*
      Functions
    */
    int precedence(char a) //Giving Operators Their Precedence
    {
        switch (a) {
            case '+':
            case '-':
                return 1;
            case '*':
            case '/':
            case '%':
                return 2;
        }
        return 0;
    }
    String convert() //Converting Infix Operation To Postfix Operation
    {
        char[] pexpr = new char[expr.length() + 1];
        char c, sc = '\0';
        int i, j;
        MyStack st = new MyStack(expr.length());
        for (i = 0, j = 0; i < expr.length(); i++) {
            c = expr.charAt(i);
            switch (c) {
                case '+':
                case '-':
                case '*':
                case '/':
                case '%':
                    while (!st.empty() && precedence(sc = st.pop()) >= precedence(c)) pexpr[j++] = sc;
                    st.push(sc);
                    st.push(c);
                    break;
                case '(':
                    st.push(c);
                    break;
                case ')':
                    while ((sc = st.pop()) != '(') pexpr[j++] = sc;
                    break;
                default:
                    pexpr[j++] = c;
            }
        }
        while (!st.empty()) pexpr[j++] = st.pop();
        return (new String(pexpr));
    }
    /*
      Main Function
    */
    public static void main(String[] arg) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter Your Infix Expression::");
        String s = br.readLine();
        In2Post infix = new In2Post(s);
        String postfix = infix.convert();
        System.out.println("\nPostfix expression is:" + postfix);
    }
}


OUTPUT:

Enter Your Infix Expression::a+b-c*d/e%f+(g-h)

Postfix expression is: ab+cd*e/f%-gh-+
Previous Post Next Post