Results 1 to 2 of 2
  1. #1
    miatech is offline Member
    Join Date
    May 2012
    Posts
    2
    Rep Power
    0

    Default need help converting from infix to postfix notation

    guys, I have an expression like: 1 + 2 * 3, and I need to convert to postfix notation: 1 2 3 * +

    This is my code, but for some reason I'm not displaying the operators, any help appreciated


    Java Code:
    package postfixstack;
    
    import java.io.*;
    import java.util.*;
    
    public class Main {
        
        static Stack<String> optor = new Stack();
        static Stack<Integer> oprand = new Stack();
        
        static String strOut = "";
        
        public static void main(String[] args) throws FileNotFoundException
        {       
            
            File inFile = new File("datafile.txt");
            
            Scanner in = new Scanner(inFile);
            
            while(in.hasNext())
            {
                String next = in.next();
                do
                {
                    if(optor.isOperator(next))
                    {
                        //process operator
                        processOptor(next);
                    }                
                    else
                    {
                        oprand.push(Integer.parseInt(next));
                        strOut += next+" ";
                        
                    }
                    next = in.next();
                }while(!next.equals("$"));                 
            }
            
            System.out.println(strOut);
            
        }
        
        //processing operators
        public static void processOptor(String op)
        {
            do
            {
                if(optor.isEmpty())
                {
                    optor.push(op);
                    return;
                }
                if(optor.priority(op) > optor.priority(optor.top()))
                {
                    optor.push(op);
                    return;
                }
                if(optor.priority(op) == optor.priority(optor.top()))
                {
                    if(optor.associativity(optor.priority(op)).equals("right"))
                    {
                        optor.push(op);
                        return;
                    }
                }
                System.out.println("here!");
                strOut += optor.top()+" ";
                optor.pop();            
                
            }while(true);
        }
        
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default Re: need help converting from infix to postfix notation

    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Trouble with converting infix to postfix
    By mustachio in forum New To Java
    Replies: 7
    Last Post: 07-21-2011, 03:48 AM
  2. Infix to Postfix using array
    By Franneldort in forum New To Java
    Replies: 0
    Last Post: 10-11-2010, 05:57 PM
  3. Postfix-Notation
    By little_polarbear in forum New To Java
    Replies: 9
    Last Post: 09-09-2008, 04:24 PM
  4. Replies: 1
    Last Post: 07-05-2008, 03:08 PM
  5. Replies: 0
    Last Post: 04-15-2008, 07:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •