Übersicht > Türme von Hanoi > COBOL

Obwohl COBOL keine Ansätze zur Rekursion hat, ist es in der Wirtschaft sehr verbreitet. Die Programmierung ist daher ähnlich aufwendig wie in Brainfuck. Wenn man die beiden Listings vergleicht, kann man erstaunliche Gemeinsamkeiten feststellen ...

Hinweis: Dieses Programm habe ich nicht selbstgeschrieben, sondern nur kopiert und so geändert, daß es läuft. (Es kursieren zwei verschiedene Versionen im Netz.)


Die Türme von Hanoi in COBOL

IDENTIFICATION DIVISION.
PROGRAM-ID. HANOI.
 
ENVIRONMENT DIVISION.
 
CONFIGURATION SECTION.
SOURCE-COMPUTER. ALMOST-PORTABLE.
OBJECT-COMPUTER. ALMOST-PORTABLE.
 
 
DATA DIVISION.
 
WORKING-STORAGE SECTION.
 
01  STACK-SPACE.
    02  STACK-PTR               PIC S9(3)  COMP.
    02  STACK-ITEM                         OCCURS 99 TIMES.
        03  DISK-NBR            PIC 9(2).
        03  SOURCE-ROD          PIC X(6).
        03  DEST-ROD            PIC X(6).
        03  SPARE-ROD           PIC X(6).
        03  WHAT                PIC 9(1).
 
01  LOCAL-VARIABLES.
    02  THE-DISK-NBR            PIC 9(2).
    02  THE-SOURCE-ROD          PIC X(6)   VALUE "SOURCE".
    02  THE-DEST-ROD            PIC X(6)   VALUE "DEST".
    02  THE-SPARE-ROD           PIC X(6)   VALUE "SPARE".
    02  THE-WHAT                PIC 9(1).
 
01  GLOBAL-VARIABLES.
    02  SWAP-ROD                PIC X(6).
    02  WHAT-TO-DO              PIC 9(1).
 
 
PROCEDURE DIVISION.
BEGIN-PROGRAM.
    DISPLAY "NBR OF DISKS = " NO ADVANCING
    ACCEPT THE-DISK-NBR
 
    IF THE-DISK-NBR > ZERO
        MOVE 1 TO STACK-PTR, WHAT-TO-DO
        MOVE LOCAL-VARIABLES TO STACK-ITEM (1)
        PERFORM MOVE-DISK
          UNTIL STACK-PTR = ZERO
    .
    STOP RUN
    .
 
MOVE-DISK.
    MOVE STACK-ITEM (STACK-PTR) TO LOCAL-VARIABLES
    IF WHAT-TO-DO = 1
        PERFORM MOVE-DISKS-AWAY
    ELSE
    IF WHAT-TO-DO = 2
        PERFORM SHOW-DISK-MOVED
    ELSE
    IF WHAT-TO-DO = 3
        PERFORM MOVE-DISKS-BACK
    ELSE
        MOVE WHAT (STACK-PTR) TO WHAT-TO-DO
        SUBTRACT 1 FROM STACK-PTR
    .
 
MOVE-DISKS-AWAY.
    MOVE THE-SPARE-ROD TO SWAP-ROD
    MOVE THE-DEST-ROD  TO THE-SPARE-ROD
    MOVE SWAP-ROD      TO THE-DEST-ROD
    PERFORM MOVE-THE-DISKS
    .
 
MOVE-THE-DISKS.
    ADD 1 TO WHAT-TO-DO
    IF THE-DISK-NBR > 1
        SUBTRACT 1 FROM THE-DISK-NBR
        MOVE WHAT-TO-DO TO THE-WHAT
        ADD 1 TO STACK-PTR
        MOVE LOCAL-VARIABLES TO STACK-ITEM (STACK-PTR)
        MOVE 1 TO WHAT-TO-DO
    .
 
SHOW-DISK-MOVED.
    ADD 1 TO WHAT-TO-DO
    DISPLAY "MOVE DISK " THE-DISK-NBR
              " FROM "   THE-SOURCE-ROD
               " TO "    THE-DEST-ROD
    .
 
MOVE-DISKS-BACK.
    MOVE THE-SPARE-ROD  TO SWAP-ROD
    MOVE THE-SOURCE-ROD TO THE-SPARE-ROD
    MOVE SWAP-ROD       TO THE-SOURCE-ROD
    PERFORM MOVE-THE-DISKS
    .