1

I have to create an application which calculates postfix expressions. I was able to find it online (rosetta code) but it's missing a unary operator. the symbol "~" is supposed to denote the unary operator I tried adding it but unfortunately it didn't work. Please advise;

else if(token.equals("~")){
            System.out.print("Operate\t\t");
            double firstOperand = stack.pop();
            stack.push(firstOperand * -1);
}else
Osama
  • 23
  • 4
  • 1
    "didn't work" deserves an answer like "correct it". A bit more information could help to help. Anyway: negate for **double** would be just `-firstOperand`, no need to use any method of Math. BTW why are you getting **two** values from the stack, only one is needed. – user85421 Mar 11 '19 at 22:01
  • 1
    You will create intrinsic ambiguity if you try and use the same symbol ('-') for binary subtraction and unary negation. –  Mar 11 '19 at 23:03
  • Hey Carlos, like dave said I have to make a distinction. Also reverse polish notation is rather confusing but what you're saying is to write it like this "stack.push(firstOperand * -1);" – Osama Mar 12 '19 at 04:32
  • I just edited it, and I believe its working but I honestly don't know the postfix is (7 5 3 - - ~~~) and the final answer I get is -5. – Osama Mar 12 '19 at 06:10
  • 3-5 = -2, -7 = -9, with three sign reversals = +9. So something is already wrong with your subtraction code. Note that the operands come off the stack in the wrong order for subtract and divide as written in infix. – user207421 Mar 12 '19 at 06:23

2 Answers2

0

The unary ~ is undefined for double values. So its not recommended to support the operator in the following way:

  …
  try {
    tokenNum = Double.parseDouble( token );
  }
  catch( NumberFormatException e ) {
    if( token.startsWith( "~" ) ) {
      tokenNum = (double) ~ Integer.parseInt( token.substring( 1 ) );
      token = tokenNum.toString();
    }
  }
  …

  return( expr.replaceAll( "[^\\^\\*\\~\\+\\-\\d/\\s]", "" ) );  // cleanExpr

For this reason the RPN language 7th does not support the unary ~. You must call eg. 2 not in place of ~2.

Kaplan
  • 11
0

nothing is stopping with aforementioned changes in the rosetta code

evalRPN( "~3 ~4 ~2 * ~1 ~5 - ~2 ~3 ^ ^ / +" );

gives

Input   Operation   Stack after
~3  Push        [-4.0]
~4  Push        [-5.0, -4.0]
~2  Push        [-3.0, -5.0, -4.0]
*   Operate     [15.0, -4.0]
~1  Push        [-2.0, 15.0, -4.0]
~5  Push        [-6.0, -2.0, 15.0, -4.0]
-   Operate     [4.0, 15.0, -4.0]
~2  Push        [-3.0, 4.0, 15.0, -4.0]
~3  Push        [-4.0, -3.0, 4.0, 15.0, -4.0]
^   Operate     [0.012345679012345678, 4.0, 15.0, -4.0]
^   Operate     [1.017262041564032, 15.0, -4.0]
/   Operate     [14.745463201337605, -4.0]
+   Operate     [10.745463201337605]
Final answer: 10.745463201337605

same result as in 7th

ok> 3 not 4 not 2 not * 1 not 5 not - 2 not 3 not y^x y^x / +
ok> .s
› 10,7454632
——
Kaplan
  • 11
  • 2
    He is using `~` to mean unary minus, not complement. Read the question. And don't answer twice. – user207421 Mar 13 '19 at 21:35
  • [_"unary minus" is the name for the symbol in *-a*_](https://unlearningmath.com/2009/05/26/notes-on-operations-unary-minus/) – Kaplan Jul 21 '19 at 09:08