China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-08-07 06:06
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to enter some separated numbers in a txt and get their sum View 1,936 Replies 16
Original Poster Posted 2008-01-06 20:05 ·  中国 江西 赣州 安远县 电信
银牌会员
★★★★
Credits 2,025
Posts 1,122
Joined 2007-09-05 20:15
18-year member
UID 96653
Gender Male
Status Offline
The content in the text is:
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


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 ]
Floor 2 Posted 2008-01-06 21:30 ·  中国 广西 玉林 博白县 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
Try this:
(fixed the out-of-range data error)

@echo off
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"

set/p input=input:

call :loop _ %Test%
echo -----
echo =%sss%
pause
exit

:loop
set n=0
:shiftloop
if "%1" == "" goto :eof
set/a n+=1
if %n% leq %input:~0,1% shift&&goto shiftloop
set/a sss=%sss%+%1
echo +%1
set "input=%input:~1%"
if not "%input%" == "" goto loop
goto :eof


[ Last edited by zh159 on 2008-1-7 at 01:37 AM ]
Floor 3 Posted 2008-01-07 00:33 ·  中国 浙江 杭州 电信
高级用户
★★
Credits 537
Posts 219
Joined 2007-08-04 09:43
19-year member
UID 94620
Gender Male
From 杭州--半山
Status Offline
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
Floor 4 Posted 2008-01-07 00:58 ·  中国 江苏 常州 溧阳市 电信
银牌会员
★★★
Credits 2,404
Posts 946
Joined 2005-09-08 13:44
20-year member
UID 42345
Status Offline
@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 ]
简单!简单!再简单!
Floor 5 Posted 2008-01-07 01:39 ·  中国 广西 玉林 博白县 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
Originally posted by terse at 2008-1-7 00:58:
@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

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 ]
Floor 6 Posted 2008-01-07 10:24 ·  中国 江西 赣州 安远县 电信
银牌会员
★★★★
Credits 2,025
Posts 1,122
Joined 2007-09-05 20:15
18-year member
UID 96653
Gender Male
Status Offline
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...)
Floor 7 Posted 2008-01-07 11:25 ·  中国 广西 玉林 博白县 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
tireless +4 2008-01-07 14:33
Floor 8 Posted 2008-01-07 11:36 ·  中国 江西 赣州 安远县 电信
银牌会员
★★★★
Credits 2,025
Posts 1,122
Joined 2007-09-05 20:15
18-year member
UID 96653
Gender Male
Status Offline
Can't it return to input: by pressing any key? Also, only showing the English result is fine. Thanks
Floor 9 Posted 2008-01-07 12:30 ·  中国 江苏 常州 溧阳市 电信
银牌会员
★★★
Credits 2,404
Posts 946
Joined 2005-09-08 13:44
20-year member
UID 42345
Status Offline
Originally posted by zh159 at 2008-1-7 01:39:

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 ]

Right now the maximum probably can't reach 31, the maximum is 9
A revision of brother zh159's

@echo off
:start
set sssE=
set sss=
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
if %sss% equ 10 echo ten&&goto start
:Eloop
set/a n=%sss:~0,1%+1
set sss=%sss:~1%
for /f "tokens=%n%" %%a in ("zero one two three four five six seven eight nine") do set ssse=%ssse% %%a&&if not "%sss%" == "" goto Eloop
echo %ssse:~1%
goto start


[ Last edited by terse on 2008-1-7 at 01:27 PM ]
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
tireless +4 2008-01-07 14:33
简单!简单!再简单!
Floor 10 Posted 2008-01-07 14:34 ·  中国 江西 赣州 安远县 电信
银牌会员
★★★★
Credits 2,025
Posts 1,122
Joined 2007-09-05 20:15
18-year member
UID 96653
Gender Male
Status Offline
Many thanks! It achieved what I wanted

[ Last edited by tireless on 2008-1-7 at 02:48 AM ]
Floor 11 Posted 2008-01-07 15:17 ·  中国 广东 广州 电信
金牌会员
★★★★
一叶枝头,万树皆春
Credits 2,564
Posts 1,127
Joined 2006-12-25 22:57
19-year member
UID 74552
Gender Male
Status Offline


for /l %%i in (1,1,45) do call set "aaa=%%aaa%%%%i "
set /p in=input:
set /a 1/(in/10000),1/(100000/in)||goto :eof
set /a in1=%in:~0,1%,in2=in1+%in:~1,1%,in3=in2+%in:~2,1%,in4=in3+%in:~3,1%,in5=in4+%in:~4,1%
for %%i in (%aaa%) do set /a num+=1,1/^(num-in1^),1/^(num-in2^),1/^(num-in3^),1/^(num-in4^),1/^(num-in5^)||call set "output=%%output%%%%i "&&set /a out+=%%i
echo %out%=%output%
pause


This should solve the length problem, right
Floor 12 Posted 2008-01-07 15:42 ·  中国 江西 赣州 安远县 电信
银牌会员
★★★★
Credits 2,025
Posts 1,122
Joined 2007-09-05 20:15
18-year member
UID 96653
Gender Male
Status Offline
What length problem?

The maximum sum of the 5 numbers I input is only 32
Floor 13 Posted 2008-01-07 15:59 ·  中国 广东 广州 电信
金牌会员
★★★★
一叶枝头,万树皆春
Credits 2,564
Posts 1,127
Joined 2006-12-25 22:57
19-year member
UID 74552
Gender Male
Status Offline
for /f fetching values can't reach 45 positions, and with your five-digit number the maximum is 45 positions
Floor 14 Posted 2008-01-07 16:22 ·  中国 江西 赣州 安远县 电信
银牌会员
★★★★
Credits 2,025
Posts 1,122
Joined 2007-09-05 20:15
18-year member
UID 96653
Gender Male
Status Offline
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:
Floor 15 Posted 2008-01-07 16:41 ·  中国 广东 广州 电信
金牌会员
★★★★
一叶枝头,万树皆春
Credits 2,564
Posts 1,127
Joined 2006-12-25 22:57
19-year member
UID 74552
Gender Male
Status Offline
Now I'm depressed
Forum Jump: