Resistance calculator: Difference between revisions

m
syntax highlighting fixup automation
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:
{{trans|Python}}
===RPN===
<langsyntaxhighlight lang="11l">T Resistor
Float resistance
voltage = 0.0
Line 119:
print(‘ Ohm Volt Ampere Watt Network tree’)
node.setVoltage(18.0)
node.report()</langsyntaxhighlight>
 
=={{header|CoffeeScript}}==
===RPN===
<langsyntaxhighlight lang="coffeescript">nd = (num) -> num.toFixed(3).padStart 8
 
class Resistor
Line 163:
node.setVoltage 18.0
print " Ohm Volt Ampere Watt Network tree"
node.report ""</langsyntaxhighlight>
 
=={{header|Go}}==
===Infix===
{{trans|Nim}}
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 239:
fmt.Println(" Ohm Volt Ampere Watt Network tree")
node.report("")
}</langsyntaxhighlight>
 
===RPN===
<langsyntaxhighlight lang="go">package main
 
import (
Line 339:
fmt.Println(" Ohm Volt Ampere Watt Network tree")
node.report("")
}</langsyntaxhighlight>
 
=={{header|Julia}}==
{{trans|Raku}}
====Infix====
<langsyntaxhighlight lang="julia">using Formatting
import Base.+, Base.*
 
Line 408:
println(" Ohm Volt Ampere Watt Network tree")
report(node)
</langsyntaxhighlight>{{out}}
<pre>
Ohm Volt Ampere Watt Network tree
Line 433:
 
====RPN====
<langsyntaxhighlight lang="julia">function rpn(arr::Vector)
stack = Any[]
for op in arr
Line 451:
setvoltage(node, 18)
report(node)
</langsyntaxhighlight>{{out}}
Same as infix version.
 
=={{header|Nim}}==
<langsyntaxhighlight lang="python">import strutils, strformat
 
type
Line 504:
node.setVoltage voltage
node.report
node</langsyntaxhighlight>
 
===RPN===
<langsyntaxhighlight 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 +")
assert 10 == node.res
Line 513:
assert 1.8 == node.current()
assert 32.4 == node.effect()
assert '+' == node.kind</langsyntaxhighlight>
 
===Infix===
<langsyntaxhighlight lang="python">proc parse(s: string): seq[string] =
var tmp = ""
for ch in s:
Line 566:
assert 1.8 == node.current()
assert 32.4 == node.effect()
assert '+' == node.kind</langsyntaxhighlight>
 
=={{header|Perl}}==
===Infix===
{{trans|Raku}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature <say state>;
Line 643:
 
Resistor::set_voltage($node,18);
say Resistor::report($node);</langsyntaxhighlight>
{{out}}
<pre style="height:20ex"> Ohm Volt Ampere Watt Network tree
Line 667:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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)
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: #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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 829:
=={{header|Python}}==
===RPN===
<langsyntaxhighlight lang="python">class Resistor :
def __init__(self, resistance, a=None, b=None, symbol='r'):
self.resistance = resistance
Line 873:
print(" Ohm Volt Ampere Watt Network tree")
node.setVoltage(18.0)
node.report()</langsyntaxhighlight>
 
===Infix===
<langsyntaxhighlight lang="python">class Resistor :
def __init__(self, resistance, a=None, b=None, symbol='r') :
self.resistance = resistance
Line 915:
node.setVoltage(18)
print(" Ohm Volt Ampere Watt Network tree")
node.report()</langsyntaxhighlight>
 
=={{header|Raku}}==
Line 921:
===Infix===
{{trans|Nim}}
<syntaxhighlight lang="raku" perl6line>class Resistor {
has Str $.symbol;
has Numeric ( $.voltage, $.resistance );
Line 972:
 
say ' Ohm Volt Ampere Watt Network tree';
$node.report;</langsyntaxhighlight>
{{out}}
<pre style="height:20ex"> Ohm Volt Ampere Watt Network tree
Line 999:
{{trans|Go}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "/fmt" for Fmt
 
class Resistor {
Line 1,053:
node.voltage = 18
System.print(" Ohm Volt Ampere Watt Network tree")
node.report("")</langsyntaxhighlight>
 
{{out}}
Line 1,083:
Additonally:
{{libheader|Wren-seq}}
<langsyntaxhighlight lang="ecmascript">import "/fmt" for Fmt
import "/seq" for Stack
 
Line 1,154:
node.voltage = 18
System.print(" Ohm Volt Ampere Watt Network tree")
node.report("")</langsyntaxhighlight>
 
{{out}}
Line 1,162:
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">class Resistor{
fcn init(resistance_,symbol_="r", a_=Void, b_=Void){
var resistance,a,b,symbol, voltage=Void;
Line 1,190:
fcn __opAdd(other){ Resistor(0,"+",self,other) }
fcn __opMul(other){ Resistor(0,"*",self,other) }
}</langsyntaxhighlight>
===Infix===
<langsyntaxhighlight 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();</langsyntaxhighlight>
{{out}}
<pre style="height:15ex">
Line 1,221:
</pre>
===RPN===
<langsyntaxhighlight lang="zkl">fcn build(rpnStr){
stack:=List();
foreach symbol in (rpnStr.split()){
Line 1,240:
node.setVoltage(18);
println(" Ohm Volt Ampere Watt Network tree");
node.report();</langsyntaxhighlight>
{{out}}
<pre style="height:15ex">
10,327

edits