2

I'm trying to understand how the RPN calculator should work in the case of the one argument and one operand, for example. divide or multiply.

I know how it should work in simple cases, eg.

> 1 
1
> 3
3
> +
result: 4  

explanation:  1 + 3 = 4

It's obvious how it works

The case 2 is harder but also pretty clear

7 2 3 * −
result: 1 
explanation:  7 - (2 * 3) = 1

So I know how it works basically.

I'm interested in these use cases.

4 -
result: -4

So in the case of a single argument, it should transform a number into a negative form. In the case of '+' we won't do anything

But how should it behave in these cases?

4 /

or

4 * 

Should I directly do a math operation with the same number? eg:

4 /   ===  4 / 4
4 *   ===  4 * 4

Thanks for any help!

P.S. Sorry for the stupid question but it's the first time when I faced this thing

UPDATE: Also, how about the use case when the user enters incorrect data. Eg. something like this?

1 + 3 - 5 * 3 /

By default, it ends the process or doesn't allow the user to continue to enter the incorrect data until the correct and valid argument be entered?

Velidan
  • 5,526
  • 10
  • 48
  • 86
  • 1
    I believe `4, - = 0 - 4 = -4`. So `4,/ = 0 /4 = 0` and `4,* = 0 * 4 = 0` – ariel Apr 01 '20 at 20:49
  • Hi @ariel, thank you for your answer. I also think so but I'd like to know if this behavior normal for the standard RPN calculator? I just want to implement things the same like in the case of original – Velidan Apr 02 '20 at 11:19

1 Answers1

1

With RPN, you have usually some values in the registers/stack.

As well as you get a CHS key for changing the sign.

If you like to make an own calculator, you could specify the function as you want, as you like to take - as operator and for changing the sign.

If you have only one value at the stack, you could omit binary operators and allow only unary operators.

Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • Hi @Nina. Thanks for your answer. Basically I want to implement my own calculator but I saw it could handle the `4 -` and covert it into negative value `-4` so I wonder how it should behave in the case of `4 /` or `4 *` by default? I mean - the normal, standard rpn calculator. Should it handle these cases or not? – Velidan Apr 02 '20 at 11:17
  • the *normal* calculator (like HP-15) have always values in the registers. The other ones, with a stack, could be empty. if you like to use a sctack, you could omit/ignore binary operators. – Nina Scholz Apr 02 '20 at 11:36
  • Could you, please, explain this one: _With RPN, you have usually some values in the registers/stack_. So do you mean that we always should have 2 arguments in the normal RPN and only then we can use some operand? Sorry but I barely understand you. Have never seen the rpn before, so sorry please. So, basically I understand that in my particular situation you suggest to use only *-* or *+* operators but forget about "/* or "*". Is it correct? Or should I follow the @ariel suggestion? because I'm confused a bit right now – Velidan Apr 02 '20 at 15:00
  • you could go with zero as value for binary operators, as @ariel suggests. – Nina Scholz Apr 02 '20 at 15:05
  • Thank you for your help. Will do. – Velidan Apr 02 '20 at 15:10
  • Nina, could you also, please, check my update? if you have time, for sure. I'd really appreciate it – Velidan Apr 03 '20 at 07:18
  • About, how it behaves in the case of incorrect input. eg: 1 + 3 - 5 * 3 / – Velidan Apr 03 '20 at 07:21
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/210849/discussion-between-nina-scholz-and-velidan). – Nina Scholz Apr 03 '20 at 07:23