![]() |
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-07 07:03 |
48,040 topics / 350,125 posts / today 1 new / 48,252 members |
| DOS批处理 & 脚本技术(批处理室) » How to enter some separated numbers in a txt and get their sum |
| Printable Version 1,939 / 16 |
| Floor1 tireless | Posted 2008-01-06 20:05 |
| 银牌会员 Posts 1,122 Credits 2,025 | |
|
The content in the text is:
Enter 5 digits, for example 52559, then calculate the result of the 5th+the (5+2)th+the (5+2+5)th+the (5+2+5+5)th+the (5+2+5+5+9)th 2 0 50000 50 0 20 4 1000 0 10 30 0 500 0 100 50000 2 0 1000 50 0 2 0 10 500 30 0 2 70 0 1000 100 [ Last edited by tireless on 2008-1-7 at 04:34 AM ] |
|
| Floor2 zh159 | Posted 2008-01-06 21:30 |
| 金牌会员 Posts 1,467 Credits 3,687 | |
|
Try this:
(fixed the out-of-range data error) [ Last edited by zh159 on 2008-1-7 at 01:37 AM ] |
|
| Floor3 junchen2 | Posted 2008-01-07 00:33 |
| 高级用户 Posts 219 Credits 537 From 杭州--半山 | |
|
echo off
set /p data_sum=data_sum number : awk -v fn=%data_sum% "{for(i=1;i<=length(fn);i++){sum+=substr(fn,i,1)};if(sum<=NF){for (i=1;i<=length(fn);i++){total+=$(fsum+=substr(fn,i,1))}} else {print \"warning: exceeds field count, note!\"}};END{print total}" 1.txt pause |
|
| Floor4 terse | Posted 2008-01-07 00:58 |
| 银牌会员 Posts 946 Credits 2,404 | |
|
@echo off
set/p input=input: set "Test=2 0 50000 50 0 20 4 1000 0 10 30 0 500 0 100 50000 2 0 1000 50 0 2 0 10 500 30 0 2 70 0 1000 100" :loop set/a n=%input:~0,1% set "input=%input:~1%" for /f "tokens=%n%*" %%a in ("%Test%") do set/a sss+=%%a&&if not "%input%" =="" set test=%%b && goto loop echo %sss% pause Just noticed that the last one couldn't be fetched, fixed it [ Last edited by terse on 2008-1-7 at 01:38 AM ] |
|
| Floor5 zh159 | Posted 2008-01-07 01:39 |
| 金牌会员 Posts 1,467 Credits 3,687 | |
Originally posted by terse at 2008-1-7 00:58: Testing found that a single tokens=n can only go up to 31 at most [ Last edited by zh159 on 2008-1-7 at 01:54 AM ] |
|
| Floor6 tireless | Posted 2008-01-07 10:24 |
| 银牌会员 Posts 1,122 Credits 2,025 | |
|
Thanks to the people above! Both the ones in post #2 and post #4 work.
One more small change: 1. After showing the result, press any key to return to "input" instead of exiting 2. Can the result be shown in English, with each digit separated by a space. For example, if the result is 302, then show three zero two (Man, last night I accidentally bumped the plug, and after rebooting I couldn't get online anymore. Just now I finally decided to uninstall the antivirus, and amazingly I could get online again...) |
|
| Floor7 zh159 | Posted 2008-01-07 11:25 |
| 金牌会员 Posts 1,467 Credits 3,687 | |
| Floor8 tireless | Posted 2008-01-07 11:36 |
| 银牌会员 Posts 1,122 Credits 2,025 | |
|
Can't it return to input: by pressing any key? Also, only showing the English result is fine. Thanks
|
|
| Floor9 terse | Posted 2008-01-07 12:30 |
| 银牌会员 Posts 946 Credits 2,404 | |
Originally posted by zh159 at 2008-1-7 01:39: Right now the maximum probably can't reach 31, the maximum is 9 A revision of brother zh159's
[ Last edited by terse on 2008-1-7 at 01:27 PM ] |
|
| Floor10 tireless | Posted 2008-01-07 14:34 |
| 银牌会员 Posts 1,122 Credits 2,025 | |
|
Many thanks! It achieved what I wanted
[ Last edited by tireless on 2008-1-7 at 02:48 AM ] |
|
| Floor11 everest79 | Posted 2008-01-07 15:17 |
| 金牌会员 Posts 1,127 Credits 2,564 | |
|
This should solve the length problem, right |
|
| Floor12 tireless | Posted 2008-01-07 15:42 |
| 银牌会员 Posts 1,122 Credits 2,025 | |
|
What length problem?
The maximum sum of the 5 numbers I input is only 32 |
|
| Floor13 everest79 | Posted 2008-01-07 15:59 |
| 金牌会员 Posts 1,127 Credits 2,564 | |
|
for /f fetching values can't reach 45 positions, and with your five-digit number the maximum is 45 positions
|
|
| Floor14 tireless | Posted 2008-01-07 16:22 |
| 银牌会员 Posts 1,122 Credits 2,025 | |
|
Uh. Anyway, no need to worry that the sum of these 5 numbers will be greater than 32, the sum of the 5 numbers I input is always within 32
How about this: if the sum of the 5 numbers is greater than 32 then return to input: |
|
| Floor15 everest79 | Posted 2008-01-07 16:41 |
| 金牌会员 Posts 1,127 Credits 2,564 | |
|
Now I'm depressed
|
|
| 1 2 Next |
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |