Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: need help converting from infix to postfix notation

  1. #1
    Member
    Join Date
    Mar 2009
    Posts
    91
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default need help converting from infix to postfix notation

    guys, I need to convert from infix to postfix notation. I'm reading from a file containing the following example

    input: 2 + 3 ( 4 - 3 ) * 10 $

    $ = eol

    output: 2 3 4 3 - 10 * +

    I'm inserting operands in one stack and operators in another stack; however, I'm having trouble devising how to display the operands... here's what I got so far

    public class Main {
     
        static Stack<String> optor = new Stack();
        static Stack<Integer> oprand = new Stack();
     
        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));
                        System.out.print(next);
                    }
                    next = in.next();
                }while(!next.equals("$"));                 
            }     
     
        }
     
        //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(optor.top());
                optor.pop();            
     
            }while(true);
        }
     
    }
    Last edited by mia_tech; June 27th, 2012 at 11:49 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: need help converting from infix to postfix notation

    Do you have an algorithm/design for what you are trying to do? What are the steps in the algorithm that you are having problems coding.

    Can you post the program's output and explain what is wrong with it?

    The posted code does not compile because it is missing the import statements.
    Last edited by Norm; June 26th, 2012 at 07:30 PM.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Mar 2009
    Posts
    91
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: need help converting from infix to postfix notation

    Quote Originally Posted by Norm View Post
    Do you have an algorithm/design for what you are trying to do? What are the steps in the algorithm that you are having problems coding.

    Can you post the program's output and explain what is wrong with it?

    The posted code does not compile because it is missing the import statements.
    mine does have the import statements, I just forgot to put them in the post. The problem with my output is that I'm only printing the numbers out, I can't figure out why I'm not displaying the operators

    input 1 + 2

    run:
    1 2 BUILD SUCCESSFUL (total time: 0 seconds)

    look at the code above I made a small mod in the main method in the else statement there's a System.out.print that it wasn't before....

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: need help converting from infix to postfix notation

    Sorry, I can't do anything without a version that compiles, executes and shows the problems.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Mar 2009
    Posts
    91
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: need help converting from infix to postfix notation

    Quote Originally Posted by Norm View Post
    Sorry, I can't do anything without a version that compiles, executes and shows the problems.
    Here are the other two classes needed for this program, and you should have a text file datafile.txt with a simple expression like 1 + 2 $
    package postfixstack;
     
    public class Node<T> {
     
        T value;
        Node next;
     
        //constructor
        public Node(T a)
        {
            value = a;
            next = null;
        }    
    }

    package postfixstack;
     
     
    public class Stack<T> {
     
        public Node<T> head;
     
        //constructor
        public Stack()
        {
            head = null;
        }
     
        //adding to stack
        public void push(T a)
        {
            Node p = new Node(a);
            p.next = head;
            head = p;
        }
     
        //removing from stack
        public void pop()
        {
            head = head.next;
        }
     
        //getting top element
        public T top()
        {
            return head.value;
        }
     
        public boolean isEmpty()
        {
            return head == null;
        }
     
        //dispaly stack
        public void display()
        {
            if(head == null)
                System.out.println("Stack is empty!...");
            else
            {
                Node s = head;
                while(s != null)
                {
                    System.out.println(s.value);
                    s = s.next;
                }
            }
        }
     
        //checking for operators
        public boolean isOperator(String s)
        {
            String operator = "-+*/^()";
            if(operator.indexOf(s) != -1)
                return true;
            else
                return false;
        }
     
        //returning level of priority
        public int priority(String s)
        {
            if(s.equals("-") || s.equals("+"))
                return 1;
            if(s.equals("*") || s.equals("/"))
                return 2;
            else
                return 3;       
        }
     
        //return associativity (left or right)
        public String associativity(int level)
        {               
            if(level <= 2)
                return "left";
            else
                return "right";            
        }
     
    }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: need help converting from infix to postfix notation

    with a simple expression like 1 + 2 $
    What does the code output now?
    What should it output?

    For easier testing change the code to:
       Scanner in = new Scanner("1 + 2 $");

    I don't see any description of the algorithm the code is supposed to be following.
    Are you trying to find a valid algorithm
    Or do you have a valid algorithm that you are trying to write the code for?
    If the second case, please post the algorithm.
    If the first case, you need to find a valid algorithm BEFORE writing code.
    Last edited by Norm; June 27th, 2012 at 01:38 PM.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: need help converting from infix to postfix notation

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

  8. #8
    Member
    Join Date
    Mar 2009
    Posts
    91
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: need help converting from infix to postfix notation

    Quote Originally Posted by Norm View Post
    Thanks man!... I got it working! you've been very helpful

    cheers

Similar Threads

  1. [SOLVED] EVALUATION OF POSTFIX NOTATION
    By Medo Almasry in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 7th, 2011, 05:24 PM
  2. POSTFIX NOTATION USING STACKS... [EXAM IN 4 HRS HELP!!!]
    By Medo Almasry in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 23rd, 2011, 12:16 AM
  3. [SOLVED] evaluating postfix expressions using stack
    By lieles in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 16th, 2011, 11:48 AM
  4. [SOLVED] Help with prefix and postfix(increment&decrement)
    By Lokesh in forum Object Oriented Programming
    Replies: 1
    Last Post: February 12th, 2011, 10:20 AM
  5. convert infix to postfix
    By tina G in forum Algorithms & Recursion
    Replies: 2
    Last Post: March 11th, 2010, 01:46 AM