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-06-28 16:54
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Help] Text output~ View 2,798 Replies 47
Floor 31 Posted 2008-05-07 23:33 ·  中国 湖南 娄底 电信
银牌会员
★★★
Credits 2,268
Posts 879
Joined 2006-12-19 16:23
19-year member
UID 73968
Gender Male
Status Offline
It seems that my expression ability is quite problematic.
Do it again
Require to generate a text file with content being a, b and spaces, but the total number of a and b is 60.
The first line cannot have spaces and must be interlaced like ababab, not like bba or aab.
For example:
Suppose the first randomly obtained letter is a, then write a into b.txt
Then obtain the second randomly obtained letter, suppose it is a again, then write it below the previously obtained a. If it is b, then write it in the first line of b.txt, that is, side by side with the first a.
Continue to obtain the randomly obtained letter, then judge whether it is the same as the previous letter. If it is the same, write it directly below the previous letter. Note: "is the previous letter", otherwise write it in the first line.
Then the final result obtained in this way will be the following format of text.

ababababa
a ab b ba
ab b a
b a
致精致简!
Floor 32 Posted 2008-05-07 23:39 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
Let me state my train of thought~

abflag = "sloreissloreleeandsloreleeisslorehahailovedoscndosgood"

Randomly generate a number to represent the row~ For example, 12
Then generate
abababababab as the first row, ab = 12
Then start:
i = i + 1
If the i%12th character of abflag is " ", see whether it's odd or even and directly output a or b, ab += 1
i%2……
If odd, then a or " "……Random next
If it's " ", then replace the i-th character of abflag with " ", else that's a, oh right~ ab += 1
If even, then b or " "……Random again
If it's " ", then replace the i-th character of abflag with " ", else that's b, oh right~ ab += 1
Do I need to handle the line break for i%12 by myself?
Hmm, ab = 60啦~88

See if you can understand this pseudo-code~ If not, just treat it as a mark……
No interest in processing text with P~

[ Last edited by slore on 2008-5-7 at 11:49 PM ]
Floor 33 Posted 2008-05-08 00:19 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
By the way, let's comment on the crooked-headed~~thinking problem...

Randomly choose to start with a or b? x = a? b
Randomly a number of lines, such as 8

k = 60 - 8
Start the loop
Randomly generate a number i ≤ k (k = k - i)
Generate i consecutive x

Randomly generate a number i ≤ k (k = k - i)
Generate i consecutive reversed x (if x is a, then reversed x is b)

If the loop has reached the 7th time... output all remaining k...
If k = 0, end...

This will generate a 52-character within 8 lines...

Suppose starting with a...
The first random is 8

In the loop, the first generated is 9
Output
aaaaaaaaa
Then the loop generates 5
bbbbb
Then the loop generates 0

Then the loop generates 4
bbbb
Then the loop generates 8
aaaaaaaa
Then the loop generates 0

Then the loop generates 12
aaaaaaaaaaaa
7 loops and still not enough 60?
The remaining 14 are given to you
bbbbbbbbbbbbbb
Result:

aaaaaaaaa
bbbbb
aaaaaaaa

bbbb
aaaaaaaa

aaaaaaaaaaaa
bbbbbbbbbbbbbb


There are codes for row-column conversion in the forum, right?
Convert and input to b.txt
Then output:
babababa
type b.txt

This seems troublesome? I don't know which is faster...
If not converting, generating this code is the easiest...
Floor 34 Posted 2008-05-08 10:52 ·  中国 广西 中移铁通
银牌会员
★★★
永远的菜鸟
Credits 1,335
Posts 574
Joined 2007-11-27 12:50
18-year member
UID 103929
Gender Male
From 广西
Status Offline
I'll come one. This code will display that ECHO is off because the number of a and b is less than 60 (can only be closed by closing the window, sweat... Hope experts can improve it). Due to limited level, the number of characters in the first line is all even numbers (alas, sweat again...), of course, it can also be odd, just because it's too far-fetched, so it's not. There is no so-called algorithm, only a little idea, that is, compare the characters in the corresponding position of the next line with the previous line, if they are different, replace them with spaces. I hope experts can give a more efficient and concise code (preferably explain the algorithm so that novices and we chumps can learn).
@echo off
setlocal enabledelayedexpansion
set "code=ab"
set /a c=%random%%%2,d=%random%%%60+1,e=d*2
for /l %%i in (1,1,%d%) do (
if %c%==0 (
set var1=!var1!ab&set /a numa+=1,numb+=1
) else (
set var1=!var1!ba&set /a numa+=1,numb+=1
))
:again
echo %var1%
set "var2="
set n=0
:again_
set /a f=%random%%%2
call set g=%%code:~%f%,1%%
call set h=%%var1:~%n%,1%%
if "%g%"=="%h%" (
set "var2=%var2%!g!"&set /a num!g!+=1,n+=1
) else (
set "var2=%var2% "&set /a n+=1
)
if %numa% gtr 60 (if %numb% gtr 60 echo %var2%&pause&exit /b)
if %n% lss %e% goto :again_
echo %var2%
set n=0
set "var1="
:more
set /a i=%random%%%2
call set j=%%code:~%i%,1%%
call set k=%%var2:~%n%,1%%
if "%j%"=="%k%" (
set "var1=%var1%!j!"&set /a num!j!+=1,n+=1
) else (
set "var1=%var1% "&set /a n+=1
)
if %numa% gtr 60 (if %numb% gtr 60 echo %var2%&pause&exit /b)
if %n% lss %e% goto :more
goto :again
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
26933062 +3 2008-05-08 11:15
Floor 35 Posted 2008-05-08 11:15 ·  中国 湖南 娄底 电信
银牌会员
★★★
Credits 2,268
Posts 879
Joined 2006-12-19 16:23
19-year member
UID 73968
Gender Male
Status Offline
Oh my God! Finally there is someone who understands the problem. There are indeed bugs in the code, and it seems that b and a are not very random. What is required is that each a or b is randomly obtained. Not bad either, bonus for encouragement.
致精致简!
Floor 36 Posted 2008-05-08 11:22 ·  中国 广西 中移铁通
银牌会员
★★★
永远的菜鸟
Credits 1,335
Posts 574
Joined 2007-11-27 12:50
18-year member
UID 103929
Gender Male
From 广西
Status Offline
And b and a don't seem very random

set "code=ab"
set f=%random%%%2
call set g=%%code:~%f%,1%%
Is this not random?
Floor 37 Posted 2008-05-08 12:01 ·  中国 湖南 娄底 电信
银牌会员
★★★
Credits 2,268
Posts 879
Joined 2006-12-19 16:23
19-year member
UID 73968
Gender Male
Status Offline
Oh, that's right. It should be random. I just noticed that your result has so many letters at first. The total should be 60.
致精致简!
Floor 38 Posted 2008-05-08 12:53 ·  中国 广西 中移铁通
银牌会员
★★★
永远的菜鸟
Credits 1,335
Posts 574
Joined 2007-11-27 12:50
18-year member
UID 103929
Gender Male
From 广西
Status Offline
Strongly request 26933062 boss to post the code
Floor 39 Posted 2008-05-08 17:09 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
```
@echo off&setlocal enabledelayedexpansion
Title This code demonstrates a boring process...
color 1f
Set Max=60
Set FixTime=0
::This is a custom switch~ If you want to use your own set number of columns, modify it...
Set firstline=slore
Set abFlag=sloreissloreleeandsloreleeisslorehahailovedoscndosgoodblue
::Naturally, it can be 1~60, but the first line is too long and not good-looking. Haha, change it if you don't like it~
if %firstline%==slore (
Set /a firstline=%random%%%5+20
Set /a Max=%random%%%20+40
Set Msg=Slore sets output!firstline! columns, total!Max! characters
) else (
Set Msg=%username% sets output%firstline% columns, total%Max% characters)
echo o(∩_∩)o-----------------------------------Xuan----
echo %Msg%
echo ----Xuan-----------------------------------o(∩_∩)o
echo.
Set /a n=%firstline%/2
for /l %%i in (1,1,%n%) do Call,Set fl=!fl!ab
Set /a n=%firstline%%%2
if %n% EQU 1 Set fl=%fl%a
echo %fl%
Set ab=%firstline%
Set /a endline=%ab%-1
Set i=0
:Loop
Set /a abPos=%i%%%%firstline%
Set /a rad1=%abPos%%%2
Set /a rad2=%random%%%2
Set /a i+=1
Set Posletter=!abFlag:~%abPos%,1!

if %FixTime% EQU %endline% Set rad2=1
if %rad1% EQU 0 (
if "%Posletter%"==" " (Set nl=%nl% ) else (
if %rad2% EQU 0 (Set nl=%nl% &Call :Fix %abPos%) else Set nl=%nl%a&Set /a ab+=1)
) else (
if "%Posletter%"==" " (Set nl=%nl% ) else (
if %rad2% EQU 0 (Set nl=%nl% &Call :Fix %abPos%) else Set nl=%nl%b&Set /a ab+=1)
)
if %abPos% EQU %endline% echo %nl%
if %abPos% EQU %endline% Set nl=
if %ab% EQU %Max% goto :ExitLoop
goto :Loop

:ExitLoop
if Not "%nl%"=="" echo %nl%
echo.
::echo The number of ab is:%ab%
echo Boring code demonstration ends!
pause>nul

:Fix
if %FixTime% EQU %endline% goto :EndFix
Set /a e=%1+1
Set abFlag=!abFlag:~0,%1! !abFlag:~%e%!
Set /a FixTime+=1
:EndFix
```

