【问题标题】:How to print and modify char in C++如何在 C++ 中打印和修改 char
【发布时间】:2022-01-01 19:37:49
【问题描述】:

我想创建一个将打印“|”的项目字符为 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[] = { '|' , '||' , '|||', '||||'} 并根据用户输入删除,但我失败了。有什么帮助吗?

【问题讨论】:

    标签: c++ arrays visual-studio-2015 character visual-studio-2019


    【解决方案1】:

    这是一个解决方案:

    #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 ] = '\0';
        }
    
        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;
    }
    

    示例输入/输出:

    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
        |
       |||
      |||||
     |||
    |||||||||
    
    

    【讨论】:

    • 非常感谢这对我的项目有很大帮助
    • @Lucky 不客气。你能理解代码在做什么吗?还是我应该在答案中添加一些解释?
    【解决方案2】:

    将您的条形图限制为 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";
        }
    }
    

    【讨论】:

    • 也感谢您的帮助
    • 很高兴为您提供帮助!
    猜你喜欢
    • 1970-01-01
    • 2016-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多