Infix to postfix algorithm
I need som help to finish my algoritm.
This is what I have so far:
import java.util.Stack;
import javax.swing.*;
public class InfixToPostfix {
public static void main(String[] args) {
Object slutt= "#";
char[] operator = {'^','*','/','+','-'};
charvParentes = '(';
charhParentes = ')';
char[] uOperator = {'^','*','/','+','-','('};
char[] opPri= {'^','*','/','+','-','(',')','#'};
int [] infixPri = { 3 , 2 , 2 , 1 , 1 , 4 , 0 , 0 };
int [] opStackPri = { 3 , 2 , 2 , 1 , 1 , 0 ,-1 , 0 };
String infixStr = JOptionPane.showInputDialog("infix : ");
infixStr+= slutt;
String postfixStr= "";
System.out.println("Infix: "+infixStr);
Stack stack = new Stack();
System.out.println ("Stack is empty: "+stack.empty ());
char c;
for (int i=0; i<infixStr.length(); i++) {
c = infixStr.charAt(i);
if (c == '+' || c == '-' || c == '*' || c == '/') {
if (stack.empty()) {
stack.push(String.valueOf(c));
}
else {
// HELP WANTED!!!!!
}
}
else if (c == '#') {
System.out.println("Postfix: "+ postfixStr);
System.exit(0);
}
else {
postfixStr += c;
}
}
}
}
In the "help wanted" else-loop I want to do the following:
The operator from stack should pop, and
it must be compared with the next operator
from the infix-expression, in according to the
priority which is given in the opPri, infixPri and opStackPri.
The if the infix-operator has a higher priority then I push it
to the stack, else the stack-operator pops and are added
to the postfix-string, and the next stack operator is popped and
compared with the same infix-operator. Then again if the infix has
a higher value I push it to the stack, else the stack-operator is popped
and added. So on and so on...
As you see, I know how it should function, but I am not sure at all
about the syntax. Can anyone please help me with this?
thanks!
torvald helmer>

