Program : To convert polish notation to infix notation.
Algorithm
Step 1: Start.
Step 2: Create a thread ct.
Step 3: Create class post implements Runnable.
Step 4: Start executing thread.
Step 5: Stop.
Function public void run()
Step 1: Start.
Step 2: Read the string s1.
Step 3: Store string length to len.
Step 4: Get characters in string s1 to array pfx.
Step 5: Set i=len-1, Set j=len-1.
Step 6: Repeat step 7-12 until i>=0,decrementing i by 1.
Step 7: If pfx[i]=+,-,*,/,%,^, go to step 8, else to 9.
Step 8: top++, Stack[top]=pfx[i].
Step 9: res[j]=pfx[i],j=j-1.
Step 10: If top>0, go to step 11, else to step 13.
Step 11: res[j]=stack[top].
Step 12: j=j-1,i=i-1.
Step 13: Print res[].
Step 14: Exit.
Program
import java.io.*; import java.lang.*; import java.util.*; class Infix implements Runnable { byte stack[]; int top; byte in[]; byte out[]; int len; Infix(String s) { super(); s.trim(); len=s.length(); stack=new byte[len]; in=new byte[len]; out=new byte[len]; in=s.getBytes(); top=-1; } void push(byte b) { stack[++top]=b; } byte pop() { return(stack[top--]); } public void run() { int i=0,outidx=0; byte ch; while(i<len) { ch=in[i]; switch(ch) { case '+': case '-': case '*': case '/': case '%': case '^': out[outidx++]=pop(); out[outidx++]=ch; break; default: push(ch); } i++; } if(top!=-1) out[len-1]=pop(); } String GetInfix() { int i; StringBuffer s=new StringBuffer(""); for(i=0;i<len;i++) s.append((char)out[i]); s.reverse(); return (s.toString()); } } public class MyInfix { public static void main(String[] args) { DataInputStream in=new DataInputStream(System.in); String si,so; try { System.out.println("Enter Polish Notation : "); si=in.readLine(); Infix inf=new Infix(si); Thread th=new Thread(inf,"Polish Thread"); Thread ct=Thread.currentThread(); th.start(); while(th.isAlive()){} so=inf.GetInfix(); System.out.println("Main thread name : " + ct.getName()); System.out.println("Child thread name : " + th.getName()); System.out.println("Polish Notation : " + si); System.out.print("Infix : " + so); } catch(Exception e) { System.out.println("Error : "+ e); } } } |
Output
Enter Polish Notation :
ABCD^+*EF^GH/*-
Main thread name : main
Child thread name : Polish Thread
Polish Notation : ABCD^+*EF^GH/*-
Infix : A-E*G/H^F*B+C^D