Resistance calculator: Difference between revisions

m (→‎{{header|Python}}: added zkl header)
(→‎{{header|zkl}}: added code)
Line 537:
 
=={{header|zkl}}==
<lang zkl></lang>class Resistor{
fcn init(resistance_,symbol_="r", a_=Void, b_=Void){
<lang zkl></lang>
var resistance,a,b,symbol, voltage=Void;
resistance,symbol,a,b = vm.arglist;
resistance=resistance.toFloat(); // deal with strings/ints
}
fcn res{
if (symbol=="+") a.res() + b.res();
else if(symbol=="*") 1.0/(1.0/a.res() + 1.0/b.res());
else resistance
}
fcn setVoltage(voltage){
if(symbol=="+"){
ra,rb := a.res(), b.res();
a.setVoltage(ra/(ra + rb)*voltage);
b.setVoltage(rb/(ra + rb)*voltage);
}
else if(symbol=="*") T(a,b).apply2("setVoltage",voltage);
self.voltage = voltage.toFloat();
}
fcn current{ voltage/res() }
fcn effect { current()*voltage }
fcn report(level=""){
println("%8.3f %8.3f %8.3f %8.3f %s%s".fmt(res(),voltage,current(),effect(),level,symbol));
T(a,b).apply2("report",level + "| "); // noop if Void
}
fcn __opAdd(other){ Resistor(0,"+",self,other) }
fcn __opMul(other){ Resistor(0,"*",self,other) }
}</lang>
===Infix===
<lang zkl>R1,R2,R3,R4,R5,R6,R7,R8,R9,R10 := T(6,8,4,8,4,6,8,10,6,2].apply(Resistor);
node:=((((R8 + R10)*R9 + R7)*R6 + R5)*R4 + R3)*R2 + R1;
node.setVoltage(18);
println(" Ohm Volt Ampere Watt Network tree");
node.report();</lang>
{{out}}
<pre style="height:15ex">
<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===
<lang zkl>fcn build(rpnStr){
stack:=List();
foreach symbol in (rpnStr.split()){
if(symbol=="+"){
a,b:=stack.pop(),stack.pop();
stack.append(Resistor(0,"+",b,a))
}
else if(symbol=="*"){
a,b:=stack.pop(),stack.pop();
stack.append(Resistor(0,"*",b,a))
}
else stack.append(Resistor(symbol,"r"));
}
stack.pop() // unevaluated top of circuit
}
node:=build("10 2 + 6 * 8 + 6 * 4 + 8 * 4 + 8 * 6 +");
node.setVoltage(18);
println(" Ohm Volt Ampere Watt Network tree");
node.report();</lang>
{{out}}
<pre style="height:15ex">
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===
Anonymous user