I don't know how to read Resistor Color Codes (I am not Electronic Engineer). The
original question have these lines:
Code: Select all
set /a percent=(%color1%%color2% * %multiplier%) * %tolerance%
set /a min=%color1%%color2% - %percent%
set /a max=%color1%%color2% + %percent%
So, for example, using Red-Red-Green-Silver bands we have:
Code: Select all
set /a percent=(22 * 100000) * 0.10
set /a min=22 - percent
set /a max=22 + percent
My first program use this formulae, but it is wrong...
The correct code seems to be this one:
Code: Select all
set /a value=%color1%%color2% * multiplier
set /a percent=value * tolerance
set /a min=value - percent
set /a max=value + percent
I fixed this part in the new code below. I also modified the input data so it now reads whole words instead of single letters, that is, the same method you used. I did this in order to make both codes as similar as possible, so the comparison between them be done just in the different details of both languages...
Code: Select all
@echo off
setlocal EnableDelayedExpansion
for %%D in (
Black:0:1
Brown:1:10:0.0100
Red:2:100:0.0200
Orange:3:1000:0.0300
Yellow:4:10000:0.0400
Green:5:100000:0.0050
Blue:6:1000000:0.0025
Violet:7:10000000:0.0010
Grey:8:100000000:0.0005
White:9:1000000000
Gold:10:10:0.0500
Silver:11:100:0.1000
) do for /F "tokens=1-5 delims=:." %%a in ("%%D") do (
set /A "%%a=%%b, mult[%%a]=%%c, tol[%%a]=1%%e%%10000"
)
echo Enter 4 or 5 band Resistor Colors, like in: Red Red Green Silver
set /P "band="
for /F "tokens=1-5" %%a in ("%band%") do (
set "color3=" & set /A "color1=%%a, color2=%%b, multiplier=mult[%%c], multBand=%%c, tolerance=tol[%%d]"
if "%%e" neq "" set /A "color3=%%c, multiplier=mult[%%d], multBand=%%d, tolerance=tol[%%e]"
)
if %multBand% leq 9 (
set /A "value=%color1%%color2%%color3%*multiplier"
) else (
set /A "value=%color1%%color2%%color3%/multiplier"
)
set /A "percent=value/100*tolerance/100, min=value-percent, max=value+percent"
echo Nominal: %value%
echo Minimum: %min%
echo Maximum: %max%
You opined that both programs use the same method and this is precisely my point. You can use practically the same structure of any other language in a Batch-file and the result will be similar, excepting of course when the emulation of a non-native Batch-file feature takes too much code. The emulation of fixed-point arithmetic with 4 decimales is rather simple. This is the reason because I don't agree when someone say that "in X language it is much simpler than in a Batch file".
However...
Batch files had evolved from ancient times and they have poor definition of many operative details. This means that a Batch-file is more
permisive than many modern better-defined languages. If we make good use of this poorly-defined features, we can perform certain things in a simpler way. A very simple example of this point is to exchange the value of two variables:
Code: Select all
set "var1=%var2%" & set "var2=%var1%"
This works because the way that %standard% expansion is performed. I don't know of any other programming language where this be possible (excepting perhaps assembler with its XCHG instruction)...
In the Batch-file version of the code we have here, we have this line:
Code: Select all
set /A "value=%color1%%color2%%color3%*multiplier"
If %color3% is not defined then it is replaced by "nothing", so the same code works for both 4- and 5-band colors with no modification.
In PowerShell we can't do the same. We need to check for each variable and individually manage it, that is the "good way" to do it:
Code: Select all
$nm = (($bds[$cl1][0] * 10) + $bds[$cl2][0]) * $bds[$mlt][1]
if ( $arr[4] ) {
$nm = (($bds[$cl1][0] * 100) + ($bds[$cl2][0] * 10) + $bds[$cl3][0]) * $bds[$mlt][1]
}
So, in this particular line, a Batch-file
is simpler than the PowerShell equivalent...
Antonio