当前位置: 代码迷 >> 汇编语言 >> C++有关问题,如何分屏输出程序结果
  详细解决方案

C++有关问题,如何分屏输出程序结果

热度:7108   发布时间:2013-02-26 00:00:00.0
C++问题,怎么分屏输出程序结果?
这是个马踏棋盘的实现程序,可是我运行的时候是把所有的结果全部显示了
问怎么样修改能使结果分屏输出?就是按任意一个键输出结果的一部分,一直到结果显示结束?
#include   <iostream.h>
const   N=8;const   N=8;
const   MaxSize=N+2*2;
class   Position  
{
public:
int   x,y;
Position   (int   x=1,int   y=1)
{
this-> x=x;
this-> y=y;
}
};
class   Horse
{
private:
int   mat[MaxSize][MaxSize];
bool   show;
int   get(Position   p);
void   set(Position   p,int   i);
bool   isValid(Position   p);    
Position   goaStep(Position   p,int   k);    
int   select(Position   p);    
void   output();
public:
Horse(bool   show=false);
~Horse(){}
void   play(int   x,int   y);
};

Horse::Horse(bool   show)
{
this-> show=show;
for(int   i=0;i <MaxSize;i++)
for(int   j=0;j <MaxSize;j++)
if(i> 1   &&   i <MaxSize-2   &&   j> 1   &&   j <MaxSize-2)
mat[i][j]=0;
else
mat[i][j]=-1;
}
int   Horse::get(Position   p)
{
return   mat[p.x+1][p.y+1];
}
void   Horse::set(Position   p,int   i)
{
mat[p.x+1][p.y+1]=i;
}
void   Horse::play(int   x,int   y)
{
Position   current(x,y);
int   count=1;
int   k=1;
while(count <=N*N   &&   k!=0)
{
set(current,count);
cout < < "第 " < <count < < "步 ";
k=select(current);
if(k==0   &&   count <N*N)
cout < < "第 " < <count < < "步无路可走 ";
else
{
count++;
current=goaStep(current,k);
}
}

}
bool   Horse::isValid(Position   p)
{
        return   (get(p)==0);
}
Position   Horse::goaStep(Position   p,int   k)
{
int   x=p.x;
int   y=p.y;
switch(k)
{
case   1:   x-=2;   y++;   break;
        case   2:   x--;   y+=2;   break;
case   3:   x++;   y+=2;   break;
case   4:   x+=2;   y++;   break;
case   5:   x+=2;   y--;   break;
case   6:   x++;   y-=2;   break;
case   7:   x--;   y-=2;   break;
case   8:   x-=2;   y--;   break;
}
Position   q(x,y);
return   q;
}
int   Horse::select(Position   p)
{
int   i=0,j=0,k=0,road=0,minroad=8;
Position   next1,next2;
cout < < "当前位置:( " < <p.x < < ", " < <p.y < < ") " < <endl;
output();
cout < < "方向         下一位置       可通方向         可通路数 " < <endl;
for(i=1;i <=8;i++)
{
road=0;
next1=goaStep(p,i);
if(isValid(next1))
{  
cout < < "     " < <i < < "\t( " < <next1.x < < ", " < <next1.y < < ")\t ";
for(j=1;j <=8;j++)
{
next2=goaStep(next1,j);
if(isValid(next2))
{
road++;
cout < < "       " < <j;
  相关解决方案