File: hanoi.c

package info (click to toggle)
cc65 2.19-2
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 20,268 kB
  • sloc: ansic: 117,151; asm: 66,339; pascal: 4,248; makefile: 1,009; perl: 607
file content (90 lines) | stat: -rw-r--r-- 2,320 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
  !!DESCRIPTION!! solves the "towers of hanoi" problem
  !!ORIGIN!!      BYTE UNIX Benchmarks
  !!LICENCE!!     Public Domain
*/

/*******************************************************************************
 *  The BYTE UNIX Benchmarks - Release 3
 *          Module: hanoi.c   SID: 3.3 5/15/91 19:30:20
 *
 *******************************************************************************
 * Bug reports, patches, comments, suggestions should be sent to:
 *
 *      Ben Smith, Rick Grehan or Tom Yager
 *      ben@bytepb.byte.com   rick_g@bytepb.byte.com   tyager@bytepb.byte.com
 *
 *******************************************************************************
 *  Modification Log:
 *  $Header: hanoi.c,v 3.5 87/08/06 08:11:14 kenj Exp $
 *  August 28, 1990 - Modified timing routines (ty)
 *
 ******************************************************************************/

#define VERBOSE
/*#define USECMDLINE*/

#include <stdio.h>
#include <stdlib.h>

unsigned short iter = 0; /* number of iterations */
char num[4];
long cnt;

int disk=5,       /* default number of disks */
    duration=10;  /* default time to run test */

void mov(unsigned char n,unsigned char f,unsigned char t)
{
char o;

        if(n == 1)
        {
                num[f]--;
                num[t]++;
        }
        else
        {
                o = (6-(f+t));
                mov(n-1,f,o);
                mov(1,f,t);
                mov(n-1,o,t);
        }

        #ifdef VERBOSE
        printf("%2d: %2d %2d %2d %2d\n",
                (int)iter,(int)num[0],(int)num[1],(int)num[2],(int)num[3]);
        #endif
}

int main(int argc,char **argv)
{
        #ifdef USECMDLINE
        if (argc < 2) {
                printf("Usage: %s [duration] [disks]\n", argv[0]);
                exit(EXIT_FAILURE);
        }
        else
        {
                if(argc > 1) duration = atoi(argv[1]);
                if(argc > 2) disk = atoi(argv[2]);
        }
        #endif

        printf("towers of hanoi\ndisks: %d\n\n",disk);

        num[1] = disk;

        #ifdef VERBOSE
        printf("%2d: %2d %2d %2d %2d\n",
                (int)iter,(int)num[0],(int)num[1],(int)num[2],(int)num[3]);
        #endif

        while(num[3]<disk)
        {
                mov(disk,1,3);
                ++iter;
        }

        return 0;
}