VC++實現(xiàn)模擬漢諾塔效果
先上效果圖
再附上源代碼:
漢諾塔:
#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(); // 繪出移動一次后漢諾塔的狀態(tài)
}
// 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為漢諾塔盤子數(shù),如要改變,只需更改初始值即可。
char one = 'A', two = 'B', three = 'C';
printf("請輸入盤子個數(shù)[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(); // 繪出漢諾塔初始狀態(tài)
hanoi(n, one, two, three);
printf("\n 模擬結(jié)束,共移動%ld次\n", (long)pow(2, n) - 1);
}
else
{
printf("數(shù)據(jù)錯誤!\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(); // 繪出移動一次后漢諾塔的狀態(tài)
}
// 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為漢諾塔盤子數(shù),如要改變,只需更改初始值即可。
char one = 'A', two = 'B', three = 'C';
printf("請輸入盤子個數(shù)[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(); // 繪出漢諾塔初始狀態(tài)
hanoi(n, one, two, three);
printf("\n 模擬結(jié)束,共移動%ld次\n", (long)pow(2, n) - 1);
}
else
{
printf("數(shù)據(jù)錯誤!\n");
}
}
以上所述就是關(guān)于VC++實現(xiàn)漢諾塔效果的全部代碼了,希望對大家理解漢諾塔算法能夠有所幫助。
上一篇:C語言中字符和字符串處理(ANSI字符和Unicode字符)
欄 目:C語言
本文標(biāo)題:VC++實現(xiàn)模擬漢諾塔效果
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/3147.html
您可能感興趣的文章
- 01-10數(shù)據(jù)結(jié)構(gòu)課程設(shè)計-用棧實現(xiàn)表達式求值的方法詳解
- 01-10使用OpenGL實現(xiàn)3D立體顯示的程序代碼
- 01-10求斐波那契(Fibonacci)數(shù)列通項的七種實現(xiàn)方法
- 01-10C語言 解決不用+、-、&#215;、&#247;數(shù)字運算符做加法
- 01-10使用C++實現(xiàn)全排列算法的方法詳解
- 01-10用C++實現(xiàn)DBSCAN聚類算法
- 01-10深入全排列算法及其實現(xiàn)方法
- 01-10全排列算法的非遞歸實現(xiàn)與遞歸實現(xiàn)的方法(C++)
- 01-10用C語言實現(xiàn)單鏈表的各種操作(一)
- 01-10用C語言實現(xiàn)單鏈表的各種操作(二)


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機閱讀
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 04-02jquery與jsp,用jquery
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-10C#中split用法實例總結(jié)
- 01-10delphi制作wav文件的方法
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置