0

我遇到了这个问题,我想制作某种程序,它可以将所有组合中的所有单词更改为大写,例如 (input = something -> echo something; Something; something; ... someeTHIng; someeTHiNg;. ..某事)有什么想法吗?

4

2 回答 2

3

编辑:我意识到我的第一个解决方案不能正确管理由非字母字符(如空格)引起的重复排列。我修改了下面的批处理文件来修复这个细节。

@echo off
setlocal EnableDelayedExpansion

set /P "input=Enter input: "

rem Convert "input" into "upcase" and "lowcase", and create "letters" string
set upcase=%input%
set lowcase=%input%
set letters=
for %%a in ("a=A" "b=B" "c=C" "d=D" "e=E" "f=F" "g=G" "h=H" "i=I" "j=J" "k=K" "l=L" "m=M"
            "n=N" "o=O" "p=P" "q=Q" "r=R" "s=S" "t=T" "u=U" "v=V" "w=W" "x=X" "y=Y" "z=Z") do (
   set upcase=!upcase:%%~a!
   for /F "tokens=1,2 delims==" %%b in (%%a) do (
      set lowcase=!lowcase:%%c=%%b!
      set letters=!letters!%%c
   )
)

rem Separate individual characters in "char" two dimensional array, and create "bit" array
rem Non-alphabetic characters are marked with "bit" elements = 2
set n=-1
:nextChar
   set /A n+=1
   set char=%lowcase:~0,1%
   if "!letters:%char%=!" neq "%letters%" (
      set bit[%n%]=0
      set char[%n%,0]=%char%
      set char[%n%,1]=%upcase:~0,1%
   ) else (
      set bit[%n%]=2
      set char[%n%,2]=%char%
   )
   set upcase=%upcase:~1%
   set lowcase=%lowcase:~1%
if defined lowcase goto nextChar

rem Generate all permutations
:nextValue
   rem Copy characters from "char" array into "output" string with current "bit" values
   set output=
   for /L %%i in (0,1,%n%) do (
      for %%b in (!bit[%%i]!) do (
         set output=!output!!char[%%i,%%b]!
      )
   )
   rem Show this permutation
   echo %output%
   rem Advance "bit" array to next value
   set /A b=n+1, carry=1
   :nextBit
      set /A b-=1
      if !bit[%b%]! lss 2 (
         if !bit[%b%]! equ 0 (
            set /A bit[%b%]=1, carry=0
         ) else (
            set /A bit[%b%]=0, carry=1
         )
      )
   if %b% gtr 0 if %carry% equ 1 goto nextBit
if %carry% equ 0 goto nextValue
于 2013-06-06T22:30:06.723 回答
1

此解决方案正确处理字符串中没有大小写的字符。

@echo off
setlocal enableDelayedExpansion

set "string=%~1"
set "permutation="
set pos=0

for %%C in (a b c d e f g h i j k l m n o p q r s t u v w x y z) do set "lower_%%C=%%C"
for %%C in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do set "upper_%%C=%%C"

if defined string call :permute
exit /b

:permute
setlocal
set "chr=!string:~%pos%,1!"
if not defined chr (
  echo(!permutation!
  exit /b
)
set /a pos+=1
for /f delims^=^ eol^= %%C in ("!chr!") do (
  if defined upper_%%C (
    set "permutation=!permutation!!lower_%%C!"
    call :permute
    set "permutation=!permutation:~0,-1!!upper_%%C!"
    call :permute
  ) else (
    set "permutation=!permutation!%%C"
    call :permute
  )
)
exit /b

假设脚本是 PERMUTE.BAT,下面是一个带有输出的示例调用:

>permute.bat "test it"
test it
test iT
test It
test IT
tesT it
tesT iT
tesT It
tesT IT
teSt it
teSt iT
teSt It
teSt IT
teST it
teST iT
teST It
teST IT
tEst it
tEst iT
tEst It
tEst IT
tEsT it
tEsT iT
tEsT It
tEsT IT
tESt it
tESt iT
tESt It
tESt IT
tEST it
tEST iT
tEST It
tEST IT
Test it
Test iT
Test It
Test IT
TesT it
TesT iT
TesT It
TesT IT
TeSt it
TeSt iT
TeSt It
TeSt IT
TeST it
TeST iT
TeST It
TeST IT
TEst it
TEst iT
TEst It
TEst IT
TEsT it
TEsT iT
TEsT It
TEsT IT
TESt it
TESt iT
TESt It
TESt IT
TEST it
TEST iT
TEST It
TEST IT
于 2013-06-07T04:44:06.363 回答