アルゴリズム – Is it possible to have an operation with three or more operands in Reverse Polish Notation?

Question: Question:

The intermediate notation "3 + 4" can be written as "3 4 +" in reverse Polish notation.
So how do you write an operand with three or more operands, for example, the intermediate notation "3 + 4 + 5" in reverse Polish notation?
It seems good to write "3 4 + 5 +", but is it possible to write "3 4 5 +"?
(It seems that multiplication can be done in the same way, but I'm not sure what happens with subtraction.)

Answer: Answer:

How should the intermediate notation "3 + 4 + 5" be written in Reverse Polish Notation?

As you mentioned in your question, it can be 3 4 + 5 + or 3 4 5 + + .

Is it possible to write something like "3 4 5 +"?

It depends on what kind of operation + is defined, but if you think of adding 2 numbers according to other parts, if it is 3 4 5 + , only the last 4 5 will be added, and 3 9 It will be in the same state as I wrote it.

If you can define something like "an operation that takes out three numbers from the stack and adds them all together", there is also a way to write 3 4 5 add3 add 3.


A language based on Reverse Polish Notation (although it may be better to say that it is based on stack operations) Forth looks like this. (The line after \ is a line comment.)

: add3 + + ;   \ スタックを3個読み取る演算子(ワード)`add3`の定義
3 4 5 add3     \ `add3`を演算として使う

(result) => 12

Scroll to Top