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

Thread: RPN Help Please! (Order of Operations)

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default RPN Help Please! (Order of Operations)

    Hi, I'm trying to make a simple calculator with order of operations.
    I had read in the Internet and found the algorithm of RPN (Reverse Polish notation).


    Lets take an example:
    2 * 5 - 3 + 4

    EDIT:

    Ok, I did as you both said check it out now:

    Proc is string array of both numbers and operations. Proc will be {2, *, 5, -, 3, +, 4}

    This is the code:

    EDIT:
     
    Lets take an example: 2 * 5 - 3 + 4
     
    Ok, I did as you both said check it out now:
     
    Proc is string array of both numbers and operations. Proc will be {2, *, 5, -, 3, +, 4}
     
    This is the code:
     
        int tempt = 0;
        Stack <Double> stc = new Stack <Double>();
        while (tempt < proc.length)
        {
            try
            {
                Double num = Double.parseDouble(proc[tempt]);
                stc.push(num);
                tempt++;
            }
            catch (Exception e)
            {
                char [] stcs = proc[tempt].toCharArray();
                switch (stcs[0])
                {
                case '+':
                {
                    double a2 = stc.pop();
                    double a1 = stc.pop();
                    stc.push(a1 + a2);
                    tempt++;
                    break;
                }
                case '-':
                {
                    double a2 = stc.pop();
                    double a1 = stc.pop();
                    stc.push(a1 - a2);
                    tempt++;
                    break;
                }
                case 'x':
                {
                    double a2 = stc.pop();
                    double a1 = stc.pop();
                    stc.push(a1 * a2);
                    tempt++;
                    break;
                }
                case '÷':
                {
                    double a2 = stc.pop();
                    double a1 = stc.pop();
                    stc.push(a1 / a2);
                    tempt++;
                    break;
                }
            }
     
            }
    How can I make it work as well?
    HELP ME PLS!
    Last edited by yuvalb; September 30th, 2011 at 10:26 AM.


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

    Default Re: RPN Help Please! (Order of Operations)

    Do you have an algorithm for solving the problem?
    You need that before you start writing code.
    How are you going to use the contents of the two arrays?

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: RPN Help Please! (Order of Operations)

    Quote Originally Posted by Norm View Post
    Do you have an algorithm for solving the problem?
    You need that before you start writing code.
    How are you going to use the contents of the two arrays?
    Please check out above I had updated it

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

    Default Re: RPN Help Please! (Order of Operations)

    Proc is string array of both numbers and operations. Proc will be {2, *, 5, -, 3, +, 4}
    You've merged the two arrays into one array.
    Now what is your algorithm for processing the contents of the array?

  5. #5

    Default Re: RPN Help Please! (Order of Operations)

    According to your code you are using a stack for the numbers: (first in, last out)

    Stack <Double> stc = new Stack <Double>();
     
        	for (int i=0; i<numsStrings.length; i++)
        	{
        		stc.push(Double.valueOf(numsStrings[i]));
        	}
    But when you parse through the operators, you start from the beginning:
    while (tempt < actionsStrings.length)
        	{
            		char [] stcs = actionsStrings[tempt].toCharArray();
            		switch (stcs[0])
    The switch statement should be stcs[tempt] otherwise it will switch on the first operator indefinitely, however, since you are using an implicit Queue (first in, first out) the operations will not apply to your numbers correctly anyway.

    Use a stack for both:

    Stack <Double> stcD = new Stack <Double>();
    Stack <char> stcc = new Stack <char>();
    switch(stcc.pop())
    {
    	case('+'):
    		double d1 = stcD.pop();
    		double d1 = stcD.pop();
    		stcD.push(d1.add(d2)); //pushing back to the stack will allow you to use the next operator successfully
    		break;
    	case('-'):
    	... //and so on
    }
    Note: This ignores the order of operations, you will have to add some functionality to pop and save elements off until you reach a specified operator in order of operations ()^*/+- perform the operation, push the new value and any values not used as well as operators not used
    Kenneth Walter
    Software Developer
    http://kennywalter.com

  6. The Following User Says Thank You to kenster421 For This Useful Post:

    JavaPF (September 30th, 2011)

  7. #6
    Junior Member
    Join Date
    Sep 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: RPN Help Please! (Order of Operations)

    Quote Originally Posted by kenster421 View Post
    According to your code you are using a stack for the numbers: (first in, last out)


    But when you parse through the operators, you start from the beginning:

    The switch statement should be stcs[tempt] otherwise it will switch on the first operator indefinitely, however, since you are using an implicit Queue (first in, first out) the operations will not apply to your numbers correctly anyway.

    Use a stack for both:

    Stack <Double> stcD = new Stack <Double>();
    Stack <char> stcc = new Stack <char>();
    switch(stcc.pop())
    {
    	case('+'):
    		double d1 = stcD.pop();
    		double d1 = stcD.pop();
    		stcD.push(d1.add(d2)); //pushing back to the stack will allow you to use the next operator successfully
    		break;
    	case('-'):
    	... //and so on
    }
    Note: This ignores the order of operations, you will have to add some functionality to pop and save elements off until you reach a specified operator in order of operations ()^*/+- perform the operation, push the new value and any values not used as well as operators not used
    Please take a look at the editing
    Last edited by yuvalb; September 30th, 2011 at 10:54 AM.

  8. #7

    Default Re: RPN Help Please! (Order of Operations)

    I think the best thing you can do at this point is put aside the code you have already completed and start over. This time, follow the advice and create a plan before you attempt to tackle this problem. Consider using Design Patterns Composite and Visitor to assist you in creating a structure that can be easily parsed.
    Kenneth Walter
    Software Developer
    http://kennywalter.com

Similar Threads

  1. Implementing order of operations for a Java calculator.
    By mjpam in forum Java Theory & Questions
    Replies: 2
    Last Post: May 15th, 2011, 06:11 PM
  2. Unchecked or Unsafe Operations
    By saxophonemaster in forum Collections and Generics
    Replies: 2
    Last Post: April 12th, 2011, 04:40 AM
  3. Help with OS operations!
    By benglish in forum Java Theory & Questions
    Replies: 8
    Last Post: April 1st, 2011, 06:32 AM
  4. Operations with lists
    By datreta in forum Collections and Generics
    Replies: 8
    Last Post: October 29th, 2010, 08:54 AM
  5. unsafe operations note??
    By bookface in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 22nd, 2010, 09:58 AM

Tags for this Thread