Anonymous user
Resistance calculator: Difference between revisions
Updated to compile with version 1.4. Removed tabulations, added parentheses. Removed useless imports, other minor modifications.
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:
=={{header|Nim}}==
<lang python>import
type
proc res(node
proc current(node
proc effect (node
proc report(node
proc setVoltage(node
proc build(tokens
proc calculate(voltage: float, tokens: seq[string]): Node =
===RPN===
<lang python>proc rpn(voltage:float, s:string): Node = calculate(voltage, s.split ' ')
var node = rpn
assert 10 == node.res
assert 18 == node.voltage
Line 517:
===Infix===
<lang python>proc parse(s: string): seq[string] =
of ' ':
if tmp!="": result.add tmp▼
of '+', '*', '(', ')':
▲ tmp=""
▲ else: tmp &= ch
if tmp!="": result.add tmp▼
tmp &= ch
proc shuntRPN(s: string): seq[string] =
while stack.len > 0:▼
if op == "(": break
result.add op▼
▲ else:
else:
while stack.len > 0:▼
if ops.find(token) >= ops.find(op): break
▲ stack.add token
else: result.add token
proc infix(voltage:float, s:string): Node = calculate(voltage, shuntRPN s)
node = infix
assert 10 == node.res
assert 18 == node.voltage
|