24小时热门版块排行榜    

查看: 1521  |  回复: 8

hsfang

新虫 (初入文坛)

[求助] 关于C++中结果输出的问题 已有2人参与

各位大侠,我有一段程序代码可运行,但是结果就是一个黑色框中一闪而过!我希望把这一过程数据反应到一个文档中,怎么样才可以做到呢?谢谢
如下程序:
#include <iostream>
using namespace std;

struct Node{
        char a[3][3];                        //表示状态的数组
        Node * pFather;                        //指向节点的父节点
        Node * pNext;                        //用于建立链表
        int i0;                                        //节点中'0'的位置x坐标
        int j0;                                        //节点中'0'的位置y坐标
};

struct List {
        Node * Head;                //队列头指针
        Node * Tail;                //队列尾指针
};

//初始化List
//参数:指向list的指针
//返回值:无
void InitList(List * pList)
{
        pList->Head = NULL;
        pList->Tail = NULL;
}

//将一个节点pNode入队
//参数:队列指针pList;节点指针pNode
//返回值:无
void InList(List * pList,Node * pNode){
        if (!pList->Head)
        {
                pList->Head = pNode;
                pList->Tail = pNode;
        }
        else
        {
                pList->Tail->pNext = pNode;
                pList->Tail = pNode;
        }
}

//将队列的第一个节点出队
//参数:队列指针pList
//返回值:队列为空,返回NULL;队列不为空,指向出队节点指针
Node * OutList(List * pList){
        Node * temp=NULL;
        if (!pList->Head)
        {
                return NULL;
               
        }
        else
        {
               
                temp = pList->Head;
                if(pList->Head == pList->Tail)                //只有一个节点
                {
                        pList->Head = NULL;
                        pList->Tail = NULL;               
                }
                else
                {
                        pList->Head =temp->pNext;                //不止一个节点
                }
                return temp;
        }
}

//判断两个节点的状态是否一致
//参数:两个指向节点的指针pNode1、pNode2
//返回值:一致true;不一致false
bool IsEqual(Node * pNode1,Node * pNode2)
{
        if(!pNode1 || !pNode2){
                return true;       
        }
        for(int i = 0;i < 3;i++)
        {
                for (int j = 0;j < 3;j++)
                {
                        if(!pNode1->a || !pNode2->a)
                        {
                                return true;       
                        }
                        if (pNode1->a[j] != pNode2->a[j])
                        {
                                return false;
                        }
                }
        }
        return true;
}


//拷贝一个节点的状态到新的节点
//参数:Node的指针:pNew,pOld
//返回值:无
void CopyNode(Node * pNew,Node * pOld)
{
        for(int i = 0;i < 3;i++)
        {
                for (int j = 0;j < 3;j++)
                {
                        pNew->a[j] = pOld->a[j];
                }
        }
}

//给定一个节点,扩展出该节点的所有子节点,返回一个保存所有节点的List
//参数;指向指针节点的指针
//返回值:List指针
List * ExpandSub(Node * pNode){
        //保存移动后,'0'元素的位置坐标
        int i = 0;                       
        int j = 0;

        //声明保存所有节点的List
        List * pSubList;
        pSubList = new List;
        InitList(pSubList);

        for (int iAdd = -1;iAdd <= 1;iAdd++)
        {
                for (int jAdd = -1;jAdd <= 1;jAdd++)
                {
                        //排除增量(-1,-1)、(-1,1)、(0,0)、(1,-1)、(1,1)
                        if(iAdd == 0 &&jAdd == 0)
                        {
                                continue;
                        }
                        if(iAdd != 0 &&jAdd != 0)
                        {
                                continue;
                        }
                        //计算移动后的'0'元素的位置坐标
                        i = pNode->i0 + iAdd;
                        j = pNode->j0 + jAdd;
                        //排除数组越界情况
                        if (i < 0 || j < 0 || i > 2 || j > 2)
                        {
                                continue;
                        }
                        //子节点指针
                        Node * pSub;
                        pSub = new Node;

                        //初始化子节点
                        pSub->i0 = i;
                        pSub->j0 = j;
                        pSub->pFather = pNode;
                        pSub->pNext = NULL;
                       
                        CopyNode(pSub,pNode);

                        pSub->a[pNode->i0][pNode->j0] = pSub->a[j];
                        pSub->a[j] = '0';

                        //子节点入队
                        InList(pSubList,pSub);
                }
        }
        return pSubList;
}

//打印某个节点
//参数:节点指针
//返回值:无
void printNode(Node * pNode)
{
        cout<<"-----------------\n";
        for(int i = 0;i < 3;i++)
        {               
                for (int j = 0;j < 3;j++)
                {
                        cout<<pNode->a[j]<<"\t";
                }
                cout<<endl;
        }
        cout<<"-----------------\n";
}

//打印结果
//参数:Node指针
//返回值:无
void printPath(Node * pResult)
{
        Node * temp;
        temp = pResult;
        while (temp)
        {
                printNode(temp);
                temp = temp->pFather;
        }
}

//搜索某个List中是否出现和节点pNode状态一致的节点
//参数:Node指针,节点指针pNode
//返回值:出现true,没有出现false
bool IsAppeared(Node * pStart,Node * pNode)
{
        Node * temp ;
        temp =  pStart;
        while (temp)
        {
                if(IsEqual(temp,pNode))
                {
                        return true;
                }
                temp = temp->pNext;
        }
        return false;
}

