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

1. ## 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!

2. ## 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. ## Re: RPN Help Please! (Order of Operations)

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?

4. ## 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. ## 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

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

JavaPF (September 30th, 2011)

7. ## Re: RPN Help Please! (Order of Operations)

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:

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

8. ## 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.