Arithmetic evaluation: Difference between revisions

Content added Content deleted
(→‎{{header|Tailspin}}: add directly calculated solution)
m (→‎{{header|Phix}}: simplified popfactor)
Line 4,520: Line 4,520:


procedure PopFactor()
procedure PopFactor()
object p2 = opstack[$]
object p1, p2 = opstack[$]
if op="u-" then
if op="u-" then
p1 = 0
if op_p_p=1 then -- op_p_p
opstack[$] = {op,0,p2}
elsif op_p_p=0 then -- p_op_p
opstack[$] = {0,op,p2}
else -- -1 -- p_p_op
opstack[$] = {0,p2,op}
end if
else
else
opstack = opstack[1..$-1]
opstack = opstack[1..$-1]
p1 = opstack[$]
if op_p_p=1 then -- op_p_p
end if
opstack[$] = {op,opstack[$],p2}
elsif op_p_p=0 then -- p_op_p
if op_p_p=1 then
opstack[$] = {opstack[$],op,p2}
opstack[$] = {op,p1,p2} -- op_p_p
elsif op_p_p=0 then
else -- -1 -- p_p_op
opstack[$] = {opstack[$],p2,op}
opstack[$] = {p1,op,p2} -- p_op_p
end if
else -- -1
opstack[$] = {p1,p2,op} -- p_p_op
end if
end if
op = 0
op = 0
Line 4,665: Line 4,660:
I added a flag (for this task) to store the ast nodes as op_p_p, p_op_p, or p_p_op, whichever you prefer.
I added a flag (for this task) to store the ast nodes as op_p_p, p_op_p, or p_p_op, whichever you prefer.
{{out}}
{{out}}
For "3+4+5+6*7/1*5^2^3", the fully parenthesised Phix equivalent being ((3+4)+5)+(((6*7)/1)*power(5,power(2,3)))
<pre>
<pre>
with op_p_p:
with op_p_p: