Write a java program evaluates the postfix expression

//Evaluating the postfix expression
import java.util.*;
class postfix1
{
            double a[]=new double[20];
            int top;
            postfix1() { top=-1; }
           
            void push(double x)
            {         
                        top++;
                        a[top]=x;
            }
           
            double pop()
            {
                        if(top==-1) { System.out.println(“nImproper expression !”+(char)7); System.exit(1); }
                        return a[top–];
            }
           
            boolean isdigit(char x)
            {
                        return(x>=’0′ & x<=’9′);
            }
           
            double val(double v1,double v2,char x)
            {
                        switch(x)
                        {
                                    case ‘+’: return v1+v2;
                                    case ‘-‘: return v1-v2;
                                    case ‘*’: return v1*v2;
                                    case ‘/’: return v1/v2;
                                    case ‘$’: return (Math.pow(v1,v2));
                                    default : System.out.println(“Improper Operation”);
                                                  System.exit(1);
                        }
                        return 1;
            }
            double eval(char b[])
            {
                        double v1,v2,v3,v;                 
                        for(int i=0;i<b.length;i++)
                        {
                                    if(isdigit(b[i]))                                    
                                    push((double)b[i]-48);
                                    else
                                    {
                                                v2=pop();                                           
                                                v1=pop();                                           
                                                v=val(v1,v2,b[i]);                                           
                                                push(v);
                                    }
                                                 
                        }
                         if(top!=0) { System.out.println(“nImproper expression”+(char)7); System.exit(1); }
                        return pop();
            }
}
class Postfix
{
            public static void main(String args[])
            {
                        postfix1 p=new postfix1();
                        Scanner scr=new Scanner(System.in);
                        System.out.println(“nEnter the post fix expression : “);
                        String str=scr.next();
                        char b[]=str.toCharArray();                
                        System.out.print(“nValue of the expression is : “+p.eval(b));
                       
            }
}
Output:
Enter the postfix expression :
23+43+-

Value of the expression is : -2.0

Leave a Reply

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

Enable Notifications OK No thanks