[ Last edited by slore on 2008-5-8 at 05:19 PM ]
Floor 40 Posted 2008-05-08 17:56 ·  中国 广西 中移铁通
银牌会员
★★★
永远的菜鸟
Credits 1,335
Posts 574
Joined 2007-11-27 12:50
18-year member
UID 103929
Gender Male
From 广西
Status Offline
First delete, sweat

[ Last edited by moniuming on 2008-5-8 at 06:27 PM ]
Floor 41 Posted 2008-05-08 18:06 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
……
Your code doesn't seem to be in a fixed output format?
And md, rd =... Creating a folder is...
Floor 42 Posted 2008-05-08 19:30 ·  中国 湖南 娄底 电信
银牌会员
★★★
Credits 2,268
Posts 879
Joined 2006-12-19 16:23
19-year member
UID 73968
Gender Male
Status Offline
Floor 39 has the correct format, but at first glance, your number of columns can be customized? Then aren't a and b not random? If each a and b is random, then the number of columns cannot be predicted.
致精致简!
Floor 43 Posted 2008-05-08 19:47 ·  中国 广西 中移铁通
银牌会员
★★★
永远的菜鸟
Credits 1,335
Posts 574
Joined 2007-11-27 12:50
18-year member
UID 103929
Gender Male
From 广西
Status Offline
Your code doesn't seem to be in a fixed output format?
And md, rd =。= Creating folders is...

Because I can't control the number of output characters, when it exceeds the range, it will display "ECHO is off", so I can only create folders to determine whether the variable is a space (that is, if I can create a folder, then continue outputting, otherwise exit)
@echo off
setlocal enabledelayedexpansion
set "code=ab"
set /a c=%random%%%2,d=%random%%%20+20,e=d*2
for /l %%i in (1,1,%d%) do (
if %c%==0 (
set var1=!var1!ab
) else (
set var1=!var1!ba
))
:again
md "%var1%" >nul 2>nul
if errorlevel 1 pause&exit /b
rd "%var1%" >nul 2>nul
echo %var1%
set "var2="
set n=0
:again_
set /a f=%random%%%2
call set g=%%code:~%f%,1%%
call set h=%%var1:~%n%,1%%
if "%g%"=="%h%" (
set "var2=%var2%!g!"&set /a n+=1
) else (
set "var2=%var2% "&set /a n+=1
)
if %n% lss %e% goto :again_
md "%var2%" >nul 2>nul
if errorlevel 1 pause&exit /b
rd "%var2%" >nul 2>nul
echo %var2%
set n=0
set "var1="
:more
set /a i=%random%%%2
call set j=%%code:~%i%,1%%
call set k=%%var2:~%n%,1%%
if "%j%"=="%k%" (
set "var1=%var1%!j!"&set /a n+=1
) else (
set "var1=%var1% "&set /a n+=1
)
if %n% lss %e% goto :more
goto :again


[ Last edited by moniuming on 2008-5-8 at 08:08 PM ]
Floor 44 Posted 2008-05-08 20:59 ·  中国 湖南 娄底 电信
银牌会员
★★★
Credits 2,268
Posts 879
Joined 2006-12-19 16:23
19-year member
UID 73968
Gender Male
Status Offline
Sweat!
I just feel that my code needs to run the loop tag about 30 times on average, which is not very efficient. I wanted to find a more efficient code, but I didn't expect that more than 40 posts came up, and most of them didn't understand the question. My expression ability is really extremely poor. I believe this post can no longer arouse the interest of other experts. Alas...
Still use code to illustrate the problem. In order to display beautifully, I replaced a and b with two other symbols, which looks clearer.
The code is as follows:

@echo off&color 1f
set _0=○&set "_1=●"&set /a ko=60
setlocal enabledelayedexpansion
for /l %%l in (1 1 %ko%) do (
set /a w=!random!%%2
call set "ss=%%_!w!%%"
if not "!ss!"=="!flag!" (
set "flag=!ss!"
set /a h=1
set "num1=!num1!!ss!"
call :loop
) else (
set /a h+=1
call set "num!h!=%%num!h!:~0,-1%%!ss!"
))
for /l %%l in (1 1 %ko%) do if not "!num%%l: =!"=="" echo !num%%l!
echo.&pause&exit
:loop
for /l %%l in (2 1 !ko!) do set "num%%l=!num%%l! "
goto :eof
致精致简!
Floor 45 Posted 2008-05-08 21:01 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
Originally posted by 26933062 at 2008-5-8 19:30:
The format of floor 39 is correct, but at first glance, your number of columns can be customized? Then aren't a and b not random? If each a and b are random, then the number of columns cannot be predicted.

Then take a closer look...


Customization is nothing special... Because the random result may not be ideal...
If you get a 1!
Then there are 60 a's in one column...

Who told you that the problem is also strange...

The first line cannot have spaces and must be interleaved like ababab

Each a and b are random...

Did you random like this?
Your first line is the maximum number of columns...
Mine is like this...

If you don't modify the default slore, the number of columns and the maximum value are set randomly...

Except for the first line, the rest are all random...
Forum Jump: