if not "%str%" == "" goto analyze
rem
for %%i in (%str_tmp%) do call :exclude %%i
pause
exit
:exclude
for %%i in (%counted%) do if "%1"=="%%i" goto :eof
set counted=%counted% %1
call :count %1
goto :eof
:count
for %%i in (%str_tmp%) do if "%1"=="%%i" set /a %1+=1
echo %1 !%1!
goto :eof
@echo off
:: 统计字符出现的次数
:: 思路:
:: 拆解字符串,以空格分隔组成新字符串
:: 通过 shift 来call 不同的参数,并用
:: set 来命名变量,变量名具有统一的开头
:: 最后通过 set 来显示这些变量
::
::
::
setlocal EnableDelayedExpansion
set str=adadfdfseffserfefsefseetsdg
:loop
set str_tmp=%str_tmp% %str:~0,1% && set str=%str:~1%
if not "%str%" == "" goto loop
call :start %str_tmp%
set .
echo 出现次数最多的:%max%=%maxN%
pause
exit
:start
if [%1]==[] ( goto :eof ) else ( set /a .%1+=1 )
if !.%1! GTR !maxN! set maxN=!.%1!&& set max=.%1
shift
goto :start
@echo off
:: 综合以上方案,最简洁的代码如下
::
::
setlocal EnableDelayedExpansion
set str=adadfdfseffserfefsefseetsdgadadfdfseffserfefsefseetsdga
:loop
set str$=%str$% %str:~0,1%&set str=%str:~1%
if not "%str%" == "" goto loop
for %%n in (%str$%) do (
set /a .%%n+=1
if !.%%n! GTR !maxN! set maxN=!.%%n!&&set max=%%n)
set .
echo 出现次数最多的:%max%=%maxN%
pause
exit
@echo off&setlocal
:: sort之后,通过比较这一次取到的内容和上一次的内容是否相等来统计重复次数
:: 如何同时保存本次和上次的内容需要很大的技巧
:: 注意要把次数的初值设置为1,for语句的后括号之后不能紧跟跳出语句
::
::
set /a n=1
for /f %%a in ('type 1.txt^|sort') do (
call :pp %%a
)
:pp
if not defined bb goto b
if "%bb%"=="%1" (set /a n+=1) else (>>ko.txt echo %bb% %n%次&set /a n=1)
:b
set bb=%1
goto :eof
@echo off&setlocal enabledelayedexpansion
:: 带排序功能的代码
:: 用 for /l 来控制每次 findstr 的字符长度,
:: 然后把同一长度的用 sort 来排序,从而突破了
:: sort 只能按字符位大小来排序这一限制
::
::
set a=[0-9]
for /l %%a in (1,1,3) do (
call :pp !a!
set a=!a![0-9]
)
goto c
:pp
for /f %%x in ('findstr "^%1$" aa.txt^|sort') do @echo %%x >>dd.txt
goto :eof
:c
set /a n=1
for /f %%a in ('type dd.txt') do (
call :pp %%a
)
:pp
if not defined bb goto b
if "%bb%"=="%1" (set /a n+=1) else (>>ko.txt echo %bb% %n%次&set /a n=1)
:b
set bb=%1
goto :eof









