Program Infix ke Postfix Dengan Java

Program Infix ke Postfix Dengan Java

Kali ini saya akan mengeshare program infix ke postfix dengan menggunakan java. program ini masuk dalam materi Notasi polist yang mana di dalamnya terdapat notasi prefix, infix dan posfix. Berikut listing program infix ke postfix dengan menggunakan java:

<br />
import java.util.Scanner;</p>
<p>class Node {<br />
      public Object data;<br />
      public Node next;<br />
      public Node(){<br />
         data=' ';<br />
         next=null;<br />
      }<br />
      public Node(Object val){<br />
           data=val;<br />
           next=null;<br />
      }<br />
}</p>
<p>public class in_post {<br />
       private Node top;</p>
<p>       public in_post(){<br />
           top=null;<br />
       }<br />
       public boolean empty(){<br />
           return top==null;<br />
       }<br />
       public boolean full(){<br />
           return false;<br />
       }<br />
       public void push(Object e){<br />
            Node temp=new Node(e);<br />
            temp.next=top;<br />
            top=temp;<br />
       }<br />
       public Object pop(){<br />
             Object e=top.data;<br />
             top=top.next;<br />
             return e;<br />
       }<br />
       public Object peek(){<br />
             Object e =  top.data;<br />
             return e;<br />
       }<br />
       public void postfix(String z){<br />
             String output=&quot;&quot;;<br />
             in_post S=new in_post();<br />
             for(int i=0;i<br />
                  char c=z.charAt(i);<br />
                  if(c==('+')||c==('*')||c==('-')||c==('/')){<br />
                     while(!S.empty() &amp;&amp; priority(S.peek()) &gt;= priority(c))<br />
                          output+=S.pop();<br />
                          S.push(c);<br />
                  }<br />
                  else if(c=='('){<br />
                       S.push(c);<br />
                  }<br />
                  else if(c==')'){<br />
                       while(!S.peek().equals('('))<br />
                       output+=S.pop();<br />
                       S.pop();<br />
                  }<br />
                  else<br />
                       output+=c;<br />
             }<br />
             while(!S.empty())<br />
                  output+=S.pop();<br />
                  System.out.println(&quot;Notasi Infix   : &quot;+z);<br />
                  System.out.println(&quot;Notasi Postfix : &quot;+output);<br />
       }</p>
<p>       public int priority(Object z){<br />
              if(z.equals('+')||z.equals('-'))<br />
                   return 1;<br />
              else if(z.equals('*')||z.equals('/'))<br />
                   return 2;<br />
              else<br />
                   return 0;<br />
       }<br />
       public static void main(String args[]){<br />
            Scanner in = new Scanner(System.in);<br />
            in_post post=new in_post();<br />
            System.out.println(&quot;n------------------------------------------&quot;);<br />
            System.out.print(&quot;Inputkan Notasi Infix:&quot;); String input = in.next();<br />
            System.out.println(&quot;------------------------------------------&quot;);</p>
<p>            post.postfix(input);<br />
            System.out.println(&quot;------------------------------------------&quot;);<br />
     }<br />
}<br />

Running program seperti gambar dibawah ini:
 

Share