Jump to content

Resistance calculator: Difference between revisions

Updated to compile with version 1.4. Removed tabulations, added parentheses. Removed useless imports, other minor modifications.
(Updated to compile with version 1.4. Removed tabulations, added parentheses. Removed useless imports, other minor modifications.)
Line 455:
 
=={{header|Nim}}==
<lang python>import tables,strutils,sequtils,sugar, strformat
 
type
Node = ref object
kind kind: char # + = serial * = parallel r = resistor
resistance resistance: float
voltage voltage: float
a a: Node
b b: 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 : Node) : float = return node.voltage / node.res
proc effect (node : Node) : float = return node.current * node.voltage
 
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 : 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 : seq[string]) : Node =
var stack : seq[Node]
for token in tokens:
if stack.add if token == "+": stack.add Node(kind : '+', a : stack.pop, b : stack.pop)
elif token == "*": stack.add Node(kind : '*', a : stack.pop, b : stack.pop)
else: stack.add else: Node(kind : 'r', resistance : parseFloat(token))
stack.pop
 
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===
<lang python>proc rpn(voltage:float, s:string): Node = calculate(voltage, s.split ' ')
var node = rpn (18.0, "10 2 + 6 * 8 + 6 * 4 + 8 * 4 + 8 * 6 +")
assert 10 == node.res
assert 18 == node.voltage
Line 517:
===Infix===
<lang python>proc parse(s: string): seq[string] =
var tmp = ""
for ch in s:
if ch == ' ':case ch
of ' ':
if tmp!="": result.add tmp
if tmp != "": result.add tmp
tmp = ""
continue
if ch in "+*()":
of '+', '*', '(', ')':
if tmp != "": result.add tmp
tmp=""
else: tmp &= ch""
result.add fmt"{$ch}"
else: tmp &= ch
else:
if tmp!="": result.add tmp
tmp &= ch
if tmp != "": result.add tmp
 
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 "(": stack.add token
stack.add token
of ")":
while stack.len > 0:
op = while stack.pop()len > 0:
if op == "stack.pop(": break)
if op == "(": break
result.add op
if tmp!="": result.add tmpop
else:
else:
if token in ops:
while stack.len > 0:
while stack.len > 0:
op = stack[^1]
if not (op in ops): break
if ops.find(token)op >=notin ops.find(op): break
if ops.find(token) >= ops.find(op): break
discard stack.pop()
result.add op
result.add op
stack.add token
else: result stack.add token
else: result.add token
 
while stack.len > 0: result.add stack.pop()
while stack.len > 0: result.add stack.pop()
 
proc infix(voltage:float, s:string): Node = calculate(voltage, shuntRPN s)
node = infix (18.0, "((((10+2)*6+8)*6+4)*8+4)*8+6")
assert 10 == node.res
assert 18 == node.voltage
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.