西卡C语言汉诺塔演示程序

来源:互联网 发布:js haschildnodes 编辑:程序博客网 时间:2024/05/16 14:31

下面是西卡学院C语言汉诺塔演示程序。以前学习没有了解那里清楚,发现数据结构的应用很广啊。显示几个栈底是复制的别人的代码,再此表示感谢了。(VC++6.0下面可以运行)

stack.h内容

#define NULL 0typedef int ElementType;typedef struct{   ElementType *pbuffer;   int max;   int top;}Stack;Stack *InitStack(int n);int Push(Stack *sp,ElementType *pdata);int Pop(Stack *sp,ElementType *pdata);int DestroyStack(Stack *sp);int IsEmpty(Stack *sp);int IsFull(Stack *sp);int TravereStack(Stack *sp,int (*pfn)(ElementType *pdata,int ,int ),int x,int y);

stack.c内容

#include "stack.h"#include <malloc.h>#include <stdio.h>#include <string.h>Stack *InitStack(int n){    Stack *sp = NULL;sp = (Stack *)malloc(sizeof(Stack));if(!sp){   return sp;}    sp->pbuffer = (ElementType *)malloc(sizeof(ElementType)*n);if(!sp->pbuffer){   free(sp);   sp=NULL;   return sp;}sp->max = n;sp->top = -1;    return sp;}int Push(Stack *sp,ElementType *pdata){   if(IsFull(sp))   {       return 0;   }      sp->top++;   //sp->pbuffer[sp->top] = *pdata;   memcpy(sp->pbuffer + sp->top,pdata,sizeof(ElementType));   return 1;}int Pop(Stack *sp,ElementType *pdata){   if(IsEmpty(sp))   {       return 0;   }   *pdata = sp->pbuffer[sp->top];   sp->top--;   return 1;}int DestroyStack(Stack *sp){   if(sp)   {      free(sp->pbuffer);  free(sp);  return 1;   }   return 0;}int IsEmpty(Stack *sp){   return sp->top == -1;}int IsFull(Stack *sp){   return sp->top == sp->max;}int TravereStack(Stack *sp,int (*pfn)(ElementType *pdata,int x,int y),int x,int y){   int i =0;   for(i=0;i<sp->top+1;i++)   {      pfn(sp->pbuffer+i,x,y);  y--;   }   printf("\n");   return 1;}

汉诺塔主体函数,没有进行优化。

#include "stack.h"#include <stdio.h>#include <malloc.h>#include <windows.h>#include <conio.h>int step = 0;typedef struct{   Stack * sp[3];   int x[3];   int y;   int total;}Hannuota;void gotoxy(int x,int y){COORD C;C.X = x;C.Y = y;SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),C);}void FreeThreeStack(Hannuota *han){    int i =0;for(;i<3;i++){    if(han->sp[i]){    free(han->sp[i]);han->sp[i] =0;}}}int DestroyHannuota(Hannuota *han){    if(han){   FreeThreeStack(han);   free(han);   return 1;}return 0;}Hannuota * InitHannuota(int n){int i =0;    Hannuota * han = NULL;han = (Hannuota *)malloc(sizeof(Hannuota));    if(!han){    return han;}han->total = n;han->y = 10;    for(i=0;i<3;i++){       han->sp[i] = 0;   han->x[i] = 10*i+10;}    for(i=0;i<3;i++){   han->sp[i] = InitStack(han->total);      if(!han->sp[i])  {   DestroyHannuota(han);   return han;  }}    return han;}int ShowElement(ElementType *pdata,int x,int y){   gotoxy(x,y);   printf("%d",*pdata);   return 1;}int ShowFace(Hannuota * ph){        int i ;        gotoxy(8,3);        printf("Hanio   Game   Ver 0.1");        for(i=0 ; i<3 ;i++)        {                gotoxy(ph->x[i] - 2 , ph->y + 1);                printf("-----");                gotoxy(ph->x[i] , ph->y + 2);                printf("%c",0x41 + i);        }        return 1;}int ShowHannuota(Hannuota *han){    int i =0;    //clrscr();system("CLS");ShowFace(han);    for(;i<3;i++)    {      TravereStack(han->sp[i],ShowElement,han->x[i],han->y);    }    gotoxy(8,14);    printf("Step is No. %d ",step++);getch();return 1;}int ChangeData(Hannuota *han,int a,int b){    ElementType data;Pop(han->sp[a],&data);Push(han->sp[b],&data);    ShowHannuota(han);//getchar();return 1;}void Mov(Hannuota *han,int num,int a,int b,int c){    if(num<1){    return;}    Mov(han,num-1,a,c,b);    ChangeData(han,a,c);    Mov(han,num-1,b,a,c);}int GameStart(Hannuota *han){      int i = han->total; for(;i>0;i--) {     Push(han->sp[0],&i); }      ShowHannuota(han); Mov(han,han->total,0,1,2); return 1;}int main(){    Hannuota *han = InitHannuota(3);if(!han){    return 0;}            GameStart(han);DestroyHannuota(han);    return 1;}

具体效果如下:


面向对象的实现,请大牛赐教。

原创粉丝点击