Professional Documents
Culture Documents
'C' ); sub Hanoi { my( $num, $from, $to, $aux ) = @_; if( $num == 1 ) { print "Move disk $num from $from to $to\n"; } Else { hanoi( $num-1, $from, $aux, $to ); print "Move disk $num from $from to $to\n"; hanoi( $num-1, $aux, $to, $from ); } }
E_NOPARAM=66 # No parameter passed to script. E_BADPARAM=67 # Illegal number of disks passed to script. Moves= # Global variable holding number of moves. # Modifications to original script.
dohanoi() { # Recursive function. case $1 in 0) ;; *) dohanoi "$(($1-1))" $2 $4 $3 echo move $2 "-->" $3 let "Moves += 1" # Modification to original script. dohanoi "$(($1-1))" $4 $3 $2 ;; esac }
case $# in 1) case $(($1>0)) in 1) dohanoi $1 1 3 2 echo "Total moves = $Moves" exit 0; # Must have at least one disk.
;; *) echo "$0: illegal value for number of disks"; exit $E_BADPARAM; ;; esac ;; *) echo "usage: $0 N" echo " Where \"N\" is the number of disks."