Skip to main content

Program to Convert Infix Expression into PostFix Expression


/*----Program to Convert Infix Expression into PostFix Expression---*/


/*  Theory :In conversion of infix expression to postfix expression infix expression a+b in this expression a & b are operand and + is a opertor while converting a will be copied to new expression callsed as postfix and after that + will be copied to the stack the b will be copied to postfix expression and as infix expression has no more elements all the element of stack will copied to postfix expression*/  

import java.io.*;
class MyStack
{
char []a;
int top;
MyStack()
{
a=new char[5];
top=-1;
}
MyStack(int size)
{
a=new char[size];
top=-1;
}
boolean empty() // Empty Method
{
return (top==-1)?true:false;
}
void push(char ele)
{
if(top<a.length-1)
a[++top]=ele;
else
System.out.println("\n\t\t Stack OverFlow");
}     // End Of PUSH Method
char pop()
{
if(!empty())
return a[top--];
else
return (char)-1;
}     // End Of POP Method
} //End Of MyStack Class
class In2PostExample
{
String exp;
In2PostExample()  
{
exp=new String("a+b");
}
In2PostExample(String s)
{
exp=new String (s);
}
int precedence(char a)  //Start of precedent method
{
switch(a)
{
case '+':
return 1;
case '-':
return 1;
case '*':
return 2;
case '/':
return 2;
}
return 0;

} //End of precedent method

  /* --------------------------------------Start of convert method--------------------------------------------*/

String convert()
{
char[] pexpr=new char[exp.length()+1];
char c,sc='\0';
int i,j;
MyStack st=new MyStack(exp.length());
for(i=0,j=0;i<exp.length();i++)
{
c=exp.charAt(i);
switch(c)
{
case '+':
case '-':
case '*':
case '/':

 /*Checking the precedence of operator in stack and the operator that has been taken from infix expression*/
while(!st.empty() && precedence(sc=st.pop())>=precedence(c))
pexpr[j++]=sc;
st.push(sc);
st.push(c);
break;
case '(':
st.push(c);
break;
case ')':
while((sc=st.pop())!='(')
pexpr[j++]=sc;
break;
default:
pexpr[j++]=c;
}
}

while(!st.empty())
if((sc=st.pop())!='(')
pexpr[j++]=sc;
return (new String(pexpr));
}

public static void main(String args[])throws IOException  //Start of main method
{
System.out.print("\n\t\t Enter the Infix Expression:: ");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str=br.readLine();
In2PostExample infix=new In2PostExample(str);
String p=infix.convert();
System.out.println("\n\t\t The Postfix String is:: "+p); //Printing Postfix string
} //End of main method

}       //End of main class

***** OUTPUT ****** 

Comments

Popular posts from this blog

Login and Registration Example in JSP with Session

Those who want to start with jsp and MySQL, this is an excellent example for themselves. Here you can learn how to insert data to MySQL using JSP. Also you can learn about session handling in jsp. 1 2 3 4 5 6 7 8 9 10 CREATE TABLE `members` (    `id` int (10) unsigned NOT NULL auto_increment,    `first_name` varchar (45) NOT NULL ,    `last_name` varchar (45) NOT NULL ,    `email` varchar (45) NOT NULL ,    `uname` varchar (45) NOT NULL ,    `pass` varchar (45) NOT NULL ,    `regdate` date NOT NULL ,    PRIMARY KEY   (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; index.jsp 1 2 3 4 5 6 ...

Timer funcitons in JavaScript: Reminder Script

Timers are used in web pages and we will discuss how to setup one and how to use one with an example. The function setTimeout() in function is used and here is its general syntax. mytime = setTimeout(expression, msec); mytime  is the identifier used to identify the current timeout function.  expression  is the statement that is to be executed after the specified time has ticked off.  msec  is the duration of time in milliseconds after which the expression will be executed.  You can see by using setTimeout function we can execute any function or object after a set of time. For example if msec is set 5000 then the expression will be executed after 5 seconds or 5000 milliseconds.  We will try one example where we will have four period buttons and each button will set a different time for another function to execute and display a alert button. We will call it as a reminder script and we will get one alert box based on the period button we click...

Binary Addition

/* File Name : BinAdd.java */    import java.util.*; public class BinAdd    {  public static String addBit(String a, String b, String c)  { String r=""; if(a.equals("1") && b.equals("0") || a.equals("0") && b.equals("1")) { if( c.equals("0")) r="1"; else { r="0"; c="1"; } } else if( a.equals("0") && b.equals("0") ) { if(c.equals("0")) r="0"; else r="1"; } else if( a.equals("1") && b.equals("1") ) { if(c.equals("0")){ r="0"; c="1"; } else { r="1"; c="1"; } } return c+r; }   public static String add(String a, String b)   { String r=""; int len=a.length(); String carry="0"; for(int i=len-1;i...

Real time changing Clock showing date and time

We can display a clock which will be showing the local time of the client computer by using JavaScript. Note that this time displayed is taken from user computer and not from the server.  We have seen in our  date object example how to display current date and time   where the current date and time is displayed once. Here we will try to display changing clock or real time change in date and time by using  setTimeout function . This setTimeout function we have used to trigger the time display function in a refresh rate of 1000 mill seconds ( 1 Sec ). This refresh rate can be changed to any other value. By changing the value to 5000 the clock will refresh and show the exact time once in every 5 seconds.  Here is the demo of this script and code is given below that.  Sat Apr 23 2016 08:27:22 GMT+0530 (IST)   Here is the code <html> <head> <title>(Type a title for your page here)</title> <script type="text/javascript...