Jump to content

Resistance calculator: Difference between revisions

Added Wren
m (→‎{{header|Perl}}: Fix link: Perl 6 --> Raku)
(Added Wren)
Line 907:
8.000 7.200 0.900 6.480 | | | r
6.000 10.800 1.800 19.440 | | r</pre>
 
=={{header|Wren}}==
===Infix===
{{trans|Go}}
{{libheader|Wren-fmt}}
<lang ecmascript>import "/fmt" for Fmt
 
class Resistor {
construct new(symbol, resistance, voltage, a, b) {
_symbol = symbol
_resistance = resistance
_voltage = voltage
_a = a
_b = b
}
 
symbol { _symbol }
resistance { _resistance }
voltage { _voltage}
 
res {
if (_symbol == "+") return _a.res + _b.res
if (_symbol == "*") return 1 / (1/_a.res + 1/_b.res)
return _resistance
}
 
current { _voltage / res }
effect { current * _voltage }
 
voltage=(v) {
if (_symbol == "+") {
var ra = _a.res
var rb = _b.res
_a.voltage = ra / (ra + rb) * v
_b.voltage = rb / (ra + rb) * v
} else if (_symbol == "*") {
_a.voltage = v
_b.voltage = v
}
_voltage = v
}
 
report(level) {
Fmt.lprint("$8.3f $8.3f $8.3f $8.3f $s$s", [res, _voltage, current, effect, level, symbol])
if (_a != null) _a.report(level + "| ")
if (_b != null) _b.report(level + "| ")
}
 
+(other) { Resistor.new("+", 0, 0, this, other) }
*(other) { Resistor.new("*", 0, 0, this, other) }
}
 
var r = List.filled(10, null)
var resistances = [6, 8, 4, 8, 4, 6, 8, 10, 6, 2]
for (i in 0..9) r[i] = Resistor.new("r", resistances[i], 0, null, null)
var node = ((((r[7]+r[9])*r[8]+r[6])*r[5]+r[4])*r[3]+r[2])*r[1] + r[0]
node.voltage = 18
System.print(" Ohm Volt Ampere Watt Network tree")
node.report("")</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>
 
===RPN===
Additonally:
{{libheader|Wren-seq}}
<lang ecmascript>import "/fmt" for Fmt
import "/seq" for Stack
 
class Resistor {
construct new(symbol, resistance, voltage, a, b) {
_symbol = symbol
_resistance = resistance
_voltage = voltage
_a = a
_b = b
}
 
symbol { _symbol }
resistance { _resistance }
voltage { _voltage}
 
res {
if (_symbol == "+") return _a.res + _b.res
if (_symbol == "*") return 1 / (1/_a.res + 1/_b.res)
return _resistance
}
 
current { _voltage / res }
effect { current * _voltage }
 
voltage=(v) {
if (_symbol == "+") {
var ra = _a.res
var rb = _b.res
_a.voltage = ra / (ra + rb) * v
_b.voltage = rb / (ra + rb) * v
} else if (_symbol == "*") {
_a.voltage = v
_b.voltage = v
}
_voltage = v
}
 
report(level) {
Fmt.lprint("$8.3f $8.3f $8.3f $8.3f $s$s", [res, _voltage, current, effect, level, symbol])
if (_a != null) _a.report(level + "| ")
if (_b != null) _b.report(level + "| ")
}
 
+(other) { Resistor.new("+", 0, 0, this, other) }
*(other) { Resistor.new("*", 0, 0, this, other) }
}
 
var build = Fn.new { |rpn|
var st = Stack.new()
for (token in rpn.split(" ")) {
if (token == "+") {
var b = st.pop()
var a = st.pop()
st.push(Resistor.new("+", 0, 0, a, b))
} else if (token == "*") {
var b = st.pop()
var a = st.pop()
st.push(Resistor.new("*", 0, 0, a, b))
} else {
var r = Num.fromString(token)
st.push(Resistor.new("r", r, 0, null, null))
}
}
return st.pop()
}
 
var node = build.call("10 2 + 6 * 8 + 6 * 4 + 8 * 4 + 8 * 6 +")
node.voltage = 18
System.print(" Ohm Volt Ampere Watt Network tree")
node.report("")</lang>
 
{{out}}
<pre>
Same as Infix version
</pre>
 
=={{header|zkl}}==
9,485

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.