THE ZEPINT NETWORK

programmer assist

Java Java XML Feeds

Java Questions Java Solutions Java Articles

Java is an object-oriented programming language developed initially by James Gosling and colleagues at Sun Microsystems. The language, initially called Oak (named after the oak trees outside Gosling's office), was intended to replace C++, although the feature set better resembles that of Objective C. Java should not be confused with JavaScript, which shares only the name and a similar C-like syntax. Sun Microsystems currently maintains and updates Java regularly.

infix to postfix, infix to prefix

DiggBlinkRedditDeliciousTechnorati

question by abel | Moderate

The conversion from infix to postfix expression and infix to prefix expression

Post reply Subscriptions

Re: infix to postfix, infix to prefix

answer by Srirangan

Converts infix arithmetic expressions to postfix

import java.io.IOException;

public class InToPost {
private Stack theStack;

private String input;

private String output = "";

public InToPost(String in) {
input = in;
int stackSize = input.length();
theStack = new Stack(stackSize);
}

public String doTrans() {
for (int j = 0; j < input.length(); j++) {
char ch = input.charAt(j);
switch (ch) {
case '+':
case '-':
gotOper(ch, 1);
break; // (precedence 1)
case '*': // it's * or /
case '/':
gotOper(ch, 2); // go pop operators
break; // (precedence 2)
case '(': // it's a left paren
theStack.push(ch); // push it
break;
case ')': // it's a right paren
gotParen(ch); // go pop operators
break;
default: // must be an operand
output = output + ch; // write it to output
break;
}
}
while (!theStack.isEmpty()) {
output = output + theStack.pop();

}
System.out.println(output);
return output; // return postfix
}

public void gotOper(char opThis, int prec1) {
while (!theStack.isEmpty()) {
char opTop = theStack.pop();
if (opTop == '(') {
theStack.push(opTop);
break;
}// it's an operator
else {// precedence of new op
int prec2;
if (opTop == '+' || opTop == '-')
prec2 = 1;
else
prec2 = 2;
if (prec2 < prec1) // if prec of new op less
{ // than prec of old
theStack.push(opTop); // save newly-popped op
break;
} else
// prec of new not less
output = output + opTop; // than prec of old
}
}
theStack.push(opThis);
}

public void gotParen(char ch){
while (!theStack.isEmpty()) {
char chx = theStack.pop();
if (chx == '(')
break;
else
output = output + chx;
}
}

public static void main(String[] args) throws IOException {
String input = "1+2*4/5-7+3/6";
String output;
InToPost theTrans = new InToPost(input);
output = theTrans.doTrans();
System.out.println("Postfix is " + output + '
');

}
class Stack {
private int maxSize;

private char[] stackArray;

private int top;

public Stack(int max) {
maxSize = max;
stackArray = new char[maxSize];
top = -1;
}

public void push(char j) {
stackArray[++top] = j;
}

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

public char peek() {
return stackArray[top];
}

public boolean isEmpty() {
return (top == -1);
}
}

}

Post reply Subscriptions

Re: infix to postfix, infix to prefix

reply by sammy

can u give me a simplier java program in converting infix to prefix

Post reply Subscriptions

Got a Java Question?

Just Sign Up and ask the top Java experts!

Search via Google

User Login

Email Address

Password

Java Experts

Rank Expert Points
#1 Srirangan 100
This a list of the Top Java experts, how many points do you have?

Leading Experts

Rank Expert Points
#1 frankzzsword 4600
#2 Bejaan 2900
#3 csfreak 1100
#4 Anurag 700
#5 keyvez 700
#6 nnarasimha 600
#7 Nakata 600
#8 martinig 600
#9 mastercomputers 400
#10 Huntress 150
#11 Adkron 150
#12 Yogesh 100
#13 lexxwern 100
#14 Mustan Khan 100
#15 poizn 100
This is a list of overall best performing experts, how many points do you have?