#include <iostream>
#include <afxwin.h>
#include <stack>
#include <stdio.h>
using namespace std;
#define nMax 100;
stack<int> Src, Mid, Tar;
void putOutMove()
{
cout<<_T("当前的堆栈为:\n");
cout<<_T("原堆栈为:");
stack<int> Tempstack=Src;
while (!Tempstack.empty())
{
cout<<Tempstack.top()<<_T(" ");
Tempstack.pop();
}
cout<<endl;
cout<<_T("中间堆栈为:");
Tempstack=Mid;
while (!Tempstack.empty())
{
cout<<Tempstack.top()<<_T(" ");
Tempstack.pop();
}
cout<<endl;
cout<<_T("目标堆栈为:");
Tempstack=Tar;
while (!Tempstack.empty())
{
cout<<Tempstack.top()<<_T(" ");
Tempstack.pop();
}
cout<<endl<<_T("---------------------------------")<<endl;
}
int GetTop(stack<int> astack)
{
int nTop=nMax;
if (!astack.empty())
{
nTop=astack.top();
}
return nTop;
}
void move(stack<int> &SrcStack, stack<int> &TarStack)
{
if (!SrcStack.empty())
{
int nSrc=GetTop(SrcStack);
int nTarTop=GetTop(TarStack);
if(nTarTop>nSrc)
{
SrcStack.pop();
TarStack.push(nSrc);
}
}
putOutMove();
}
stack<int> StackTop(stack<int> &SrcStack, int nRemain)
{
if (nRemain<=0)
{
return SrcStack;
}
stack<int> StackTemp;
while (SrcStack.size()>nRemain)
{
int nSrcTop=GetTop(SrcStack);
SrcStack.pop();
StackTemp.push(nSrcTop);
}
stack<int> TarStack;
while (!StackTemp.empty())
{
int nTop=GetTop(StackTemp);
StackTemp.pop();
TarStack.push(nTop);
}
return TarStack;
}
bool moveStackScrToTar(stack<int> &SrcStack, stack<int> &MidStack, stack<int> &TarStack)
{
if (SrcStack.empty() /*&& MidStack.empty()*/)
{
return true;
}
stack<int> StackTemp=StackTop(SrcStack, 1);
moveStackScrToTar(StackTemp, TarStack, MidStack);
move(SrcStack, TarStack);
moveStackScrToTar(MidStack, SrcStack, TarStack);
return true;
}
int main()
{
int n;
while(1)
{
cout<<_T("请输入一个整数");
cin>>n;
for (int i=n; i>0; i--)
{
Src.push(i);
}
putOutMove();
moveStackScrToTar(Src, Mid, Tar);
}
return 0;
}
#include <afxwin.h>
#include <stack>
#include <stdio.h>
using namespace std;
#define nMax 100;
stack<int> Src, Mid, Tar;
void putOutMove()
{
cout<<_T("当前的堆栈为:\n");
cout<<_T("原堆栈为:");
stack<int> Tempstack=Src;
while (!Tempstack.empty())
{
cout<<Tempstack.top()<<_T(" ");
Tempstack.pop();
}
cout<<endl;
cout<<_T("中间堆栈为:");
Tempstack=Mid;
while (!Tempstack.empty())
{
cout<<Tempstack.top()<<_T(" ");
Tempstack.pop();
}
cout<<endl;
cout<<_T("目标堆栈为:");
Tempstack=Tar;
while (!Tempstack.empty())
{
cout<<Tempstack.top()<<_T(" ");
Tempstack.pop();
}
cout<<endl<<_T("---------------------------------")<<endl;
}
int GetTop(stack<int> astack)
{
int nTop=nMax;
if (!astack.empty())
{
nTop=astack.top();
}
return nTop;
}
void move(stack<int> &SrcStack, stack<int> &TarStack)
{
if (!SrcStack.empty())
{
int nSrc=GetTop(SrcStack);
int nTarTop=GetTop(TarStack);
if(nTarTop>nSrc)
{
SrcStack.pop();
TarStack.push(nSrc);
}
}
putOutMove();
}
stack<int> StackTop(stack<int> &SrcStack, int nRemain)
{
if (nRemain<=0)
{
return SrcStack;
}
stack<int> StackTemp;
while (SrcStack.size()>nRemain)
{
int nSrcTop=GetTop(SrcStack);
SrcStack.pop();
StackTemp.push(nSrcTop);
}
stack<int> TarStack;
while (!StackTemp.empty())
{
int nTop=GetTop(StackTemp);
StackTemp.pop();
TarStack.push(nTop);
}
return TarStack;
}
bool moveStackScrToTar(stack<int> &SrcStack, stack<int> &MidStack, stack<int> &TarStack)
{
if (SrcStack.empty() /*&& MidStack.empty()*/)
{
return true;
}
stack<int> StackTemp=StackTop(SrcStack, 1);
moveStackScrToTar(StackTemp, TarStack, MidStack);
move(SrcStack, TarStack);
moveStackScrToTar(MidStack, SrcStack, TarStack);
return true;
}
int main()
{
int n;
while(1)
{
cout<<_T("请输入一个整数");
cin>>n;
for (int i=n; i>0; i--)
{
Src.push(i);
}
putOutMove();
moveStackScrToTar(Src, Mid, Tar);
}
return 0;
}