#include #include #include #define HEIGHT 10 typedef int PEG[HEIGHT]; PEG A, B, C; void set_pegs(int nofdisks); void ht(int nofdisks,PEG origin,PEG help,PEG destination); int remove_top(PEG peg); void put_top(PEG peg,int disk); void move(PEG from,PEG to); void show_pegs(void); char *make_disk(int disk); /* function main -------------------------------------------- */ int main(int argc,char* argv[]) { int nofdisks=5; if (argc != 2) { printf("usage - call \n"); exit(1); } if (sscanf(argv[1],"%d",&nofdisks) != 1) { printf("number of disks must be 2 - %d\n",HEIGHT); exit(0); } if (nofdisks < 2 || nofdisks > HEIGHT) { printf("number of disks must be 2 - %d\n",HEIGHT); exit(0); } set_pegs(nofdisks); show_pegs(); ht(nofdisks,A,B,C); printf("done\n"); return 0; }/*end main*/ /* function set_pegs ---------------------------------- */ void set_pegs(int nofdisks) { int i; if (nofdisks > HEIGHT) { printf("too many disks (%d) for the height of the pegs (%d)\n", nofdisks,HEIGHT); exit(1); } for(i = 0; i < HEIGHT; i++) A[i] = B[i] = C[i] = -1; for(i = 0; i < nofdisks; i++) A[i] = nofdisks-1-i; }/* end set_pegs */ /* function ht ------------------------------------------ */ void ht(int nofdisks,PEG origin,PEG help,PEG destination) { if (nofdisks == 2) { // base case move(origin,help); show_pegs(); move(origin,destination); show_pegs(); move(help,destination); show_pegs(); return; } // recursion ht(nofdisks-1,origin,destination,help); move(origin,destination); show_pegs(); ht(nofdisks-1,help,origin,destination); }/* end ht */ /* function remove_top ------------------------------------ */ int remove_top(PEG peg) { int i, res; for(i = 0; i < HEIGHT; i++) if (peg[i] == -1) break; if (i == 0) { printf("peg is empty\n"); exit(0); } i--; res = peg[i]; peg[i] = -1; return res; }/* end remove_top */ /* function put_top --------------------------------------- */ void put_top(PEG peg,int disk) { int i; for(i = 0; i < HEIGHT; i++) if (peg[i] == -1) break; if (i == HEIGHT) { printf("peg is full\n"); exit(1); } peg[i] = disk; }/* end put_top */ /* function move ---------------------------------------------- */ void move(PEG from,PEG to) { int disk; disk = remove_top(from); put_top(to,disk); }/* end move */ /* function show_pegs ----------------------------------------- */ void show_pegs(void) { int i; for(i = HEIGHT-1; i >= 0; i--) { printf("%s",make_disk(A[i])); printf(" %s",make_disk(B[i])); printf(" %s\n",make_disk(C[i])); } printf("%s",make_disk(-2)); printf(" %s",make_disk(-2)); printf(" %s\n",make_disk(-2)); fflush(stdout); getchar(); fflush(stdin); }/* end show_pegs */ /* function make_disk -------------------------------------- */ char *make_disk(int disk) { static char buf[26]; int i, j; if (disk == -2) /* make base */ strcpy(buf,"HHHHHHHHHHHHHHHHHHHHHHHHH"); else if (disk == -1) /* make peg */ strcpy(buf," | "); else{ for(j = 0; j < 1+HEIGHT-disk; buf[j++] = ' '); for(i = 0; i <= disk; buf[j++] = '=',i++); buf[j++] = '|'; for(i = 0; i <= disk; buf[j++] = '=',i++); for(i = 0; i < 1+HEIGHT-disk; buf[j++] = ' ',i++); buf[j] = '\0'; } return &buf[0]; }/* end make_disk */