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 2 of 2

Thread: Reverse Polish Notation Calculator

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Reverse Polish Notation Calculator

    Here is my code for what i have done using an array stack.
    there are no errors but my program doesn't return the final value when I enter "="
    import cs2.*;
    import java.util.Scanner;
     
    public class ArrayStack<T> implements Stack<T>
      {
     
        private T[] data;
     
        private int topIndex;
     
        @SuppressWarnings("unchecked")
        public ArrayStack (int initSize)
          { data = (T[])new Object[initSize];
            topIndex = -1;
          } // Constructor
     
        public ArrayStack ()
          { this(100);
          } // Constructor
     
          public void push (T item)
     
    // Push the new item onto an ArrayStack
    { if (topIndex >= data.length-1)
    throw new NoSpaceAvailableException();
    data[++topIndex] = item;
    } // push
     
     
    public T pop () // Pop item off top of stack
    { if (topIndex < 0)
    throw new EmptyException("stack is empty");
    return data[topIndex--];
    } // pop
     
    public T top () // Return a copy of top item
    { if (topIndex < 0)
    throw new EmptyException("stack is empty");
    return data[topIndex];
    } // top
     
     
    public boolean isEmpty ()
    // Return TRUE if no items on stack
    { return topIndex < 0;
    } // isEmpty
     
    //public static double execute(String expr)
     
     
     
     
          	public static void main(String[] args)
          	{
    ArrayStack<Double> st = new ArrayStack<>();
     
     
    Scanner scan = new Scanner(System.in);
     
     
    Object x = scan.next();
     
     
     
     
            while(scan.hasNext())
            {
                if(scan.hasNextDouble())
                {
                        st.push(scan.nextDouble());
                }
                else
                {
     
                    double a, b;
     
     
     
                            if (x == '+')
                            {a = st.pop();
                            b = st.pop();
                            st.push(b + a);}
     
                            else if (x == '-')
                            {a = st.pop();
                            b = st.pop();
                            st.push(b - a);}
                            else if (x == '*')
                            {a = st.pop();
                            b = st.pop();
                            st.push(b * a);}
     
     
     
                            else if (x == '/')
                            {a = st.pop();
                            b = st.pop();
                            st.push(b / a);}
     
     
                            else if (x == '=')
                            {st.pop();
                            System.out.print("The answer is" + st.pop());
     
                            }
     
                        }
                   }
     
     
     
     
     
          	} //main
     
      }// class ArrayStack
    Last edited by helloworld922; May 9th, 2012 at 12:56 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Reverse Polish Notation Calculator

    what does it return? Post the print out from the program.

    Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Big-O Notation question
    By colerelm in forum Java Theory & Questions
    Replies: 1
    Last Post: November 28th, 2011, 09:52 PM
  2. [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
  3. 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
  4. Parsing Scientific Notation from String
    By aussiemcgr in forum Java Theory & Questions
    Replies: 7
    Last Post: December 14th, 2010, 09:45 AM
  5. Compute the frequency count and big-oh notation for a certain code segment
    By maykel_trinidad in forum Java Theory & Questions
    Replies: 3
    Last Post: November 13th, 2009, 10:23 AM