Unix Desk Calculator
The Towers of Hanoi as a dc (Unix Desk Calculator) program.
Dc is a reverse-polish desk calculator which supports unlimited precision arithmetic. More details are available at the GNU bc home page (dc is part of the bc package).
In order to use the program, use a command sequence such as:
dc -e n hanoi.dc
where n is an integer representing the number of disks.
#
# The Towers Of Hanoi
# dc (Unix Desk Calculator) version
# Copyright (C) 2003 Amit Singh. All Rights Reserved.
# http://hanoi.kernelthread.com
#
# Tested under GNU dc 1.3
#
# Usage: dc -e n hanoi.dc
# where n is the number of disks
[ # move(from, to)
n # print from
[ --> ]n # print " --> "
p # print to\n
sw # p doesn't pop, so get rid of the value
]sm
[ # init(n)
sw # tuck n away temporarily
9 # sentinel as bottom of stack
lw # bring n back
1 # "from" tower's label
3 # "to" tower's label
0 # processed marker
]si
[ # Move()
lt # push to
lf # push from
lmx # call move(from, to)
]sM
[ # code block <d>
ln # push n
lf # push from
lt # push to
1 # push processed marker 1
ln # push n
1 # push 1
- # n - 1
lf # push from
ll # push left
0 # push processed marker 0
]sd
[ # code block <e>
ln # push n
1 # push 1
- # n - 1
ll # push left
lt # push to
0 # push processed marker 0
]se
[ # code block <x>
ln 1 =M
ln 1 !=d
]sx
[ # code block <y>
lMx
lex
]sy
[ # quit()
q # exit the program
]sq
[ # run()
d 9 =q # if stack empty, quit()
sp # processed
st # to
sf # from
sn # n
6 #
lf #
- #
lt #
- # 6 - from - to
sl #
lp 0 =x #
lp 0 !=y #
lrx # loop
]sr
lix # init(n)
lrx # run()