简体   繁体   English

在 C++ 中以错误的顺序打印堆栈

[英]printing stack in the incorrect order in c++

I have a function that pushes down all non zero integers to the top of my array and then reprints it in a 3x3 matrix.我有一个函数将所有非零整数下推到数组的顶部,然后在 3x3 矩阵中重新打印它。 the problem i am having is when i push all the non zero integers into my stack it reverses the order.我遇到的问题是,当我将所有非零整数推入堆栈时,它会颠倒顺序。

** the indexing of my array is backwards. ** 我的数组的索引是向后的。 ie, in a 3x3 matrix the coordinates (0,0) would be the bottom left **即,在 3x3 矩阵中,坐标 (0,0) 将是左下角 **

here is the relevant code:这是相关代码:

  void State::pushDown() {


  std::stack<int> tempStack;

    for ( int c = 0; c < BOARDSIZE; c++ )
    {
        for ( int r = 0; r < BOARDSIZE ; r++ )
        {
            if ( grid[r][c] != 0 ) tempStack.push( grid[r][c] );
        }

        for ( int r = BOARDSIZE; r != 0; --r )
        {
            if ( !tempStack.empty() )
            {
                grid[r-1][c] = tempStack.top();
                tempStack.pop();
            }
            else
            {
                grid[r-1][c] = 0;
            }
        }
    }

}
State() {
        for (int i = 0; i < BOARDSIZE; i++)
            for (int j = 0; j < BOARDSIZE; j++)
                grid[i][j] = rand() % 7;

    }

void State::printBoard() {
    cout << endl;

    for (int i = 0; i < BOARDSIZE; i++) {
        for (int j = 0; j < BOARDSIZE; j++) {
            cout << " " << grid[BOARDSIZE - i - 1][j] << " ";
        }
        cout << endl;
    }
}

int main() {
    srand(time(0));

    State state;
    state.printBoard();

    state.pushDown();
    state.printBoard();



    return 0;
}

here is my current output:这是我当前的输出:

before push down function前下推功能

045
504
226

after push down function:下推功能后:

006
224
545

as you can see it successfully pushes the non zero elements to the bottom of the matrix however in the process it reverses the order of the other numbers and i believe this is because of the stack.正如你所看到的,它成功地将非零元素推送到矩阵的底部,但是在这个过程中它颠倒了其他数字的顺序,我相信这是因为堆栈。

my expected output would be the following:我的预期输出如下:

before push down function前下推功能

045
504
226

after push down function:下推功能后:

005
544
226

My question is- how can i fix my function so that the order of the elements remain the same without reversing.我的问题是 - 我如何修复我的函数,以便元素的顺序保持不变而不颠倒。

Here is a demonstrative program that shows how the loops can be defined.这是一个演示程序,显示了如何定义循环。

#include <iostream>
#include <stack>

const int BOARDSIZE = 3;

void reformat( int ( &a )[BOARDSIZE][BOARDSIZE] )
{
    std::stack<int> tempStack;

    for ( int c = 0; c < BOARDSIZE; c++ )
    {
        for ( int r = 0; r < BOARDSIZE ; r++ )
        {
            if ( a[r][c] != 0 ) tempStack.push( a[r][c] );
        }

        for ( int r = BOARDSIZE; r != 0; --r )
        {
            if ( !tempStack.empty() )
            {
                a[r-1][c] = tempStack.top();
                tempStack.pop();
            }
            else
            {
                a[r-1][c] = 0;
            }
        }
    }
}

int main() 
{
    int a[BOARDSIZE][BOARDSIZE] =
    {
        { 0, 4, 5 },
        { 5, 0, 4 },
        { 2, 2, 6 }
    };


    for ( const auto &row : a )
    {
        for ( const auto &item : row ) std::cout << item << ' ';
        std::cout << '\n';
    }

    std::cout << '\n';

    reformat( a );

    for ( const auto &row : a )
    {
        for ( const auto &item : row ) std::cout << item << ' ';
        std::cout << '\n';
    }

    std::cout << '\n';

    return 0;
}

Its output is它的输出是

0 4 5 
5 0 4 
2 2 6 

0 0 5 
5 4 4 
2 2 6 

If you are outputting the array starting from its last row then use the following loops如果您从最后一行开始输出数组,则使用以下循环

#include <iostream>
#include <stack>

const int BOARDSIZE = 3;

void reformat( int ( &a )[BOARDSIZE][BOARDSIZE] )
{
    std::stack<int> tempStack;

    for ( int c = 0; c < BOARDSIZE; c++ )
    {
        for ( int r = 0; r < BOARDSIZE ; r++ )
        {
            if ( a[BOARDSIZE-r-1][c] != 0 ) tempStack.push( a[BOARDSIZE-r-1][c] );
        }

        for ( int r = 0; r != BOARDSIZE; ++r )
        {
            if ( !tempStack.empty() )
            {
                a[r][c] = tempStack.top();
                tempStack.pop();
            }
            else
            {
                a[r][c] = 0;
            }
        }
    }
}

int main() 
{
    int a[BOARDSIZE][BOARDSIZE] =
    {
        { 2, 2, 6 },
        { 5, 0, 4 },
        { 0, 4, 5 }
    };


    for ( size_t i = 0; i < BOARDSIZE; i++  )
    {
        for ( const auto &item : a[BOARDSIZE - i - 1] ) std::cout << item << ' ';
        std::cout << '\n';
    }

    std::cout << '\n';

    reformat( a );

     for ( size_t i = 0; i < BOARDSIZE; i++  )
    {
        for ( const auto &item : a[BOARDSIZE - i - 1] ) std::cout << item << ' ';
        std::cout << '\n';
    }

    std::cout << '\n';

    return 0;
}

The program output is the same as shown above程序输出与上图相同

0 4 5 
5 0 4 
2 2 6 

0 0 5 
5 4 4 
2 2 6 

But now the array is outputted starting from its last line.但是现在数组从最后一行开始输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM