📄 simtower.cpp
字号:
#include <iostream.h>
#include <stdlib.h>
struct DataType
{
short int retAddr;
int nParam;
char fromParam;
char auxParam;
char toParam;
};
const int MaxStackSize = 10;
#include "SeqStack.h"
void SimTowers(int n, char fromPeg, char auxPeg, char toPeg)
{
DataType currArea;
SeqStack s;
char temp;
short int i;
//当前工作区初始化
currArea.retAddr = 1;
currArea.nParam = n;
currArea.fromParam = fromPeg;
currArea.auxParam = auxPeg;
currArea.toParam = toPeg;
s.Push(currArea);
//以下为模拟出口
start:
if(currArea.nParam == 1)
{
cout << "Move Disk 1 from Peg " << currArea.fromParam
<< " to Peg " << currArea.toParam << endl;
i = currArea.retAddr;
currArea = s.Pop();
switch(i)
{
case 1: goto lable1;
case 2: goto lable2;
case 3: goto lable3;
}
}
//以下模拟递归自调用过程
s.Push(currArea);
currArea.nParam --;
temp = currArea.auxParam;
currArea.auxParam = currArea.toParam;
currArea.toParam = temp;
currArea.retAddr = 2;
goto start;
//以下模拟返回第一次递归调用
lable2:
cout << "Move Disk " << currArea.nParam << " from Peg "
<< currArea.fromParam << " to Peg "
<< currArea.toParam << endl;
s.Push(currArea);
currArea.nParam --;
temp = currArea.fromParam;
currArea.fromParam = currArea.auxParam;
currArea.auxParam = temp;
currArea.retAddr = 3;
goto start;
//以下模拟返回第二次递归调用
lable3:
i = currArea.retAddr;
currArea = s.Pop();
switch(i)
{
case 1: goto lable1;
case 2: goto lable2;
case 3: goto lable3;
}
//以下模拟返回主调函数
lable1:
return;
}
void main(void)
{
int n = 3;
SimTowers(n,'A','B','C');
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -