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

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

stack.h内容

#define NULL 0

typedef 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;
}

具体效果如下:


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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值