Resistance calculator: Difference between revisions
Content added Content deleted
Alextretyak (talk | contribs) m (→{{header|11l}}) |
(Updated to compile with version 1.4. Removed tabulations, added parentheses. Removed useless imports, other minor modifications.) |
||
Line 455: | Line 455: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
<lang python>import |
<lang python>import strutils, strformat |
||
type |
type |
||
Node = ref object |
|||
kind: char # + = serial * = parallel r = resistor |
|||
resistance: float |
|||
voltage: float |
|||
a: Node |
|||
b: Node |
|||
proc res(node |
proc res(node: Node): float = |
||
if node.kind == '+': return node.a.res + node.b.res |
|||
if node.kind == '*': return 1 / (1 / node.a.res + 1 / node.b.res) |
|||
node.resistance |
|||
proc current(node |
proc current(node: Node): float = node.voltage / node.res |
||
proc effect (node |
proc effect (node: Node): float = node.current * node.voltage |
||
proc report(node |
proc report(node: Node, level: string = "") = |
||
echo fmt"{node.res:8.3f} {node.voltage:8.3f} {node.current:8.3f} {node.effect:8.3f} {level}{node.kind}" |
|||
if node.kind in "+*": |
|||
node.a.report level & "| " |
|||
node.b.report level & "| " |
|||
proc setVoltage(node |
proc setVoltage(node: Node, voltage: float) = |
||
node.voltage = voltage |
|||
if node.kind == '+': |
|||
let ra = node.a.res |
|||
let rb = node.b.res |
|||
node.a.setVoltage ra / (ra+rb) * voltage |
|||
node.b.setVoltage rb / (ra+rb) * voltage |
|||
if node.kind == '*': |
|||
node.a.setVoltage voltage |
|||
node.b.setVoltage voltage |
|||
proc build(tokens |
proc build(tokens: seq[string]): Node = |
||
var stack: seq[Node] |
|||
for token in tokens: |
|||
stack.add if token == "+": Node(kind: '+', a: stack.pop, b: stack.pop) |
|||
elif token == "*": Node(kind: '*', a: stack.pop, b: stack.pop) |
|||
else: Node(kind: 'r', resistance: parseFloat(token)) |
|||
stack.pop |
|||
proc calculate(voltage:float, tokens:seq[string]): Node = |
proc calculate(voltage: float, tokens: seq[string]): Node = |
||
echo "" |
|||
echo " Ohm Volt Ampere Watt Network tree" |
|||
let node = build tokens |
|||
node.setVoltage voltage |
|||
node.report |
|||
node</lang> |
|||
===RPN=== |
===RPN=== |
||
<lang python>proc rpn(voltage:float, s:string): Node = calculate(voltage, s.split ' ') |
<lang python>proc rpn(voltage:float, s:string): Node = calculate(voltage, s.split ' ') |
||
var node = rpn |
var node = rpn(18, "10 2 + 6 * 8 + 6 * 4 + 8 * 4 + 8 * 6 +") |
||
assert 10 == node.res |
assert 10 == node.res |
||
assert 18 == node.voltage |
assert 18 == node.voltage |
||
Line 517: | Line 517: | ||
===Infix=== |
===Infix=== |
||
<lang python>proc parse(s: string): seq[string] = |
<lang python>proc parse(s: string): seq[string] = |
||
var tmp = "" |
|||
for ch in s: |
|||
case ch |
|||
of ' ': |
|||
⚫ | |||
if tmp != "": result.add tmp |
|||
⚫ | |||
continue |
|||
if ch in "+*()": |
|||
of '+', '*', '(', ')': |
|||
if tmp != "": result.add tmp |
|||
⚫ | |||
⚫ | |||
result.add $ch |
|||
⚫ | |||
⚫ | |||
⚫ | |||
tmp &= ch |
|||
⚫ | |||
proc shuntRPN(s:string): seq[string] = |
proc shuntRPN(s: string): seq[string] = |
||
let ops = "+*" |
|||
var tokens = parse s |
|||
var stack: seq[string] |
|||
var op: string |
|||
for token in tokens: |
|||
case token |
|||
of "(": |
|||
⚫ | |||
of ")": |
|||
⚫ | |||
while stack.len > 0: |
|||
op = stack.pop() |
|||
if op == "(": break |
|||
⚫ | |||
⚫ | |||
⚫ | |||
else: |
|||
if token in ops: |
|||
⚫ | |||
⚫ | |||
op = stack[^1] |
|||
if not (op in ops): break |
|||
if op notin ops: break |
|||
if ops.find(token) >= ops.find(op): break |
|||
discard stack.pop() |
|||
result.add op |
|||
⚫ | |||
⚫ | |||
stack.add token |
|||
else: result.add token |
|||
while stack.len > 0: result.add stack.pop() |
|||
⚫ | |||
proc infix(voltage:float, s:string): Node = calculate(voltage, shuntRPN s) |
proc infix(voltage:float, s:string): Node = calculate(voltage, shuntRPN s) |
||
node = infix |
node = infix(18, "((((10+2)*6+8)*6+4)*8+4)*8+6") |
||
assert 10 == node.res |
assert 10 == node.res |
||
assert 18 == node.voltage |
assert 18 == node.voltage |