【问题标题】:Accelerated C++ Exercise 2.4加速 C++ 练习 2.4
【发布时间】:2020-02-26 19:59:12
【问题描述】:

我已经关注 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;
}

提前感谢大家的帮助

【问题讨论】:

  • 代码应该做什么?你的输入是什么。什么是预期的输出?实际输出是多少?是什么让您认为它不起作用?
  • "我发现它并没有真正起作用,我真的不明白为什么" “不起作用”是什么意思?
  • @Jabberwocky 该程序最初输出一条带有框架的消息,在这个特定的练习中,我应该改变打印框架内空白的方式,从一次打印一个变为写入在单个输出中
  • @AlgirdasPreidžius 当我更改框架的尺寸时,它不会打印一些星号,有时也不会打印框架内的消息
  • @Yata 请将您的准确输入和输出放在问题中。

标签: c++ accelerated-c++


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

    好吧,我花了 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;
    }
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-13
      • 2011-09-27
      • 1970-01-01
      • 2020-10-21
      • 2015-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多