欢迎来到51自学网!

51自学网

当前位置: 主页 > 网络编程 >

VC++实现模拟汉诺塔效果

时间:2018-07-27 15:40来源:网络整理 作者:51自学网
先上效果图 再附上源代码: 汉诺塔: 复制代码 代码如下: #include "stdio.h" #include "math.h" int arrA[15], arrB[15], arrC[15]; // 分别为A、B、C int length; int lenA, lenB, lenC; char plate[32]; // Make void makepl

先上效果

VC++实现模拟汉诺塔效果

再附上源代码:

汉诺塔

复制代码 代码如下:


#include "stdio.h"
#include "math.h"
int arrA[15], arrB[15], arrC[15];   // 分别为A、B、C
int length;
int lenA, lenB, lenC;
char plate[32];
// Make
void makeplate(int n)
{
    int i;
    if (n == length + 1)
    {
        for (i = 0; i < 2 * length + 3; i++)
        {
            if (i == length + 1)
            {
                plate[i] = '|';
            }
            else
            {
                plate[i] = '_';
            }
        }
    }
    else
    {
        if (n == 0)
        {
            for (i = 0; i < 2 * length + 3; i++)
            {
                if (i == length + 1)
                {
                    plate[i] = '|';
                }
                else
                {
                    plate[i] = ' ';
                }
            }
        }
        else
        {
            for (i = 0; i < 2 * length + 3; i++)
            {
                if (i == length + 1)
                {
                    plate[i] = '|';
                }
                else
                {
                    if (i >= length + 1 - n && i <= length || i > length + 1
                        && i <= length + 1 + n)
                    {
                        plate[i] = '_';
                    }
                    else
                    {
                        plate[i] = ' ';
                    }
                }
            }
        }
    }
    plate[i] = '\0';
}
// Draw
void drawtower()
{
    int i;
    printf(" ");
    for (i = length; i >= 0; i--)
    {
        if (i <= lenA)
        {
            makeplate(arrA[i]);
            printf("%s", plate);
        }
        else
        {
            makeplate(0);
            printf("%s", plate);
        }
        if (i <= lenB)
        {
            makeplate(arrB[i]);
            printf("%s", plate);
        }
        else
        {
            makeplate(0);
            printf("%s", plate);
        }
        if (i <= lenC)
        {
            makeplate(arrC[i]);
            printf("%s", plate);
        }
        else
        {
            makeplate(0);
            printf("%s", plate);
        }
        printf("\n ");
    }
}
// Move
void moveplate(int n, char x, char y)
{
    int i, j;
    if (x == 'A')
    {
        lenA--;
    }
    else
    {
        if (x == 'B')
        {
            lenB--;
        }
        else
        {
            lenC--;
        }
    }
    if (y == 'A')
    {
        lenA++;
        arrA[lenA] = n;
    }
    else
    {
        if (y == 'B')
        {
            lenB++;
            arrB[lenB] = n;
        }
        else
        {
            lenC++;
            arrC[lenC] = n;
        }
    }
    drawtower();                // 绘出移动一次后汉诺塔的状态
}
// Print And Move
void printandmove(int n, char x, char y)
{
    printf("\n %d 号盘从 %c 柱移到 %c 柱\n\n", n, x, y);
    moveplate(n, x, y);
}
// Hanoi
void hanoi(int n, char one, char two, char three)
{
    if (n == 1)
    {
        printandmove(n, one, three);
    }
    else
    {
        hanoi(n - 1, one, three, two);
        printandmove(n, one, three);
        hanoi(n - 1, two, one, three);
    }
}
// Main
void main()
{
    int n, i;                   // n为汉诺塔盘子数,如要改变,只需更改初始值即可。
    char one = 'A', two = 'B', three = 'C';
    printf("请输入盘子个数[1—12]:");
    scanf("%d", &n);
    if (n >= 1 && n <= 12)
    {
        length = n;
        lenA = n;
        for (i = 0; i <= lenA; i++)
        {
            arrA[i] = n + 1 - i;
        }
        lenB = lenC = 0;
        arrB[0] = arrC[0] = n + 1;
        printf(" 汉诺塔模拟移动过程[%d个盘]\n\n", n);
        drawtower();            // 绘出汉诺塔初始状态
        hanoi(n, one, two, three);
        printf("\n 模拟结束,共移动%ld次\n", (long)pow(2, n) - 1);
    }
    else
    {
        printf("数据错误!\n");
    }
}

汉诺塔.c

复制代码 代码如下:

(责任编辑:admin)

织梦二维码生成器
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
栏目列表
推荐内容