Mittwoch, 8. Februar 2012

Fibonacci in batch

Here a simple programm showing some of the power of batch, just so you get an impression why I think it is a good start to learn how to program:
Fibonacci.bat
1 :: This script prints out all Fibonacci-numbers up
2 :: to a position defined by the user
3 :: Version 1.0
4 :: Date: 2012/01/26
5 :: Author: Lukas Boehm
6 @echo off
7
8 :: Section that asks the user to enter a VALID
9 :: position in the fibonacci-sequence
10 setlocal
11 :input
12 set /p POS=Bitte geben Sie die gewuenschte Stelle in der Folge ein
13 set /a NUM=%POS% + 0
14 echo.
15 if %NUM% neq %POS% (
16 echo Fehler: keine gueltige Zahl!
17 goto input
18 )
19 endlocal & set POS=%POS%
20
21 :: Section that calculates and prints out
22 :: the fibonacci numbers
23 setlocal EnableDelayedExpansion
24 set LAST=0
25 set CURRENT=1
26
27 FOR /L %%N IN (1, 1, %POS%) DO (
28 set SWAP=!CURRENT!
29 set /A CURRENT +=!LAST!
30 echo !LAST!
31 set LAST=!SWAP!
32 )
33 endlocal
34
35 :EOF
36 PAUSE