//子节点入队
//参数:Node * pStart,List * ,Node *;
//返回值: 无
void SubNodeInList(Node * pStart,List * list,Node * pOut)
{
        Node * temp;
        Node * temp1;
        //保存子节点队列
        List * pSubList = NULL;
        pSubList = new List;
        InitList(pSubList);

        pSubList = ExpandSub(pOut);


        temp = OutList(pSubList);//取子节点
        while (temp)
        {
                if(IsAppeared(pStart,temp))
                {
                        temp1 = temp;
                        temp = temp->pNext;
                        delete temp1;
                }
                else
                {
                        Node * tempSwap =NULL;
                        tempSwap = new Node;
                        CopyNode(tempSwap,temp);
                        tempSwap->i0 = temp->i0;
                        tempSwap->j0 = temp->j0;
                        tempSwap->pFather = temp->pFather;
                        tempSwap->pNext = NULL;

                        InList(list,tempSwap);
                        temp = temp->pNext;
                }
        }
}

//搜索函数
//参数:原始节点指针pSrc,目的节点指针pDes
//无结果,返回NULL;有结果,返回保存路径的List指针
Node * Search(Node * pSrc,Node * pDes)
{
        //保存队列头节点
        Node * pStart = pSrc;

        //用来保存结果
         Node * pResult = NULL;


         //用来遍历
         List * list = NULL;
         list = new List;
         InitList(list);
         
         //入队
         InList(list,pSrc);

         int i = 1;
         bool flagxx = false;

         Node * pOut = NULL;
         pOut = OutList(list);
         Node * s;
         while (pOut)
         {
                 SubNodeInList(pStart,list,pOut);
                 pOut = OutList(list);
                 if(!flagxx)
                 {
                         pSrc->pNext=pOut;
                         flagxx = true;
                 }                 

                 s = pStart;

                 while(s)
                 {
                         if(IsEqual(s,pDes))
                         {
                                 pResult = s;
                                 return pResult;
                         }
                         s=s->pNext;
                 }
                 //cout<<"\n\n$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n\n";
         }
         return NULL;
}
int main()
{
        Node temp = {{{'1','2','3'},{'4','0','6'},{'7','5','8'}},NULL,NULL,1,1};        //原始状态
        //指向原始状态的指针
        Node * pSrc;
        pSrc = &temp;

        Node TEMP = {{{'1','2','3'},{'4','5','6'},{'7','8','0'}},NULL,NULL,2,2};        //目的状态z
        //指向目的状态的指针
        Node * pDes;
        pDes = &TEMP;

        Node * pResult = NULL;
        pResult = Search(pSrc,pDes);
        if (!pResult)
        {
                cout<<"问题无解!";
        }
        else
        {
                printPath(pResult);
        }


        /*测试函数List * ExpandSub(Node * pNode)
        List * p;
        p = new List;
        InitList(p);
        p = ExpandSub(pSrc);

        printList(p);
        */

        system("pause";
        return 0;
}

[ Last edited by hsfang on 2014-3-18 at 17:01 ]
回复此楼

» 猜你喜欢

» 本主题相关价值贴推荐,对您同样有帮助:

已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

liqizuiyang

木虫 (著名写手)

在Unix系统下运行,将结果重定向到文件。

./foobar.x > output
2楼2014-03-18 19:18:29
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

ifound

新虫 (初入文坛)

【答案】应助回帖


感谢参与,应助指数 +1
hsfang: 金币+1, 有帮助, 还是谢谢你 2014-03-22 11:29:50
楼主的程序是在windows 下运行的吧, 在main函数的最后加上一个等待输入的,黑框就不会一闪而过了,比如  
int a;cin>>a;
3楼2014-03-19 11:01:52
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

houqi1993

新虫 (初入文坛)

【答案】应助回帖


感谢参与,应助指数 +1
hsfang: 金币+1, 有帮助, 这个我还没试出,还是谢谢你 2014-03-22 11:30:22
想保留结果,可以在命令行下运行,并将结果重定向到文件中,在exe目录下,执行:
XXX.exe > result.txt
即可。
4楼2014-03-21 15:19:35
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

houqi1993

新虫 (初入文坛)

【答案】应助回帖

★ ★ ★
hsfang: 金币+3, ★★★很有帮助, 搞定了,谢谢 2014-03-22 11:30:44
如果是想看到结果,可以利用System(pause);
或者也可以利用 getchar()
希望可以帮到你。
5楼2014-03-21 15:20:37
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

hsfang

新虫 (初入文坛)

引用回帖:
2楼: Originally posted by liqizuiyang at 2014-03-18 19:18:29
在Unix系统下运行,将结果重定向到文件。

./foobar.x > output

我是新手,先试试!谢谢
6楼2014-03-22 09:32:26
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

hsfang

新虫 (初入文坛)

引用回帖:
5楼: Originally posted by houqi1993 at 2014-03-21 15:20:37
如果是想看到结果,可以利用System(pause);
或者也可以利用 getchar()
希望可以帮到你。

我是菜鸟,不熟悉!要试试,谢谢
7楼2014-03-22 09:33:42
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

hsfang

新虫 (初入文坛)

引用回帖:
2楼: Originally posted by liqizuiyang at 2014-03-18 19:18:29
在Unix系统下运行,将结果重定向到文件。

./foobar.x > output

windows 下
8楼2014-03-22 09:46:19
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

hsfang

新虫 (初入文坛)

问题已解决,谢谢各位大侠
9楼2014-03-22 11:28:52
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 hsfang 的主题更新
信息提示
请填处理意见