kernelthread.com

FORTRAN 90

The Towers of Hanoi as a FORTRAN 90 program.

! The Towers Of Hanoi ! FORTRAN 90 ! Copyright (C) 2002 Amit Singh. All Rights Reserved. ! http://hanoi.kernelthread.com ! ! Tested under Intel Fortran Compiler 6.0 for Linux ! Compile as follows: ! ! $IFC_ROOT/compiler60/ia32/bin/ifc hanoi.f -o hanoi -lPEPCF90 ! PROGRAM MAIN INTEGER :: argc INTEGER :: N CHARACTER*128 BUFFER argc = IARGC() IF (argc /= 1) THEN PRINT *, 'usage: hanoi <n>' CALL EXIT() END IF CALL GETARG(1, BUFFER) READ(BUFFER, *) N IF (N < 1) THEN PRINT *, 'usage: hanoi <n> (<n> is a +-ve integer)' CALL EXIT() END IF CALL HANOI(N) END PROGRAM MAIN FUNCTION HANOI(n) RESULT (dummy) INTEGER :: dummy CALL XHANOI(n, 1, 2, 3) dummy = 1 END FUNCTION HANOI FUNCTION MOVEDISK(from, to) RESULT (dummy) INTEGER :: dummy INTEGER, INTENT (IN) :: from, to PRINT *, from, ' --> ', to dummy = 1 END FUNCTION MOVEDISK RECURSIVE FUNCTION XHANOI(n, from, using, to) RESULT (dummy) INTEGER :: dummy INTEGER, INTENT (IN) :: n, from, using, to IF (n < 1) THEN dummy = 1 ELSE dummy = XHANOI(n-1, from, to, using) dummy = MOVEDISK(from, to) dummy = XHANOI(n-1, using, from, to) END IF END FUNCTION XHANOI

Download

hanoi.f