-6

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).

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;
            }
        }

        }

STILL DOESNT WORK

How can I make it work as well? HELP ME PLS!

YuvalB
  • 1
  • 1
  • 4

1 Answers1

4

You've got the algorithm wrong. 2 * 5 - 3 + 4 in RPN translates to: 2 5 * 3 - 4 +. I don't know why you are treating numbers and symbols independently in two separate lists: in Reverse Polish notation:

2 3 + 4 * === (2 + 3) * 4

while

2 3 4 + * === 2 * (3 + 4)

That being said your program is almost correct except that for input you should take a series of symbols (both values and operators). Now you read symbols from left to right. If it is a number, push it onto the stack. If operator: pop top two values and push the result. That's it!


UPDATE: Example

Input: 2 5 * 3 - 4 +

Stack: []

Iteration I: (reading 2 from input)

Input: 5 * 3 - 4 +

Stack: [2]

Iteration II: (reading 5 from input)

Input: * 3 - 4 +

Stack: [2, 5]

Iteration III: (reading * from input)

Input: 3 - 4 +

Stack: [2 * 5] == [10]

Iteration IV: (reading 3 from input)

Input: - 4 +

Stack: [10, 3]

Iteration V: (reading - from input)

Input: 4 +

Stack: [10 - 3] == [7]

Iteration VI: (reading 4 from input)

Input: +

Stack: [7, 4]

Iteration VII: (reading + from input)

Input: ``

Stack: [7 + 4] == [11]

Result: 11 (no further input, one and only element on the stack is the result)


UPDATE 2: C'mon!

You are writing a program to interpret RPN but you are feeding it with infix notation! Try with this input:

String[] proc = new String[]{"2", "5", "x", "3", "-", "4", "+"};

There are several other flaws in your code (duplication, exception driven flow control, no error handling), but essentially with this input it should work.

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
  • Hi, I still don't get it. Can you explain further pls? – YuvalB Sep 30 '11 at 15:03
  • See my update, it's really simple. – Tomasz Nurkiewicz Sep 30 '11 at 15:14
  • Because you have two different collections for operators and numbers, it's impossible to tell when to apply the operators to the numbers. – James Cronen Sep 30 '11 at 15:15
  • Ok, I did as you both said check it out now: – YuvalB Sep 30 '11 at 15:18
  • Please check out the edit of the main thread – YuvalB Sep 30 '11 at 15:21
  • I found the problem but I can't solve it. When I pop 2 values from stack at first it has only 1 first value, so it crash. I mean it push the 2 to the stack and then trying to pop 2 value in order to make an operation. double a2 = stc.pop(); double a1 = stc.pop(); stc.push(a1 - a2); – YuvalB Sep 30 '11 at 15:34
  • I have an inteface that you press buttons and it add them to the string. It is adding them in infix notation (I think..) How can I convert them to the other kind? – YuvalB Sep 30 '11 at 15:50
  • I mean my array is : String[] proc = new String[]{2, *, 5, -, 3, +, 4}; Yours is a bit different.. String[] proc = new String[]{"2", "5", "x", "3", "-", "4", "+"}; – YuvalB Sep 30 '11 at 15:52
  • Do you understand the difference between RPN and infix notation? Your program correctly parses and evaluates RPN, not infix notation. If your user inputs operations in infix notation, why are you writing and asking about a program to evaluate RPN? – Tomasz Nurkiewicz Sep 30 '11 at 16:03
  • I just want to be able to calculate the final result as well considering the order of operations in math (* comes before + and so on..) Any suggestions? – YuvalB Sep 30 '11 at 16:06
  • If you want to evaluate "`2 + 2 * 2`" to `6` than either search the Internet how to convert from infix to postfix (since you can already evaluate postfix/RPN notation) or search how to evaluate infix notation directly. – Tomasz Nurkiewicz Sep 30 '11 at 16:17
  • I had already did, but no results. Maybe can you help me pls? – YuvalB Sep 30 '11 at 16:20