I noticed after i sent in my patch for this that Andy had removed some 
experimental stuff from cvs, that my stuff depends on . So that patch itself is 
not much use anymore. 

Therefore, attached is the new version of FormulaParser.java. Just replace the 
old one and it will compile and run. 

Also inline is the method in class Ptg, to convert infix to postfix. it depends 
on some other changes i made, so you cant compile it, but it gives you an idea 
of what i want to do. 

Anyway, sorry to be sending random stuff out, but i guess i will send in proper 
patches only after the branching has taken place. Till then will work locally, 
and send snippets out on the list for feedback. 

PS. sorry if this is a duplicate post.. the earlier one did not seem to have 
made it thru

/** convert infix order ptg list to rpn order ptg list
     * @return List ptgs in RPN order
     * @param infixPtgs List of ptgs in infix order
     */
    public static List ptgsToRpn(List infixPtgs) {
        java.util.Stack operands = new java.util.Stack();
        java.util.List retval = new java.util.Stack();
        
        java.util.ListIterator i = infixPtgs.listIterator();
        Object p;
        OperationPtg o ;
        boolean weHaveABracket = false;
        while (i.hasNext()) {
            p=i.next();
            if (p instanceof OperationPtg) {
                if (p instanceof ParenthesisPtg) {
                    if (!weHaveABracket) {
                        operands.push(p);
                        weHaveABracket = true;
                    } else {
                        o = (OperationPtg) operands.pop();
                        while (!(o instanceof ParenthesisPtg)) { 
                            retval.add(o);
                        }
                        weHaveABracket = false;
                    }
                } else {
                    
                    while  (!operands.isEmpty() && ((OperationPtg) operands.peek
()).getPrecedence() >= ((OperationPtg) p).getPrecedence() ) { //TODO handle ^ 
since it is right associative
                        retval.add(operands.pop());
                    }
                    operands.push(p);
                }
            } else {
                retval.add(p);
            }
        }
        while (!operands.isEmpty()) {
            if (operands.peek() instanceof ParenthesisPtg ){
                //throw some error
            } else {
                retval.add(operands.pop());
            }   
        }
        return retval;
    }

Reply via email to