Tuesday 21 August 2012

import java.io.*;
class f
{
int i,l,top=-1;
String s;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public void read()throws IOException
{
System.out.print("Enter the string");
s=br.readLine();
}
public void postfix()throws IOException
{
int l=s.length();
for(i=0;i<l;i++)
{
char d=s.charAt(i);
if(d>=0 && d<=9)
{
push(d);
}
else
{char c1=pop();
char c2=pop();
switch (d)
{
case '+':push(c1+c2);break;
case '-':push(c1-c2);break;
case '*':push(c1*c2);break;
case '/':push(c1/c2);break;
//case '':push(c1%c2);break;
}
}
}}
public void push(int e)
{
if (top>=(l-1))
System.out.print("Stack is full");
else
{
top++;
s[top]=e;
}}
char pop()
{
char tp;
tp=s[top];
top--;
return(tp);

}
}
class InFixPost
{
public static void main (String args[])throws IOException
{
f t=new f();
t.read();
t.postfix();
}
}




0 comments:

Post a Comment

Blogroll

Blogger templates

About