0% found this document useful (0 votes)
5 views2 pages

Java Postfix Expression Converter

This Java code defines a PostFix class with methods to convert an infix expression to postfix notation by using a stack. It takes an infix string as input, iterates through the characters, and pushes/pops from the stack to output the equivalent postfix expression.

Uploaded by

Akhil Tiwari
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

Java Postfix Expression Converter

This Java code defines a PostFix class with methods to convert an infix expression to postfix notation by using a stack. It takes an infix string as input, iterates through the characters, and pushes/pops from the stack to output the equivalent postfix expression.

Uploaded by

Akhil Tiwari
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

import [Link].

Scanner;

public class PostFix


{

char stack[] = new char[20];


char op[] = new char[20];
int i ;
static int top = -1;

void push(char x)
{
stack[++top]=x;
}

char pop()
{
return(stack[top--]);
}

int precedence(char x)
{
int r;
switch(x)
{
case '+':
case '-': r= 1;
break;
case '*':
case '/': r= 2;
break;
default: r=0;
}
return r;
}
void con(String ip)
{
char c;
for(int j=0;j<[Link]();j++)
{
c = [Link](j);
if(c>='a' && c<='z')
{
op[++i] = c;
}

else
{
switch(c)
{
case '(':push(c);
break;

case ')': while(stack[top]!='(')


{
op[++i]=pop();
}
pop();
break;
case '-' :
case '*' :
case '/' :
case '+' : if(top==-1)
{
push(c);
}

else
{
while(precedence(c) <=
precedence(stack[top]))
{
op[++i]=pop();
if(top==-1)
break;
}
push(c);
}
break;

}
}
}
while(top!=-1)
op[++i]=pop();

for(int t=0;t<[Link];t++)
[Link](op[t]);
}

void read()
{
Scanner in = new Scanner([Link]);
String str = new String();
[Link]("Enter the String: ");
str = [Link]();
[Link]("Postfix Expression is: ");
con(str);

}
public static void main(String[] args)
{
PostFix f = new PostFix();
[Link]();
}
}

You might also like