1
0
mirror of https://github.com/cc65/cc65.git synced 2024-12-23 19:29:37 +00:00
cc65/test/ref/hanoi.c

91 lines
2.3 KiB
C
Raw Normal View History

2014-09-24 14:45:10 +00:00
/*
!!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:
*
2019-02-12 21:50:49 +00:00
* Ben Smith, Rick Grehan or Tom Yager
* ben@bytepb.byte.com rick_g@bytepb.byte.com tyager@bytepb.byte.com
2014-09-24 14:45:10 +00:00
*
*******************************************************************************
* 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;
2019-02-12 21:50:49 +00:00
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);
}
2014-09-24 14:45:10 +00:00
2019-02-12 21:50:49 +00:00
#ifdef VERBOSE
printf("%2d: %2d %2d %2d %2d\n",
(int)iter,(int)num[0],(int)num[1],(int)num[2],(int)num[3]);
#endif
2014-09-24 14:45:10 +00:00
}
int main(int argc,char **argv)
{
2019-02-12 21:50:49 +00:00
#ifdef USECMDLINE
if (argc < 2) {
printf("Usage: %s [duration] [disks]\n", argv[0]);
exit(EXIT_FAILURE);
2019-02-12 21:50:49 +00:00
}
else
{
if(argc > 1) duration = atoi(argv[1]);
if(argc > 2) disk = atoi(argv[2]);
}
#endif
2014-09-24 14:45:10 +00:00
2019-02-12 21:50:49 +00:00
printf("towers of hanoi\ndisks: %d\n\n",disk);
2014-09-24 14:45:10 +00:00
2019-02-12 21:50:49 +00:00
num[1] = disk;
2014-09-24 14:45:10 +00:00
2019-02-12 21:50:49 +00:00
#ifdef VERBOSE
printf("%2d: %2d %2d %2d %2d\n",
(int)iter,(int)num[0],(int)num[1],(int)num[2],(int)num[3]);
#endif
2014-09-24 14:45:10 +00:00
2019-02-12 21:50:49 +00:00
while(num[3]<disk)
{
mov(disk,1,3);
++iter;
}
2014-09-24 14:45:10 +00:00
2019-02-12 21:50:49 +00:00
return 0;
2014-09-24 14:45:10 +00:00
}