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>

[2216 byte] By [torvald_helmera] at [2007-10-2 3:24:54]
# 1

Hi there, firstly im not sure why u have done infixPri and opStackPri, i dont think those are necessary? I have implemented an infix to postfix algorithm that uses only these operators : + - / *. From the help wanted part what you'd want to do is to do this: while the stack is not empty and a '(' character is not encountered, operators of greater or equal precedence from the stack will be popped then appended to the postfixExp else stop the while. Once the while is finished then u push infix onto the stack. Hope this helps. Cheers

meth0da at 2007-7-15 22:34:21 > top of Java-index,Other Topics,Algorithms...
# 2
Maybe this is what you need http://www.demo2s.com/Code/Java/Collections-Data-Structure/Convertsinfixarithmeticexpressionstopostfix.htm
yinpeng265a at 2007-7-15 22:34:21 > top of Java-index,Other Topics,Algorithms...