C/C++教程

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

本文主要是介绍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

复制代码 代码如下:

/* 汉诺塔模拟
   2013-5-13
*/
#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");
    }
}

以上所述就是关于VC++实现汉诺塔效果的全部代码了,希望对大家理解汉诺塔算法能够有所帮助。

这篇关于VC++实现模拟汉诺塔效果的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!