-3

我已经关注 Accelerated C++ 几个星期了,但我一直坚持练习 2.4 一段时间,最后我想我找到了,但在尝试给它不同的维度后,我发现它并没有真正起作用并且我真的不明白为什么

代码最初打印一个框架消息,在这个特定的练习中,我应该将代码一次打印一个字符的空白更改为一次写入所有木板

这是代码:

// [2-0, 2-4] Exercises
#include<iostream>
#include<string>

// saying what standard-library names we use
using std::cout;        using std::endl;
using std::cin;         using std::string;

int main()
{
    // asking for the name
    cout << "Please enter your first name: ";

    // reading the name
    string name;
    cin >> name;

    // building the message that we intend to write
    const string greeting = "Hello, " + name + "!";

    //  2.2 & 2.3 asking for inpadY
    cout << "Please enter the number of padY (Vertical padding): ";

    // 2.2 & 2.3 reading the inpadY
    int inpadY;
    cin >> inpadY;

    //  2.2 & 2.3 asking for inpadX
    cout << "Please enter the number of padX (Horizontal padding): ";

    // 2.2 & 2.3 reading the inpadX
    int inpadX;
    cin >> inpadX;

    // the number of planks surrounding the greeting
    // 2.2 & 2.3 added inpadY as the number of planks;
    const int padY = inpadY;

    // 2.2 & 2.3 added inpadX
    const int padX = inpadX;

    // 2.4 pad size
    const int pad = inpadX + inpadY;

    // the number of rows and columns to write
    const int rows = padY * 2 + 3;
    const string::size_type cols = greeting.size() + padX * 2 + 2;

    // 2.4 creating a padding string left and right and top and bottom
    const string LeftRightPad(padY, ' ');
    const string TopBottomPad(cols - 2, ' ');

    // write a blank line to separate the output and the input
    cout << endl;

    // write rows rows of output
    // invariant: we have written r rows so far
    for (int r = 0; r != rows; ++r) {

        string::size_type c = 0;

        // invariant: we have written c characters so far in the current row
        while (c != cols) {

            // is it time to write the greeting?
            if (r == padY + 1 && c == padX + 1)
            {
                cout << greeting;
                c += greeting.size();
            } else {

                // are we on the border?
                if (r == 0 || r == rows - 1 ||
                    c == 0 || c == cols - 1)
                    {cout << "*";
                    ++c;}
                else
                    // 2.4 typing out the spaces at once
                    {cout << LeftRightPad;
                    c += LeftRightPad.size();}
            }
        }

        cout << endl;
    }

    return 0;
}

编辑有输入和输出

Please enter your first name: Estrogen
Please enter the number of padY (Vertical padding): 2
Please enter the number of padX (Horizontal padding): 2

**********************
*                    *
*                    *
*  Hello, Estrogen!  *
*                    *
*                    *
**********************

Process returned 0 (0x0)   execution time : 3.281 s
Press any key to continue.
Please enter your first name: Estrogen
Please enter the number of padY (Vertical padding): 2
Please enter the number of padX (Horizontal padding): 5

****************************
*                          *
*                          *
*                          *
*                          *
*                          *
****************************

Process returned 0 (0x0)   execution time : 5.098 s
Press any key to continue.
Please enter your first name: Estrogen
Please enter the number of padY (Vertical padding): 3
Please enter the number of padX (Horizontal padding): 2

**********************
*
*
*
*
*
*
*
**********************

Process returned 0 (0x0)   execution time : 4.333 s
Press any key to continue.

更新:我重写了代码,输出是星号的无限循环这里是新代码

#include<iostream>
#include<string>

using std::string;      using std::endl;
using std::cout;        using std::cin;

int main()
{
   cout << "Please enter your first name: ";

   string name;
   cin >> name;

   const string message = "Hello, " + name + "!";

   cout << "Enter the length: ";

   int length;
   cin >> length;

   cout << "Enter the height: ";

   int height;
   cin >> height;

   const int rows = height * 2 + 3;
   const string::size_type cols = message.size() + length * 2 + 2;

   const string TopBottom(cols, '*');
   const string Blank(cols - 2, ' ');
   const string messageblank(cols - 3 - message.size(), ' ');

   cout << endl;


    for (int r = 0; r != rows; ++r) {

        string::size_type c = 0;
        while (c != cols) {

                if ( r == height + 1 && c == length + 1)
                {
                    cout << messageblank << message << messageblank;
                    c += Blank.size();
                } else
                if (r == 0 && c == 0 || r == rows - 1 && c == cols -1)
                {
                    cout << TopBottom;
                    c += TopBottom.size();

                } else
                if ( r != 0 && c == 0 || r != rows -1 && c == cols - 1)
                {
                    cout << "*";
                    ++c;
                } else
                    cout << Blank;
                    c += Blank.size();
        }

    cout << endl;
    }


    return 0;
}

提前谢谢大家的帮助

4

2 回答 2

0

除非代码应该以这种方式编写,否则我会提出另一种逐行方法:

print_frame_row(cols);
for (int i = 0; i < padY; ++i)
    print_v_padding(cols);

print_greeting(padX, greeting);
for (int i = 0; i < padY; ++i)
    print_v_padding(cols);

print_frame_row(cols);

在哪里

void print_frame_row(int cols)
{
    std::cout << std::string(cols, '*') << '\n';
}

void print_v_padding(int cols)
{
    const std::string h_padding(cols - 2, ' ');
    std::cout << '*' << h_padding << "*\n";
}

void print_greeting(int padX, const std::string &msg)
{
    const std::string h_padding(padX, ' ');
    std::cout << '*' << h_padding << msg << h_padding << "*\n";
}

这样你的逻辑就更简单了,不必担心计算列数或决定何时写入每个字符。

于 2020-02-27T07:32:13.380 回答
0

好的,所以我花了 3 天时间,但我终于弄清楚这是工作代码

#include<iostream>
#include<string>

using std::string;      using std::endl;
using std::cout;        using std::cin;

int main()
{
   cout << "Please enter your first name: ";

   string name;
   cin >> name;

   cout << "Enter the length: ";

   int length;
   cin >> length;

   cout << "Enter the height: ";

   int height;
   cin >> height;

   const string message = "Hello, " + name + "!";

   const int rows = height * 2 + 3;
   const string::size_type cols = message.size() + length * 2 + 2;

   const string TopBottom(cols, '*');
   const string Blank(cols - 2, ' ');
   const string messageblank(length, ' ');

   cout << endl;

   for (int r = 0; r != rows; ++r) {

        string::size_type c = 0;
        while (c != cols) {

                if ( r == height + 1 && c == 0)
                {
                    cout << "*" << messageblank << message << messageblank << "*";
                    c += TopBottom.size();

                } else
                if (r == 0 && c == 0 || r == rows - 1 && c == 0)
                {
                    cout << TopBottom;
                    c += TopBottom.size();

                } else
                if ( c == 0 && r != 0 || c == 0 && r != rows - 1)
                {
                    cout << "*" << Blank << "*";
                    c += TopBottom.size();

                }
        }


    cout << endl;
    }

    return 0;
}

于 2020-02-29T16:25:01.613 回答