• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

RPN Square Root

Status
Not open for further replies.
Level 8
Joined
Oct 9, 2011
Messages
101
Hello guys, I'm studying java and I recently discovered the RPN, I was wondering how do add a square root in an RPN?


EDIT: Figured it out XD hehe

EDIT: I THOUGHT I FIGURED IT OUT help
JavaScript:
public static double SolvePostfix(String[] tokens) {
        double returnValue = 0;
        String operators = "+-*/^%";

        Stack<String> stack = new Stack<String>();

        for (String t : tokens) {
            if (!operators.contains(t)) {
                stack.push(t);
            } else {double a = Double.valueOf(stack.pop());
               
                               
                                    double b = Double.valueOf(stack.pop());
                                    switch (t) {
                                        case "+":
                                                stack.push(String.valueOf(a + b));
                                                break;
                                        case "-":
                                                stack.push(String.valueOf(b - a));
                                                break;
                                        case "*":
                                                stack.push(String.valueOf(a * b));
                                                break;
                                        case "/":
                                                stack.push(String.valueOf(b / a));
                                                break;
                                        case "^":
                                                stack.push(String.valueOf(pow(a,b)));
                                                break;
                                        case "%":
                                                stack.push(String.valueOf(b%a));
                                                break;
                                    }
                           
            }
        }

How do I evaluate unary operators?
 
Last edited:
Level 29
Joined
Jul 29, 2007
Messages
5,174
You need a way to differentiate between a normal minus and an unary one. Assuming your tokenizer knows the difference, simply use your own imaginary operator, which the solver then knows to use, by popping only one argument.

I'd do it by having an additional list for all unary operators (which, again, are any character you want), and after you get the first argument, check if the operator is unary or not, and respond appropriately.
 
Level 8
Joined
Oct 9, 2011
Messages
101
That's why I did, If token == 'symbol here' then ( a = pop then return a(sqrt) ) but it gives me an empty stack exception I don't know what I did wrong

EDIT: lets say the new symbol is x, infix: x ( 9 * 6), the postfix looks like this 6 9 * x when I print it. Is this correct?
 
Level 8
Joined
Oct 9, 2011
Messages
101
I figured out why it doesn't work. It seems that the square root symbol can't be read, I replaced it with s and now it works.

EDIT: Now my problem is, What precedence and associativity will I give to the unary operators

EDIT 2: How can I make: x ( ( 1 - c ( 30 ) ) / 2 ) to postfix: 1 30 c - 2 / x, it gives me this postfix: 1 30 2 / c - x
 
Last edited:
Status
Not open for further replies.
Top