Resistance calculator: Difference between revisions

Content added Content deleted
m (→‎{{header|Phix}}: syntax coloured, made p2js compatible, and a single runnable source, and removed RPN/infix sections)
m (syntax highlighting fixup automation)
Line 48: Line 48:
{{trans|Python}}
{{trans|Python}}
===RPN===
===RPN===
<lang 11l>T Resistor
<syntaxhighlight lang="11l">T Resistor
Float resistance
Float resistance
voltage = 0.0
voltage = 0.0
Line 119: Line 119:
print(‘ Ohm Volt Ampere Watt Network tree’)
print(‘ Ohm Volt Ampere Watt Network tree’)
node.setVoltage(18.0)
node.setVoltage(18.0)
node.report()</lang>
node.report()</syntaxhighlight>


=={{header|CoffeeScript}}==
=={{header|CoffeeScript}}==
===RPN===
===RPN===
<lang coffeescript>nd = (num) -> num.toFixed(3).padStart 8
<syntaxhighlight lang="coffeescript">nd = (num) -> num.toFixed(3).padStart 8


class Resistor
class Resistor
Line 163: Line 163:
node.setVoltage 18.0
node.setVoltage 18.0
print " Ohm Volt Ampere Watt Network tree"
print " Ohm Volt Ampere Watt Network tree"
node.report ""</lang>
node.report ""</syntaxhighlight>


=={{header|Go}}==
=={{header|Go}}==
===Infix===
===Infix===
{{trans|Nim}}
{{trans|Nim}}
<lang go>package main
<syntaxhighlight lang="go">package main


import "fmt"
import "fmt"
Line 239: Line 239:
fmt.Println(" Ohm Volt Ampere Watt Network tree")
fmt.Println(" Ohm Volt Ampere Watt Network tree")
node.report("")
node.report("")
}</lang>
}</syntaxhighlight>


===RPN===
===RPN===
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 339: Line 339:
fmt.Println(" Ohm Volt Ampere Watt Network tree")
fmt.Println(" Ohm Volt Ampere Watt Network tree")
node.report("")
node.report("")
}</lang>
}</syntaxhighlight>


=={{header|Julia}}==
=={{header|Julia}}==
{{trans|Raku}}
{{trans|Raku}}
====Infix====
====Infix====
<lang julia>using Formatting
<syntaxhighlight lang="julia">using Formatting
import Base.+, Base.*
import Base.+, Base.*


Line 408: Line 408:
println(" Ohm Volt Ampere Watt Network tree")
println(" Ohm Volt Ampere Watt Network tree")
report(node)
report(node)
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
Ohm Volt Ampere Watt Network tree
Ohm Volt Ampere Watt Network tree
Line 433: Line 433:


====RPN====
====RPN====
<lang julia>function rpn(arr::Vector)
<syntaxhighlight lang="julia">function rpn(arr::Vector)
stack = Any[]
stack = Any[]
for op in arr
for op in arr
Line 451: Line 451:
setvoltage(node, 18)
setvoltage(node, 18)
report(node)
report(node)
</lang>{{out}}
</syntaxhighlight>{{out}}
Same as infix version.
Same as infix version.


=={{header|Nim}}==
=={{header|Nim}}==
<lang python>import strutils, strformat
<syntaxhighlight lang="python">import strutils, strformat


type
type
Line 504: Line 504:
node.setVoltage voltage
node.setVoltage voltage
node.report
node.report
node</lang>
node</syntaxhighlight>


===RPN===
===RPN===
<lang python>proc rpn(voltage:float, s:string): Node = calculate(voltage, s.split ' ')
<syntaxhighlight lang="python">proc rpn(voltage:float, s:string): Node = calculate(voltage, s.split ' ')
var node = rpn(18, "10 2 + 6 * 8 + 6 * 4 + 8 * 4 + 8 * 6 +")
var node = rpn(18, "10 2 + 6 * 8 + 6 * 4 + 8 * 4 + 8 * 6 +")
assert 10 == node.res
assert 10 == node.res
Line 513: Line 513:
assert 1.8 == node.current()
assert 1.8 == node.current()
assert 32.4 == node.effect()
assert 32.4 == node.effect()
assert '+' == node.kind</lang>
assert '+' == node.kind</syntaxhighlight>


===Infix===
===Infix===
<lang python>proc parse(s: string): seq[string] =
<syntaxhighlight lang="python">proc parse(s: string): seq[string] =
var tmp = ""
var tmp = ""
for ch in s:
for ch in s:
Line 566: Line 566:
assert 1.8 == node.current()
assert 1.8 == node.current()
assert 32.4 == node.effect()
assert 32.4 == node.effect()
assert '+' == node.kind</lang>
assert '+' == node.kind</syntaxhighlight>


=={{header|Perl}}==
=={{header|Perl}}==
===Infix===
===Infix===
{{trans|Raku}}
{{trans|Raku}}
<lang perl>use strict;
<syntaxhighlight lang="perl">use strict;
use warnings;
use warnings;
use feature <say state>;
use feature <say state>;
Line 643: Line 643:


Resistor::set_voltage($node,18);
Resistor::set_voltage($node,18);
say Resistor::report($node);</lang>
say Resistor::report($node);</syntaxhighlight>
{{out}}
{{out}}
<pre style="height:20ex"> Ohm Volt Ampere Watt Network tree
<pre style="height:20ex"> Ohm Volt Ampere Watt Network tree
Line 667: Line 667:


