![]() |
China DOS Union-- Unite DOS · Advance DOS · Grow DOS --Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum |
| Guest | Log in | Register | Members | Search | China DOS Union |
|
中国DOS联盟论坛 The time now is 2026-08-08 22:14 |
47,811 topics / 349,895 posts / today 0 new / 48,253 members |
| DOS批处理 & 脚本技术(批处理室) » [Wonderful] [Statistics of the Number of Occurrences of Batch Processing Characters] |
| Printable Version 23,761 / 41 |
| Floor1 redtek | Posted 2006-11-29 08:26 |
| 金牌会员 Posts 1,147 Credits 2,902 | |
|
### 代码实现
```batch @echo off setlocal enabledelayedexpansion set "str=adadfdfseffserfefsefseetsdg" set "chars=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" set max_count=0 set max_char= for %%c in (%chars%) do ( set count=0 for /l %%i in (0,1,100) do ( set "char=!str:~%%i,1!" if "!char!"=="%%c" ( set /a count+=1 ) ) if !count! gtr !max_count! ( set max_count=!count! set max_char=%%c ) ) echo 出现次数最多的字母是:%max_char%,出现次数:%max_count% endlocal ``` ### 代码解释 1. `@echo off`:关闭命令回显,使脚本执行时不显示命令本身。 2. `setlocal enabledelayedexpansion`:启用延迟环境变量扩展,以便在循环中正确获取更新后的变量值。 3. `set "str=adadfdfseffserfefsefseetsdg"`:设置要处理的字符串。 4. `set "chars=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"`:定义所有可能的字母。 5. `for %%c in (%chars%) do`:遍历每个字母。 6. `set count=0`:初始化每个字母的计数为0。 7. `for /l %%i in (0,1,100) do`:循环遍历字符串的每个字符(假设字符串长度不超过100)。 8. `set "char=!str:~%%i,1!"`:获取字符串中指定位置的字符。 9. `if "!char!"=="%%c" ( set /a count+=1 )`:如果当前字符与遍历的字母相同,计数加1。 10. `if !count! gtr !max_count! ( set max_count=!count! & set max_char=%%c )`:如果当前字母的计数大于最大计数,更新最大计数和对应的字母。 11. `echo 出现次数最多的字母是:%max_char%,出现次数:%max_count%`:输出出现次数最多的字母及其次数。 12. `endlocal`:结束局部环境,恢复之前的环境变量状态。 |
|
| Floor2 namejm | Posted 2006-11-29 08:52 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
|
Actually, there is ready-made code available for plagiarism in this topic in the forum. Brother 3742668 did it before, and I also helped others answer in the Wuyou Boot Forum. I won't provide the code for now. You all can discuss it first ^_^.
|
|
| Floor3 vkill | Posted 2006-11-29 09:11 |
| 金牌会员 Posts 1,744 Credits 4,103 From 甘肃.临泽 | |
|
Suggestion: set "str=`-=\;',./~%!@#o^&*()_+{}|:<>?"abc123ABC00"
This kind of consideration for special characters is a bit challenging, oh [ Last edited by vkill on 2006-11-30 at 04:43 AM ] |
|
| Floor4 electronixtar | Posted 2006-11-29 09:33 |
| 铂金会员 Posts 2,672 Credits 7,493 | |
|
There is a command called wc in Linux haha ha ha ha
|
|
| Floor5 zh159 | Posted 2006-11-29 13:30 |
| 金牌会员 Posts 1,467 Credits 3,687 | |
|
Have a try^_^
[ Last edited by zxcv on 2006-11-29 at 01:31 AM ] |
|
| Floor6 redtek | Posted 2006-11-29 23:25 |
| 金牌会员 Posts 1,147 Credits 2,902 | |
|
Brother zxcv, the code on floor 5 is interesting~ Appreciate it~~~
The above first "breaks down" the string in the str variable "adadfdfseffserfefsefseetsdg" into individual characters separated by spaces, for the convenience of the for loop to take character values one by one~ : ) (This technique of breaking down a continuous string into individual letters separated by spaces is very interesting~ : ) When the string has been "broken down" into individual letters each separated by a space, it reads them one by one through for and passes the data to the :Scan1 label code segment in the form of a call statement with parameters. At this time, the :Scan1 label code segment can use the %1 method to get the data called over and process it~ : ) (The purpose of breaking down the continuous string into individual letters separated by spaces is to facilitate the for to get individual letters) (And for getting strings with spaces as delimiters makes use of the default value of the for Delims parameter) …… Brother zxcv, please talk about the ideas and principles of the other parts of the code? (The most difficult thing is when "away from" the various functions that can be called at any time in high-level languages, (such as special functions for string interception, statistics, etc......) (In this case, using internal commands and simple statements in batch processing can also complete the "transmission" of ideas. ) (Almost everything is like primitive operations, a few simple statement separations, a few Goto, a few SET...... can still complete the idea like simulating the "underlying" level. ) (In such a thinking process, everything is under the condition of no ready-made function support, using these simple commands to reproduce this complex process and the realization of the dream) (In this process, you can experience and feel the "primitive" and the most direct way to reproduce the beauty of batch processing) (All the points were given away yesterday, and I will make up the points for zxcv this afternoon~ : ) |
|
| Floor7 无奈何 | Posted 2006-11-29 23:52 |
| 荣誉版主 Posts 356 Credits 1,338 | |
|
Special characters in cmd are always a headache, and there's no way to solve them.
I also posted a thread. There's really no better way to implement it, so I use a crude method to make do.
|
|
| Floor8 zh159 | Posted 2006-11-30 00:08 |
| 金牌会员 Posts 1,467 Credits 3,687 | |
:Scan1 Using "for %%i in (%exclude%) do if "%1"=="%%i" to filter out the same characters, if there is a duplicate, return "for %%i in (%str$%) do call :Scan1 %%i" to filter the next character. The new character not in %exclude% is added with "set exclude=%exclude% %1" (this is when I was sorting mobile phone ringtones to filter the selected numbers) :Scan2 The filtered characters are calculated one by one for the number of occurrences. After self-calculation, "!%1!" is used with delayed variables to display the number of occurrences The reason for using "set /a %1+=1" is to provide usage for a single character later If you just want to display, change it to: :Scan1 You can do without delayed variables |
|
| Floor9 redtek | Posted 2006-11-30 00:22 |
| 金牌会员 Posts 1,147 Credits 2,902 | |
|
Helplessly, the code of the moderator and brother zxcv shines with the crystallization of every line of thinking ~~~
It's really wonderful ~~ |
|
| Floor10 ccwan | Posted 2006-11-30 00:30 |
| 金牌会员 Posts 1,160 Credits 2,725 From 河北廊坊 | |
|
I'll help Brother redtek add points! The two of you have great comments. Unfortunately, I don't have points right now. Next time then.
[ Last edited by ccwan on 2006-11-30 at 12:35 AM ] |
|
| Floor11 zh159 | Posted 2006-11-30 00:37 |
| 金牌会员 Posts 1,467 Credits 3,687 | |
|
Borrowing the script from Moderator Wunaike, but it hasn't been sorted alphabetically yet ^_^:
@echo off |
|
| Floor12 vkill | Posted 2006-11-30 00:44 |
| 金牌会员 Posts 1,744 Credits 4,103 From 甘肃.临泽 | |
|
If combined with sed grep, it is much simpler and appears
|
|
| Floor13 redtek | Posted 2006-11-30 00:46 |
| 金牌会员 Posts 1,147 Credits 2,902 | |
|
vkill use sed to make one~ :)
|
|
| Floor14 vkill | Posted 2006-11-30 00:53 |
| 金牌会员 Posts 1,744 Credits 4,103 From 甘肃.临泽 | |
Originally posted by redtek at 2006-11-30 00:46: Hehe~ grep -c can be used, because without using third-party tools, special characters can't be handled at all, I think |
|
| Floor15 redtek | Posted 2006-11-30 02:30 |
| 金牌会员 Posts 1,147 Credits 2,902 | |
|
I also come to a section, borrow the code segment with the space-separated letters of zxcv brother's :loop ~:)
|
|
| 1 2 3 Next |
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |