* ******************************************************************************* * * This program illustrates the use of the REENTRANT control to write * a recursive procedure in FORTRAN-86. It solves the "towers of Hanoi" * problem, which is described as follows: * * There are three pegs, labelled "A", "B", and "C". Peg A has a stack of * disks (number provided by the operator) on it; Pegs B and C have none. * Each disk is of a different size, and they are ordered on Peg A by size, * starting with the largest on the bottom. The disks can be moved one at * a time to any other peg as long as no disk is placed on top of another * disk which is smaller in size. * * How can these disks be transferred from Peg A to Peg B? * * * NOTE: Due to the recursive nature of this procedure, it may be necessary * to increase the 8086 stack size for large numbers of disks. See * the iAPX 86,88 Family Utilities User's Guide for more information. * ******************************************************************************* * PROGRAM PROG4 WRITE(6,100) 100 FORMAT('How many disks are to be moved from peg A to peg B: ',$) READ(5,200)NUM 200 FORMAT(I5) CALL HANOI('A','B','C',NUM) END $REENTRANT SUBROUTINE HANOI(FROM,TO,BUFF,NUM) CHARACTER*1 FROM,TO,BUFF IF(NUM .EQ. 0) RETURN CALL HANOI(FROM,BUFF,TO,NUM-1) WRITE(6,100)FROM,TO 100 FORMAT('Move a disk from peg ',A,' to peg ',A) CALL HANOI(BUFF,TO,FROM,NUM-1) END