=={{header|Phix}}==
=={{header|Phix}}==
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">RPN</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">true</span> <span style="color: #000080;font-style:italic;">// or false for infix (same output)
<span style="color: #008080;">constant</span> <span style="color: #000000;">RPN</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">true</span> <span style="color: #000080;font-style:italic;">// or false for infix (same output)
Line 802: Line 802:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" Ohm Volt Ampere Watt Network tree\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" Ohm Volt Ampere Watt Network tree\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">report</span><span style="color: #0000FF;">(</span><span style="color: #000000;">node</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">report</span><span style="color: #0000FF;">(</span><span style="color: #000000;">node</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">)</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 829: Line 829:
=={{header|Python}}==
=={{header|Python}}==
===RPN===
===RPN===
<lang python>class Resistor :
<syntaxhighlight lang="python">class Resistor :
def __init__(self, resistance, a=None, b=None, symbol='r'):
def __init__(self, resistance, a=None, b=None, symbol='r'):
self.resistance = resistance
self.resistance = resistance
Line 873: Line 873:
print(" Ohm Volt Ampere Watt Network tree")
print(" Ohm Volt Ampere Watt Network tree")
node.setVoltage(18.0)
node.setVoltage(18.0)
node.report()</lang>
node.report()</syntaxhighlight>


===Infix===
===Infix===
<lang python>class Resistor :
<syntaxhighlight lang="python">class Resistor :
def __init__(self, resistance, a=None, b=None, symbol='r') :
def __init__(self, resistance, a=None, b=None, symbol='r') :
self.resistance = resistance
self.resistance = resistance
Line 915: Line 915:
node.setVoltage(18)
node.setVoltage(18)
print(" Ohm Volt Ampere Watt Network tree")
print(" Ohm Volt Ampere Watt Network tree")
node.report()</lang>
node.report()</syntaxhighlight>


=={{header|Raku}}==
=={{header|Raku}}==
Line 921: Line 921:
===Infix===
===Infix===
{{trans|Nim}}
{{trans|Nim}}
<lang perl6>class Resistor {
<syntaxhighlight lang="raku" line>class Resistor {
has Str $.symbol;
has Str $.symbol;
has Numeric ( $.voltage, $.resistance );
has Numeric ( $.voltage, $.resistance );
Line 972: Line 972:


say ' Ohm Volt Ampere Watt Network tree';
say ' Ohm Volt Ampere Watt Network tree';
$node.report;</lang>
$node.report;</syntaxhighlight>
{{out}}
{{out}}
<pre style="height:20ex"> Ohm Volt Ampere Watt Network tree
<pre style="height:20ex"> Ohm Volt Ampere Watt Network tree
Line 999: Line 999:
{{trans|Go}}
{{trans|Go}}
{{libheader|Wren-fmt}}
{{libheader|Wren-fmt}}
<lang ecmascript>import "/fmt" for Fmt
<syntaxhighlight lang="ecmascript">import "/fmt" for Fmt


class Resistor {
class Resistor {
Line 1,053: Line 1,053:
node.voltage = 18
node.voltage = 18
System.print(" Ohm Volt Ampere Watt Network tree")
System.print(" Ohm Volt Ampere Watt Network tree")
node.report("")</lang>
node.report("")</syntaxhighlight>


{{out}}
{{out}}
Line 1,083: Line 1,083:
Additonally:
Additonally:
{{libheader|Wren-seq}}
{{libheader|Wren-seq}}
<lang ecmascript>import "/fmt" for Fmt
<syntaxhighlight lang="ecmascript">import "/fmt" for Fmt
import "/seq" for Stack
import "/seq" for Stack


Line 1,154: Line 1,154:
node.voltage = 18
node.voltage = 18
System.print(" Ohm Volt Ampere Watt Network tree")
System.print(" Ohm Volt Ampere Watt Network tree")
node.report("")</lang>
node.report("")</syntaxhighlight>


{{out}}
{{out}}
Line 1,162: Line 1,162:


=={{header|zkl}}==
=={{header|zkl}}==
<lang zkl>class Resistor{
<syntaxhighlight lang="zkl">class Resistor{
fcn init(resistance_,symbol_="r", a_=Void, b_=Void){
fcn init(resistance_,symbol_="r", a_=Void, b_=Void){
var resistance,a,b,symbol, voltage=Void;
var resistance,a,b,symbol, voltage=Void;
Line 1,190: Line 1,190:
fcn __opAdd(other){ Resistor(0,"+",self,other) }
fcn __opAdd(other){ Resistor(0,"+",self,other) }
fcn __opMul(other){ Resistor(0,"*",self,other) }
fcn __opMul(other){ Resistor(0,"*",self,other) }
}</lang>
}</syntaxhighlight>
===Infix===
===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);
<syntaxhighlight 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:=((((R8 + R10)*R9 + R7)*R6 + R5)*R4 + R3)*R2 + R1;
node.setVoltage(18);
node.setVoltage(18);
println(" Ohm Volt Ampere Watt Network tree");
println(" Ohm Volt Ampere Watt Network tree");
node.report();</lang>
node.report();</syntaxhighlight>
{{out}}
{{out}}
<pre style="height:15ex">
<pre style="height:15ex">
Line 1,221: Line 1,221:
</pre>
</pre>
===RPN===
===RPN===
<lang zkl>fcn build(rpnStr){
<syntaxhighlight lang="zkl">fcn build(rpnStr){
stack:=List();
stack:=List();
foreach symbol in (rpnStr.split()){
foreach symbol in (rpnStr.split()){
Line 1,240: Line 1,240:
node.setVoltage(18);
node.setVoltage(18);
println(" Ohm Volt Ampere Watt Network tree");
println(" Ohm Volt Ampere Watt Network tree");
node.report();</lang>
node.report();</syntaxhighlight>
{{out}}
{{out}}
<pre style="height:15ex">
<pre style="height:15ex">