문제

I know how RPN works, i.e. we have input:

a + b * c

and output is

bc*a+

Is it easy to create algorithm which shows me what is the last computation of this equation? I mean that I want to know that first operation is + on two sentences: "a" and "b*c".

Numerical equations are not connected with my problem but with logical sentences. For example I have a logical sentence:

p&(q|r)

and I need to divide this firstly to

p,(q|r) with &operator

and second sentence:

q,r with |operator

I need to create some kind of parser or something? Is it possible to achieve it relatively easy?

도움이 되었습니까?

해결책

The question is somewhat unclear, but I try to answer the 'is it easy to create an algorithm which shows me what is the last computation'.

Think of rpn as a stack based method of calculation: so bc*a+ means push (the value of) b on the stack, push c on the stack, apply * to the two topmost elements and push the result on the top of the stack. Then push a on the stack and apply + to the two topmost elements and push the result on the stack. So the last computation an rpn formula does is the right-most operator.

Logical sentences are exacty the same thing, just replace * with & and + with |.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top