如何在 C++ 中打印和修改 char

How to print and modify char in C++

我想创建一个将打印“|”的项目字符为 4 层 1 3 5 7 类似

   |
  |||
 |||||
|||||||

我为此写了一个 for 循环,代码在这里:

for (int i = 1; i <= 4; i++) {
    //for loop for displaying space
    for (int s = i; s < 4; s++) {
        cout << " ";
    }
    //for loop to display star equal to row number
    for (int j = 1; j <= (2 * i - 1); j++) {
        cout << "|";
    }
    // ending line after each row
    cout << "\n";
}

那么我如何制作一个代码来接受用户输入,例如

cout << "Please enter a row number \n" << "Please enter a column number" << endl;

假设用户输入 2 作为行号 2 作为列号我希望输出类似于

   |
  |
 |||||
|||||||

删除 2 个“|”第二行的字符 首先,我认为将每个字符都放在一个数组中,例如 char arr[] = { '|' , '||' , '|||', '||||'} 并根据用户输入删除,但我失败了。有帮助吗?

将你的金条堆限制在 4 个级别,这应该有效:

  • 您基本上只需要固定大小的条形字符串“|”。
  • 然后从该字符串中删除 n 个连续字符。
  • 您唯一需要计算的是开始删除的起始索引,然后用空格替换 n 个字符。
  • 您可以为 rowcol 边界添加一些检查。

[Demo]

#include <iostream>  // cout
#include <string>

int main()
{
    std::string bars(16, '|');

    auto get_start_deleting_pos = [](int row, int col) {
        if (row == 1) { if (col > 1) { return -1; } return 0; }
        else if (row == 2) { if (col > 3) { return -1; } return col; }
        else if (row == 3) { if (col > 5) { return -1; } return 3 + col; }
        else if (row == 4) { if (col > 7) { return -1; } return 8 + col; }
        else return -1;
    };
    auto print_bars = [&bars]() {
        std::cout << "   " << bars[0] << "\n";
        std::cout << "  "  << bars.substr(1, 3) << "\n";
        std::cout << " "  << bars.substr(4, 5) << "\n";
        std::cout << bars.substr(9) << "\n";
    };

    auto start_deleting_from_row{4};
    auto start_deleting_from_col{1};
    auto num_chars_to_delete{4};
    auto pos{ get_start_deleting_pos(start_deleting_from_row, start_deleting_from_col) };

    if (pos != -1)
    {
        bars.replace(pos, num_chars_to_delete, num_chars_to_delete, ' ');
    }

    print_bars();
}

如果你想要一个更通用的解决方案,用户输入 levelrowcol 开始删除,以及要删除的字符数:

[Demo]

#include <iostream>  // cout
#include <string>

auto get_size_for_levels(int l) { return l*l; }

auto get_index_for_row_and_col(int row, int col) { return (row - 1) * (row - 1) - 1 + col; }

auto get_num_cols_for_row (int row) { return row * 2 - 1; }

auto check_row_and_col(int levels, int row, int col) {
    if (row < 1 or levels < row) { return false; }
    if (col < 1 or get_num_cols_for_row(row) < col) { return false; }
    return true;
}

int main()
{
    auto levels{7};  // levels start at 1
    auto start_deleting_from_row{4};  // rows start at 1
    auto start_deleting_from_col{5};  // cols start at 1
    auto num_chars_to_delete{6};

    std::string bars(get_size_for_levels(levels), '|');

    if (check_row_and_col(levels, start_deleting_from_row, start_deleting_from_col))
    {
        bars.replace(
            get_index_for_row_and_col(start_deleting_from_row, start_deleting_from_col),
            num_chars_to_delete,
            num_chars_to_delete,
            ' ');
    }

    for (int l{1}; l <= levels; ++l)
    {
        std::cout
            << std::string(levels - l, ' ')
            << bars.substr(get_index_for_row_and_col(l, 1), get_num_cols_for_row(l))
            << "\n";
    }
}

这是一个解决方案:

#include <iostream>
#include <vector>


std::size_t getLayerCount( )
{
    std::cout << "How many layers to print: ";
    std::size_t layerCount { };
    std::cin >> layerCount;

    return layerCount;
}

std::vector< std::vector<char> > generateShape( const std::size_t layerCount )
{
    const std::size_t MAX_CHAR_COUNT_IN_A_ROW { layerCount * 2 };
    constexpr char spaceChar { ' ' };

    std::vector< std::vector<char> > shape( layerCount, std::vector<char>( MAX_CHAR_COUNT_IN_A_ROW, spaceChar ) );

    for ( std::size_t row { }; row < layerCount; ++row )
    {
        for ( std::size_t offset { layerCount - row - 1 }; offset < layerCount + row; ++offset )
        {
            shape[ row ][ offset ] = '|';
        }

        shape[ row ][ MAX_CHAR_COUNT_IN_A_ROW - 1 ] = '[=10=]';
    }

    return shape;
}

void printShape( const std::vector< std::vector<char> >& shape )
{
    for ( const auto& row : shape )
    {
        std::cout.write( row.data( ), row.size( ) ).write( "\n", 1 );
    }
}

void deleteSpecificChars( std::vector< std::vector<char> >& shape )
{
    std::cout << "Please enter a row number: ";
    std::size_t rowNumber { };
    std::cin >> rowNumber;

    std::cout << "Please enter a column number: ";
    std::size_t colNumber { };
    std::cin >> colNumber;

    --rowNumber;
    --colNumber;

    const std::size_t layerCount { shape.size( ) };
    const std::size_t posOfFirstCharInRow { layerCount - rowNumber - 1 };
    const std::size_t posOfTargetCharInRow { posOfFirstCharInRow + colNumber };
    const std::size_t posOfLastCharInRow { posOfFirstCharInRow + ( 2 * rowNumber ) };

    for ( std::size_t idx { posOfTargetCharInRow }; idx <= posOfLastCharInRow; ++idx )
    {
        shape[ rowNumber ][ idx ] = ' ';
    }
}

int main( )
{
    const std::size_t layerCount { getLayerCount( ) };

    std::vector< std::vector<char> > shape { generateShape( layerCount ) };

    printShape( shape );

    deleteSpecificChars( shape );

    printShape( shape );

    return 0;
}

样本input/output:

How many layers to print: 4
   |
  |||
 |||||
|||||||
Please enter a row number: 2
Please enter a column number: 2
   |
  |
 |||||
|||||||

另一个:

How many layers to print: 5
    |
   |||
  |||||
 |||||||
|||||||||
Please enter a row number: 4
Please enter a column number: 4
    |
   |||
  |||||
 |||
|||||||||