簡體   English   中英

加速 C++ 練習 2.4

[英]Accelerated C++ Exercise 2.4

我已經關注 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;
}

提前感謝大家的幫助

除非代碼應該以這種方式編寫,否則我會提出另一種逐行方法:

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";
}

這樣你就有了一個更簡單的邏輯,而不必擔心計算列數或決定何時寫每個字符。

好的,所以我花了 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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM