批处理脚本:如何将字母转换为预定义的一组数字对应物,然后将它们相加
Batch Script: How to convert letters into their predefined set of numerical counterparts then sum them up
我找到了 similar topic and solution by Endoro 关于将字母转换成对应数字的方法,但在我的例子中,我想使用一组预定义的数字,然后将它们相互求和。这可能吗?
我的意思是:
"A=1" "B=2" "C=3" "D=9" "E=4" "F=8" "G=2" "H=5" "I=2" "J=5" "K=2" "L=5" "M=4" "N=5" "O=2" "P=8" "Q=1" "R=9" "S=8" "T=7" "U=6" "V=6" "W=1" "X=5" "Y=1" "Z=1"
输出应如下所示:
Atom
1 7 2 4
14
以上参考:
@ECHO OFF &SETLOCAL ENABLEDELAYEDEXPANSION
SET /p "text=input : "
cls
SET "alfa=0abcdefghijklmnopqrstuvwxyz"
FOR /l %%x IN (1,1,26) DO SET "$!alfa:~%%x,1!=%%x"
SET /a count=0
:loop
SET "char=!text:~%count%,1!"
SET "code=!$%char%!
SET /a count+=1
IF DEFINED char SET "line=!line!%code% "&GOTO :loop
ECHO %text%
ECHO %line%
我不太熟悉 for
命令的全部内容,因此不胜感激。
我想了解这是如何工作的。提前致谢。
因为你想给每个字母设置它自己的值,从一个新词中提取总和值,并显示它,我们需要采取几个步骤。
第一步是设置自定义字符串。为此,可以使用基本的 for
循环。因为你引用了所有的字母,所以使用 set
命令有双重好处,每个结果将显示为 Set "A=1"
、Set "B=2"
、Exc.
For %%A in ("A=1" "B=2" "C=3" "D=9" "E=4" "F=8" "G=2" "H=5" "I=2" "J=5" "K=2" "L=5" "M=4" "N=5" "O=2" "P=8" "Q=1" "R=9" "S=8" "T=7" "U=6" "V=6" "W=1" "X=5" "Y=1" "Z=1") do (
Set %%A
)
请记住,我们正在使用 set "text=!text: =!"
或 syntax-replace.
删除句子(如果存在)中的空格
最后一步是将每个字母转换为相应的值,并将它们相加。为此,我们必须从字符串中提取每个字母。使用循环,我们可以提取这些值并使用 Set /a "String=!String!+!New Number!"
.
添加它们
GetSumUsingCustumNumericals.bat:
@Echo off
@setlocal ENABLEDELAYEDEXPANSION
Rem | Ask User For Word
Set /p "text=input: "
Set "orginaltext=!text!"
set "text=!text: =!"
Cls
Rem | Set Strings
For %%A in ("A=1" "B=2" "C=3" "D=9" "E=4" "F=8" "G=2" "H=5" "I=2" "J=5" "K=2" "L=5" "M=4" "N=5" "O=2" "P=8" "Q=1" "R=9" "S=8" "T=7" "U=6" "V=6" "W=1" "X=5" "Y=1" "Z=1") do (
Set %%A
)
Rem | Convert Text
set pos=0
:NextChar
Set "Letter=!text:~%pos%,1%!"
Set "Converted=!%Letter%!"
Set "Numbers=!Numbers! !Converted!"
Set /a "Sum=!Sum!+!Converted!"
set /a pos=pos+1
if "!text:~%pos%,1!" NEQ "" goto NextChar
goto Finished
:Finished
Rem | Display Results
Echo Text: !orginaltext!
Echo Letter Values: !Numbers:~1!
Echo Sum: !Sum!
pause>nul
goto :EOF
输入:
Hello my name is John
算法:
A=1 B=2 C=3 D=9 E=4 F=8 G=2 H=5 I=2 J=5 K=2 L=5 M=4 N=5 O=2 P=8 Q=1 R=9 S=8 T=7 U=6 V=6 W=1 X=5 Y=1 Z=1
输出:
Text: Hello my name is John
Letter Values: 5 4 5 5 2 4 1 5 1 4 4 2 8 5 2 5 5
Sum: 67
如需任何命令的帮助,请执行以下操作:
goto /?
set /?
for /?
if /?
- 等等。
解决同一个问题有多种不同的方法。这个解决方案非常简单,不需要一个for
命令!代码中解释了每个简单的步骤。也许 "most complicated" :/
部分是具有字母值的变量的初始化。
这一行:
set init="A=1" "B=2" "C=3" ... "X=5" "Y=1" "Z=1"
与您在问题中发布的 "initial values" 行完全相同行。
这一行:
set %init: =&set %
是一个简单的替换,将 每个 space 更改为 &set
。这意味着上一行更改为:
set "A=1"&set "B=2"&set "C=3" ...&set "X=5"&set "Y=1"&set "Z=1"
然后,之后,该行执行...很简单,不是吗? ;)
@echo off
setlocal
rem Define initialization string
set init="A=1" "B=2" "C=3" "D=9" "E=4" "F=8" "G=2" "H=5" "I=2" "J=5" "K=2" "L=5" "M=4" "N=5" "O=2" "P=8" "Q=1" "R=9" "S=8" "T=7" "U=6" "V=6" "W=1" "X=5" "Y=1" "Z=1"
rem Initialize variables
set %init: =&set %
set "nums="
set "sum=0"
set /P "text=input: "
echo %text%
:loop
rem Get first char in text
set "char=%text:~0,1%"
rem Get code and add it to sum
set /A "code=%char%, sum+=code"
rem Join code to nums
set "nums=%nums%%code% "
rem Remove first char from text and repeat
set "text=%text:~1%"
IF DEFINED text GOTO :loop
ECHO %nums%
ECHO %sum%
示例:
input: Atom
Atom
1 7 2 4
14
您输入示例的输出示例与您上面列出的完全相同...
编辑:添加了新版本
@echo off
setlocal
rem Define initialization string
set init="A=1" "B=2" "C=3" "D=9" "E=4" "F=8" "G=2" "H=5" "I=2" "J=5" "K=2" "L=5" "M=4" "N=5" "O=2" "P=8" "Q=1" "R=9" "S=8" "T=7" "U=6" "V=6" "W=1" "X=5" "Y=1" "Z=1"
rem Initialize variables
set %init: =&set %
:repeat
set "text= "
set /P "text=input: "
set "text=%text: =%"
if not defined text goto :EOF
set "out="
set "nums="
set "sum=0"
:loop
rem Get first char in text and join it to out
set "char=%text:~0,1%"
set "out=%out%%char% "
rem Get code and add it to sum
set /A "code=%char%, sum+=code"
rem Join code to nums
set "nums=%nums%%code% "
rem Remove first char from text and repeat
set "text=%text:~1%"
IF DEFINED text GOTO :loop
ECHO %out%
ECHO %nums%
ECHO %sum%
echo/
goto repeat
示例:
input: Atom
A t o m
1 7 2 4
14
input: My name is Antonio
M y n a m e i s A n t o n i o
4 1 5 1 4 4 2 8 1 5 7 2 5 2 2
53
input:
编辑:另一个较短的版本,仅供娱乐...
@echo off
setlocal EnableDelayedExpansion
rem Define initialization string
set init="A=1" "B=2" "C=3" "D=9" "E=4" "F=8" "G=2" "H=5" "I=2" "J=5" "K=2" "L=5" "M=4" "N=5" "O=2" "P=8" "Q=1" "R=9" "S=8" "T=7" "U=6" "V=6" "W=1" "X=5" "Y=1" "Z=1"
rem Initialize one-letter variables
set %init: =&set %
:repeat
rem Read a line from user
set "text= "
set /P "text=input: "
set "text=%text: =%"
if not defined text goto :EOF
rem Insert a space between letters
set "out="
:loop
set "out=%out% %text:~0,1%"
set "text=%text:~1%"
IF DEFINED text GOTO :loop
set "out=%out:~1%"
rem Get the sum: replace the space between letters by a plus sign
set /A "sum=%out: =+%"
rem Show letters separated by space
echo %out%
rem Change spaces by "! !" to show *the values* of the letters via Delayed Expansion
echo !%out: =! !%!
echo %sum%
echo/
goto repeat
我找到了 similar topic and solution by Endoro 关于将字母转换成对应数字的方法,但在我的例子中,我想使用一组预定义的数字,然后将它们相互求和。这可能吗?
我的意思是:
"A=1" "B=2" "C=3" "D=9" "E=4" "F=8" "G=2" "H=5" "I=2" "J=5" "K=2" "L=5" "M=4" "N=5" "O=2" "P=8" "Q=1" "R=9" "S=8" "T=7" "U=6" "V=6" "W=1" "X=5" "Y=1" "Z=1"
输出应如下所示:
Atom
1 7 2 4
14
以上参考:
@ECHO OFF &SETLOCAL ENABLEDELAYEDEXPANSION
SET /p "text=input : "
cls
SET "alfa=0abcdefghijklmnopqrstuvwxyz"
FOR /l %%x IN (1,1,26) DO SET "$!alfa:~%%x,1!=%%x"
SET /a count=0
:loop
SET "char=!text:~%count%,1!"
SET "code=!$%char%!
SET /a count+=1
IF DEFINED char SET "line=!line!%code% "&GOTO :loop
ECHO %text%
ECHO %line%
我不太熟悉 for
命令的全部内容,因此不胜感激。
我想了解这是如何工作的。提前致谢。
因为你想给每个字母设置它自己的值,从一个新词中提取总和值,并显示它,我们需要采取几个步骤。
第一步是设置自定义字符串。为此,可以使用基本的 for
循环。因为你引用了所有的字母,所以使用 set
命令有双重好处,每个结果将显示为 Set "A=1"
、Set "B=2"
、Exc.
For %%A in ("A=1" "B=2" "C=3" "D=9" "E=4" "F=8" "G=2" "H=5" "I=2" "J=5" "K=2" "L=5" "M=4" "N=5" "O=2" "P=8" "Q=1" "R=9" "S=8" "T=7" "U=6" "V=6" "W=1" "X=5" "Y=1" "Z=1") do (
Set %%A
)
请记住,我们正在使用 set "text=!text: =!"
或 syntax-replace.
最后一步是将每个字母转换为相应的值,并将它们相加。为此,我们必须从字符串中提取每个字母。使用循环,我们可以提取这些值并使用 Set /a "String=!String!+!New Number!"
.
GetSumUsingCustumNumericals.bat:
@Echo off
@setlocal ENABLEDELAYEDEXPANSION
Rem | Ask User For Word
Set /p "text=input: "
Set "orginaltext=!text!"
set "text=!text: =!"
Cls
Rem | Set Strings
For %%A in ("A=1" "B=2" "C=3" "D=9" "E=4" "F=8" "G=2" "H=5" "I=2" "J=5" "K=2" "L=5" "M=4" "N=5" "O=2" "P=8" "Q=1" "R=9" "S=8" "T=7" "U=6" "V=6" "W=1" "X=5" "Y=1" "Z=1") do (
Set %%A
)
Rem | Convert Text
set pos=0
:NextChar
Set "Letter=!text:~%pos%,1%!"
Set "Converted=!%Letter%!"
Set "Numbers=!Numbers! !Converted!"
Set /a "Sum=!Sum!+!Converted!"
set /a pos=pos+1
if "!text:~%pos%,1!" NEQ "" goto NextChar
goto Finished
:Finished
Rem | Display Results
Echo Text: !orginaltext!
Echo Letter Values: !Numbers:~1!
Echo Sum: !Sum!
pause>nul
goto :EOF
输入:
Hello my name is John
算法:
A=1 B=2 C=3 D=9 E=4 F=8 G=2 H=5 I=2 J=5 K=2 L=5 M=4 N=5 O=2 P=8 Q=1 R=9 S=8 T=7 U=6 V=6 W=1 X=5 Y=1 Z=1
输出:
Text: Hello my name is John
Letter Values: 5 4 5 5 2 4 1 5 1 4 4 2 8 5 2 5 5
Sum: 67
如需任何命令的帮助,请执行以下操作:
goto /?
set /?
for /?
if /?
- 等等。
解决同一个问题有多种不同的方法。这个解决方案非常简单,不需要一个for
命令!代码中解释了每个简单的步骤。也许 "most complicated" :/
部分是具有字母值的变量的初始化。
这一行:
set init="A=1" "B=2" "C=3" ... "X=5" "Y=1" "Z=1"
与您在问题中发布的 "initial values" 行完全相同行。
这一行:
set %init: =&set %
是一个简单的替换,将 每个 space 更改为 &set
。这意味着上一行更改为:
set "A=1"&set "B=2"&set "C=3" ...&set "X=5"&set "Y=1"&set "Z=1"
然后,之后,该行执行...很简单,不是吗? ;)
@echo off
setlocal
rem Define initialization string
set init="A=1" "B=2" "C=3" "D=9" "E=4" "F=8" "G=2" "H=5" "I=2" "J=5" "K=2" "L=5" "M=4" "N=5" "O=2" "P=8" "Q=1" "R=9" "S=8" "T=7" "U=6" "V=6" "W=1" "X=5" "Y=1" "Z=1"
rem Initialize variables
set %init: =&set %
set "nums="
set "sum=0"
set /P "text=input: "
echo %text%
:loop
rem Get first char in text
set "char=%text:~0,1%"
rem Get code and add it to sum
set /A "code=%char%, sum+=code"
rem Join code to nums
set "nums=%nums%%code% "
rem Remove first char from text and repeat
set "text=%text:~1%"
IF DEFINED text GOTO :loop
ECHO %nums%
ECHO %sum%
示例:
input: Atom
Atom
1 7 2 4
14
您输入示例的输出示例与您上面列出的完全相同...
编辑:添加了新版本
@echo off
setlocal
rem Define initialization string
set init="A=1" "B=2" "C=3" "D=9" "E=4" "F=8" "G=2" "H=5" "I=2" "J=5" "K=2" "L=5" "M=4" "N=5" "O=2" "P=8" "Q=1" "R=9" "S=8" "T=7" "U=6" "V=6" "W=1" "X=5" "Y=1" "Z=1"
rem Initialize variables
set %init: =&set %
:repeat
set "text= "
set /P "text=input: "
set "text=%text: =%"
if not defined text goto :EOF
set "out="
set "nums="
set "sum=0"
:loop
rem Get first char in text and join it to out
set "char=%text:~0,1%"
set "out=%out%%char% "
rem Get code and add it to sum
set /A "code=%char%, sum+=code"
rem Join code to nums
set "nums=%nums%%code% "
rem Remove first char from text and repeat
set "text=%text:~1%"
IF DEFINED text GOTO :loop
ECHO %out%
ECHO %nums%
ECHO %sum%
echo/
goto repeat
示例:
input: Atom
A t o m
1 7 2 4
14
input: My name is Antonio
M y n a m e i s A n t o n i o
4 1 5 1 4 4 2 8 1 5 7 2 5 2 2
53
input:
编辑:另一个较短的版本,仅供娱乐...
@echo off
setlocal EnableDelayedExpansion
rem Define initialization string
set init="A=1" "B=2" "C=3" "D=9" "E=4" "F=8" "G=2" "H=5" "I=2" "J=5" "K=2" "L=5" "M=4" "N=5" "O=2" "P=8" "Q=1" "R=9" "S=8" "T=7" "U=6" "V=6" "W=1" "X=5" "Y=1" "Z=1"
rem Initialize one-letter variables
set %init: =&set %
:repeat
rem Read a line from user
set "text= "
set /P "text=input: "
set "text=%text: =%"
if not defined text goto :EOF
rem Insert a space between letters
set "out="
:loop
set "out=%out% %text:~0,1%"
set "text=%text:~1%"
IF DEFINED text GOTO :loop
set "out=%out:~1%"
rem Get the sum: replace the space between letters by a plus sign
set /A "sum=%out: =+%"
rem Show letters separated by space
echo %out%
rem Change spaces by "! !" to show *the values* of the letters via Delayed Expansion
echo !%out: =! !%!
echo %sum%
echo/
goto repeat