# RPN Help Please! (Order of Operations)

• September 30th, 2011, 09:27 AM
yuvalb
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:

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!
• September 30th, 2011, 10:22 AM
Norm
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?
• September 30th, 2011, 10:25 AM
yuvalb
Re: RPN Help Please! (Order of Operations)
Quote:

Originally Posted by Norm
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?

• September 30th, 2011, 10:30 AM
Norm
Re: RPN Help Please! (Order of Operations)
Quote:

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?
• September 30th, 2011, 10:31 AM
kenster421
Re: RPN Help Please! (Order of Operations)
According to your code you are using a stack for the numbers: (first in, last out)

Quote:

Code :

```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:
Quote:

Code :

```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:

Code :

```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
• September 30th, 2011, 10:39 AM
yuvalb
Re: RPN Help Please! (Order of Operations)
Quote:

Originally Posted by kenster421
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:

Code :

```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
• September 30th, 2011, 10:53 AM
kenster421
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.