0% found this document useful (0 votes)
421 views

Infix To PostFix Stack

This document contains code for converting infix expressions to postfix expressions and then evaluating the postfix expression. It includes classes for a Stack, converting expressions to postfix notation, and evaluating postfix expressions. The Stack class implements a stack using a character array. The convert method uses the Stack to convert an infix string to postfix by pushing and popping operators. The Evaluator_Postfix class also uses a stack, but with integers, to evaluate the postfix expression by pushing operands and popping to perform operations.

Uploaded by

Ann Juvie Papas
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
421 views

Infix To PostFix Stack

This document contains code for converting infix expressions to postfix expressions and then evaluating the postfix expression. It includes classes for a Stack, converting expressions to postfix notation, and evaluating postfix expressions. The Stack class implements a stack using a character array. The convert method uses the Stack to convert an infix string to postfix by pushing and popping operators. The Evaluator_Postfix class also uses a stack, but with integers, to evaluate the postfix expression by pushing operands and popping to perform operations.

Uploaded by

Ann Juvie Papas
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Infix to Postfix

Conversion and
Evaluator CODES

-Eivuj Papas
/**
* StackTest.java
*NOTE: I used Stack using char int Array.
* @author ME – [email protected]
* @version 1.00 2010/8/4
*/

import java.io.*;
import java.util.*;

public class StackTest {

public static void main(String[] args) {

String input=" ";


BufferedReader dataIn = new BufferedReader(new InputStreamReader( System.in) );

System.out.println("Enter infix expression operators(+-*/%) and digits(0-9): ");

try{
input=dataIn.readLine();
}catch(IOException w){ System.out.println("Error"+w);}

input=input+")";
Stack stack=new Stack(input.length());
stack.push('(');

String post=stack.convert(input);
StringTokenizer output=new StringTokenizer(post);
System.out.println("\npostfix expression:");

while(output.hasMoreTokens())
{
System.out.print(output.nextToken()+" ");
}

Evaluator_Postfix evaluate=new Evaluator_Postfix(post.length());

int answer=evaluate.produce(post);
System.out.println("\nResult: ");
System.out.println(answer);

System.exit(0);
}

/**
* Stack.java
*NOTE: I used Stack using char int Array.
* @author ME – [email protected]
* @version 1.00 2010/8/4
*/

import java.io.*;
import java.util.*;

public class Stack{

private int size;


private int top;
private char[] values;

public Stack(int size){


this.size = size;
values = new char[size];
top = -1;
}

public void push(char data)


{ if(!isFull()){

top++;
values[top] = data;
}
}

public boolean isFull(){


if(top < size-1){
return false;
}
else{
return true;
}
}

public boolean isEmpty(){


if(top == -1){
return true;
}
else{
return false;
}
}
public char pop()
{
char retVal = ' ';
if(!isEmpty())
{
retVal=values[top];
top--;
}

return retVal;
}

public char stackTop()


{ int temp=top;
char retVal = ' ';

if(!isEmpty()) {
retVal=values[temp];
temp--;
}
return retVal;

public int precedence(char it)


{
if(it=='*'||it=='^')
return 6;

else if(it=='/')
return 5;

else if(it=='%')
return 4;

else if(it=='+')
return3;

else if(it=='-')
return2;

else
return1;
}

public String convert(String in_put)


{
StringBuffer postfix=new StringBuffer(in_put.length());
int i=0;
char c;
String word=" ";

while(i<in_put.length())
{
c=in_put.charAt(i);

if(Character.isDigit(c))
postfix.append(c).append(" ");

if(c=='(')
push('(');

if(c=='*'||c=='/'||c=='%'||c=='+'||c=='-')
{
char v=stackTop();
if(v=='*'||v=='/'||v=='%'||v=='+'||v=='-')
{
if(precedence(v)>=precedence(c))
{
{
postfix.append(pop()).append(" ").toString();
}

}
}
push(c);
}

if(c==')')
{ char st=stackTop();
while(st!='('&&!isEmpty())
{

postfix.append(pop()).append(" ").toString();
st=stackTop();

}
pop();
}

i++;

}
return postfix.toString();

}//end of inner while(token)

public void print()


{
char temp=values[top];
System.out.println("Stack content");
while(!isEmpty())
{
System.out.println("\n"+temp);
temp=values[top--];
}

/**
* Evaluator_Postfix.java
*NOTE: I used Stack using char int Array.
* @author ME – [email protected]
* @version 1.00 2010/8/4
*/
import java.io.*;
import java.util.*;

public class Evaluator_Postfix {

private int size;


private int top;
private int[] values;
private int valEx;
public Evaluator_Postfix(int size){
this.size = size;
values = new int[size];
top = -1;
}

public void push(int data)


{ if(!isFull()){

top++;
values[top] = data;
}
}

public boolean isFull(){


if(top < size-1){
return false;
}
else{
return true;
}
}

public boolean isEmpty(){


if(top == -1){
return true;
}
else{
return false;
}
}
public int pop()
{
int retVal = 0;
if(!isEmpty())
{
retVal=values[top];
top--;
}

return retVal;
}

public int produce(String post_fix) {

post_fix=post_fix+"\0";
int i=0;
int ans;
int x,y;

while(i<post_fix.length())
{
char c=post_fix.charAt(i);

if(Character.isDigit(c))
{
int l=(c-'0');
push(l);

if(c=='*'||c=='/'||c=='%'||c=='+'||c=='-')
{
x=pop();
y=pop();

//calculating

if(c=='*'){
ans=x*y;
push(ans);
}
if (c=='/'){
if(x<y)
{
ans=y/x;
push(ans);
}

else{
ans=x/y;
push(ans);
}
}
if (c=='%'){
ans=x%y;
push(ans);
}
if(c=='+'){
ans=x+y;
push(ans);
}
if(c=='-'){
if(x<y)
{
ans=y-x;
push(ans);
}

else{
ans=x-y;
push(ans);
}
}

if(c=='\0')
{
while(!isEmpty()){
valEx=pop();
}
}
i++;
}

return valEx;
}

Don't forget to say Thank You... You may visit my facebook account and here's my email add:
[email protected]. If Error occurs just debug it and hopefully understand the algorithm. BYE

--THE END--

You might also like