我找到了similar topic和solution by Endoro来将字母转换为数字,但是我想使用一组预定义的数字,然后将它们相互求和。有可能吗?
我的意思是:
ParentCollectionViewControllerCell
输出应如下所示:
"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%
命令的全部内容不太熟悉,因此希望对解决方案进行解释。
我想了解这是如何工作的。预先感谢。
答案 0 :(得分:1)
要想将每个字母设置为其自己的值,从一个新单词中提取和值并显示出来,我们需要采取一些步骤。
第一步是设置自定义字符串。为此,可以使用基本的for
循环。因为您引用了所有字母,所以使用set
命令会产生双重好处,每个结果将显示为Set "A=1"
,Set "B=2"
,例如。
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 /?
答案 1 :(得分:1)
有几种解决相同问题的方法。此解决方案非常简单,不需要单个for
命令!每个简单的步骤都在代码中进行了说明。也许“最复杂”的 :/
部分是具有字母值的变量的初始化。
此行:
set init="A=1" "B=2" "C=3" ... "X=5" "Y=1" "Z=1"
与您在问题中发布的“初始值”完全相同行。
此行:
set %init: =&set %
是一个简单的替换,它用&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