Resistance calculator: Difference between revisions

m (→‎Infix: show less)
Line 550:
8.000 7.200 0.900 6.480 | | | r
6.000 10.800 1.800 19.440 | | r</pre>
 
=={{header|Phix}}==
<lang Phix>-- node contents:
enum KIND, -- '+', '*', or 'r'
RESISTANCE, VOLTAGE,
A, B -- nested nodes or NULL
function resistance(sequence node)
switch node[KIND] do
case '+': return resistance(node[A]) + resistance(node[B])
case '*': return 1 / (1/resistance(node[A]) + 1/resistance(node[B]))
case 'r': return node[RESISTANCE]
default: ?9/0 -- unknown node kind
end switch
end function
function setVoltage(sequence node, atom voltage)
switch node[KIND] do
case '+':
atom ra := resistance(node[A]),
rb := resistance(node[B])
node[A] = setVoltage(node[A], ra / (ra + rb) * voltage)
node[B] = setVoltage(node[B], rb / (ra + rb) * voltage)
case '*':
node[A] = setVoltage(node[A],voltage)
node[B] = setVoltage(node[B],voltage)
end switch
node[VOLTAGE] = voltage
return node
end function
function current(sequence node)
return node[VOLTAGE] / resistance(node)
end function
function effect(sequence node)
return current(node) * node[VOLTAGE]
end function
procedure report(sequence node, string level="")
printf(1,"%8.3f %8.3f %8.3f %8.3f %s%c\n", {resistance(node), node[VOLTAGE], current(node), effect(node), level, node[KIND]})
if node[A]!=NULL then
report(node[A],level & "| ")
end if
if node[B]!=NULL then
report(node[B],level & "| ")
end if
end procedure
function push(sequence stack, string tok)
switch tok do
case "+","*": sequence b = stack[$],
a = stack[$-1]
stack = stack[1..$-1]
stack[$] = {tok[1], 0, 0, a, b}
default: integer {{r}} = scanf(tok,"%d")
stack = append(stack,{'r', r, 0, NULL, NULL})
end switch
return stack
end function</lang>
===RPN===
<lang Phix>function rpn(string s)
sequence stack = {},
tokens = split(s)
for i=1 to length(tokens) do
stack = push(stack,tokens[i])
end for
return stack[$]
end function
sequence node = rpn("10 2 + 6 * 8 + 6 * 4 + 8 * 4 + 8 * 6 +")
node = setVoltage(node,18)
printf(1," Ohm Volt Ampere Watt Network tree\n")
report(node,"")</lang>
{{out}}
<pre>
Ohm Volt Ampere Watt Network tree
10.000 18.000 1.800 32.400 +
4.000 7.200 1.800 12.960 | *
8.000 7.200 0.900 6.480 | | +
4.000 3.600 0.900 3.240 | | | *
8.000 3.600 0.450 1.620 | | | | +
4.000 1.800 0.450 0.810 | | | | | *
12.000 1.800 0.150 0.270 | | | | | | +
4.000 0.600 0.150 0.090 | | | | | | | *
12.000 0.600 0.050 0.030 | | | | | | | | +
10.000 0.500 0.050 0.025 | | | | | | | | | r
2.000 0.100 0.050 0.005 | | | | | | | | | r
6.000 0.600 0.100 0.060 | | | | | | | | r
8.000 1.200 0.150 0.180 | | | | | | | r
6.000 1.800 0.300 0.540 | | | | | | r
4.000 1.800 0.450 0.810 | | | | | r
8.000 3.600 0.450 1.620 | | | | r
4.000 3.600 0.900 3.240 | | | r
8.000 7.200 0.900 6.480 | | r
6.000 10.800 1.800 19.440 | r
</pre>
===infix===
slightly trickier
<lang Phix>constant ops = {"+","*"}
function infix(string s)
string lastnum = ""
sequence tokens = {}
for i=1 to length(s) do
integer ch = s[i]
if ch>='0' and ch<='9' then
lastnum &= ch
else
if length(lastnum) then
tokens = append(tokens,lastnum)
lastnum = ""
end if
tokens = append(tokens,ch&"")
end if
end for
if length(lastnum) then
tokens = append(tokens,lastnum)
end if
sequence stack = {}, result = {}
for i=1 to length(tokens) do
string token = tokens[i], op
switch token do
case "(": stack = append(stack,token)
case ")": while true do
op = stack[$]
stack = stack[1..$-1]
if op == "(" then break end if
result = push(result,op)
end while
else:
integer tp = find(token,ops)
if tp then
while length(stack) do
op = stack[$]
integer sp = find(op,ops)
if not sp or tp>=sp then exit end if
stack = stack[1..$-1]
result = push(result,op)
end while
stack = append(stack,token)
else
result = push(result,token)
end if
end switch
end for
for i=length(stack) to 1 by -1 do
result = push(result,stack[i])
end for
return result[1]
end function
 
sequence node = infix("((((10+2)*6+8)*6+4)*8+4)*8+6")</lang>
then as per last 3 lines of RPN, same output.
 
=={{header|Python}}==
7,796

edits