EVALUATE POSTFIX NOTATION

import java.io.*; class PostFix {  public static void main(String args[])throws IOException  {   BufferedReader br = new Buffere...

import java.io.*;
class PostFix
{
 public static void main(String args[])throws IOException
 {
  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  System.out.println("ENTER ARRAY SIZE");
  int n=Integer.parseInt(br.readLine());
  int st[]=new int[n];
  int ch,top=-1,res=0;
  do
  {
    System.out.println("ENTER YOUR CHOICE");
    System.out.println("1:OPERAND 2: OPERATOR");
    ch = Integer.parseInt(br.readLine());
    switch(ch)
    {
       case 1:
       System.out.println("ENTER OPERAND");
      st[++top]= Integer.parseInt(br.readLine());
      break;

      case 2:
      System.out.println("ENTER OPERATOR");
      String s=br.readLine();
      char op=s.charAt(0);
     int a=st[top--];
     int b=st[top--];
     switch(op)
     {
      case '+':
      res=b+a;
     break;

     case '-':
     res=b-a;
     break;

     case '*':
     res=b*a;
     break;

    case '/':
    res=b/a;
    break;

    case '%':                                                                                                                                                    res=b%a;
     break;
   }
  st[++top]=res;
  System.out.println("result="+res);
  break;
  }
  }while(ch<=3);
  }
  }
                                                                                   
OUTPUT:
                                               
ENTER ARRAY SIZE
5
ENTER YOUR CHOICE
1:OPERAND 2: OPERATOR
1
ENTER OPERAND
5
ENTER YOUR CHOICE
1:OPERAND 2: OPERATOR
1
ENTER OPERAND
6
ENTER YOUR CHOICE
1:OPERAND 2: OPERATOR
1
ENTER OPERAND
2
ENTER YOUR CHOICE
1:OPERAND 2: OPERATOR
2
ENTER OPERATOR
+
result=8
ENTER YOUR CHOICE
1:OPERAND 2: OPERATOR
2
ENTER OPERATOR
*
result=40
ENTER YOUR CHOICE

1:OPERAND 2: OPERATOR

Related

Data Structures 2582850195365130170

Post a Comment

emo-but-icon

item