服务器之家

服务器之家 > 正文

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

时间:2021-02-22 12:06     来源/作者:C++教程网

先上效果图

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++实现汉诺塔效果的全部代码了,希望对大家理解汉诺塔算法能够有所帮助。

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部