How to resolve the algorithm Old lady swallowed a fly step by step in the Batch File programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Old lady swallowed a fly step by step in the Batch File programming language

Table of Contents

Problem Statement

Present a program which emits the lyrics to the song   I Knew an Old Lady Who Swallowed a Fly,   taking advantage of the repetitive structure of the song's lyrics. This song has multiple versions with slightly different lyrics, so all these programs might not emit identical output.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Old lady swallowed a fly step by step in the Batch File programming language

Source code in the batch programming language

@echo off
setlocal enabledelayedexpansion

	%== An "ugly" pseudo-array ===%
set pseudo=^
fly/@^
spider/That_wiggled_and_jiggled_and_tickled_inside_her,@^
bird/How_absurd,_to_swallow_a_bird,@^
cat/Imagine_that._She_swallowed_a_cat,@^
dog/What_a_hog_to_swallow_a_dog,@^
goat/She_just_opened_her_throat_and_swallowed_that_goat,@^
cow/I_don't_know_how_she_swallowed_that_cow,@^
horse/She's_dead_of_course...

	%== Counting and seperating... ===%
set str=!pseudo!
:count
if "!str!"=="" goto print_song
for /f "tokens=1,* delims=@" %%A in ("!str!") do (
	set /a cnt+=1
	for /f "tokens=1,2 delims=/" %%C in ("%%A") do (
		set animal!cnt!=%%C
		set comment!cnt!=%%D
	)
	set str=%%B
)
goto count

	%== Print the song ===%
:print_song
for /l %%i in (1,1,!cnt!) do (
	echo There was an old lady who swallowed a !animal%%i!.
	if not "!comment%%i!"=="" echo !comment%%i:_= !	
	if %%i equ !cnt! goto done

	for /l %%j in (%%i,-1,2) do (
		set/a prev=%%j-1
		call set prev_animal=%%animal!prev!%%
		echo She swallowed the !animal%%j! to catch the !prev_animal!.
	)
	echo I don't know why she swallowed the fly.
	echo Perhaps she'll die.
	echo.
)
:done
pause>nul&exit/b 0

  

You may also check:How to resolve the algorithm Currying step by step in the C# programming language
You may also check:How to resolve the algorithm Inheritance/Single step by step in the OxygenBasic programming language
You may also check:How to resolve the algorithm Web scraping step by step in the Rust programming language
You may also check:How to resolve the algorithm Variables step by step in the zkl programming language
You may also check:How to resolve the algorithm Hello world/Newline omission step by step in the Rust programming language