Write a java program Converts infix expression into Postfix form

//Program for infix to postfix conversion
import java.util.*;
class itop1
{
            char s[]=new char[10];
            int top;
            itop1() {top=-1; }
           
            void i2post(char in[])
            {
                        int i,j=0;
                        char post[]=new char[20];
                        char ichar,schar;
                        for(i=0;i<in.length;i++)
                        {
                                    ichar=in[i];
                                    if(isoperand(ichar)) post[j++]=ichar;
                                    else
                                    {
                                                while(!isempty() && (val(ichar)<=val(topsmb())) && (ichar!='(‘))
                                                {
                                                            schar=pop();
                                                            if(schar=='(‘) break;
                                                            if(ichar!='(‘) post[j++]=schar;
                                                }
                                    if(ichar!=’)’)  push(ichar);
                                    }
                        }
                        while(!isempty()) post[j++]=pop();
                        for(i=0;i<j;i++)
                        if((post[i]!='(‘) && (post[i]!=’)’))
                        System.out.print(post[i]);
            }
           
            boolean isoperand(char x)
            {
                        return((x>=’0′ && x<=’9′) || (x>=’a’ && x<=’z’) || (x>=’A’ && x<=’Z’));
            }
            boolean isempty()
            {
                        return (top==-1);
            }
           
            int val(char x)
            {
                        switch(x)
                        {
                                    case ‘)’ :
                                    case ‘(‘ : return 0;
                                    case ‘+’:
                                    case ‘-‘ : return 1;
                                    case ‘*’:
                                    case ‘/’ : return 2;
                                    case ‘$’: return 3;
                                    default :System.out.println(“nInvalid expression !”+(char)7);
                                                 System.exit(1);
                                    return 0;
                        }
            }
            char topsmb()
            {                                 
                        return (s[top]);
            }
            void push(char x)
            {
                        top++;            
                        s[top]=x;
            }
            char pop()
            {
                        return (s[top–]);
            }
}
class Itop
{
            public static void main(String args[])
            {
                        Scanner scr=new Scanner(System.in);
                        itop1 p=new itop1();
                        System.out.println(“nEnter infix expression : “);
                        String str=scr.next();
                        char b[]=str.toCharArray();
                        System.out.println(“nPost fix expression for “+str+” is :”);
                        p.i2post(b);
            }
}
Output:
Enter infix expression :
(a+b)
Post fix expression for (a+b) is :

ab+

Leave a Reply

Your email address will not be published. Required fields are marked *

Enable Notifications OK No thanks