Insanity must be setting in; I’ve been trying to solve RosettaCode tasks with Windows CMD.EXE batch scripts. For example, their A+B task:
A+B - in programming contests, classic problem, which is given so contestants can gain familiarity with online judging system being used.
A+B is one of few problems on contests, which traditionally lacks fabula.
Problem statement Given 2 integer numbers, A and B. One needs to find their sum.
- Input data
- Two integer numbers are written in the input stream, separated by space.
- Output data
- The required output is one integer: the sum of A and B.
- Example:
Input Output
2 2 4
3 2 5
These are what I posted as solutions:
Prompts version
::aplusb.cmd @echo off setlocal set /p a="A: " set /p b="B: " set /a c=a+b echo %c% endlocal
All on the commandline version
::aplusb.cmd @echo off setlocal set a=%1 set b=%2 set /a c=a+b echo %c% endlocal
Formula on the command line version
::aplusb.cmd @echo off setlocal set /a c=%~1 echo %c% endlocal
Example of 'Formula on the command line version'
>aplusb 123+456 579 >aplusb "1+999" 1000
Parse the input stream version (thanks to Tom Lavedas on alt.msdos.batch.nt)
::aplusb.cmd @echo off setlocal set /p a="Input stream: " call :add %a% echo %res% endlocal goto :eof :add set /a res=res+%1 shift if "%1" neq "" goto :add
Example of 'parse the input stream version'
>aplusb Input stream: 1234 5678 6912 >aplusb Input stream: 123 234 345 456 567 678 789 890 4082
Batch files are a bit arcane, it’s true, but the extensions added post Windows 2000 make it a lot more entertaining.