Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何避免在同一行上打印?_C++_Loops_For Loop - Fatal编程技术网

C++ 如何避免在同一行上打印?

C++ 如何避免在同一行上打印?,c++,loops,for-loop,C++,Loops,For Loop,我试图从X和O创建一个正方形(边界是X,内部是O),我试图避免在同一行上使用for循环打印的最后一位代码。任何帮助都将不胜感激 #include <iostream> using namespace std; int main() { int x = 0; cout << "Please enter an integer for the dimension that will represent the " "rows and columns

我试图从X和O创建一个正方形(边界是X,内部是O),我试图避免在同一行上使用for循环打印的最后一位代码。任何帮助都将不胜感激

#include <iostream>
using namespace std;

int main() {
  int x = 0;
  cout << "Please enter an integer for the dimension that will represent the "
          "rows and columns: "
       << endl;
  cin >> x;
  cout << endl;

  for (int i = 0; i <= x; i++) {
    for (int j = x; j > i; j--)
      cout << "X";
    cout << endl;
  }

  for (int i = 0; i < x; i++) {
    for (int j = 0; j < i; j++) {
      cout << " ";
    }
    for (int k = x; k >= i + 1; k--) {
      cout << "X";
    }
    cout << endl;
  }
  cout << endl;

  for (int m = 0; m < x; m++) {
    cout << "X";
  }
  for (int m = 0; m < x - 2; m++) {
    cout << "X";
    for (int n = 0; n < x - 2; n++) {
      cout << "O";
    }
  }
  for (int m = 0; m < x; m++) {
    cout << "X";
  }
  cout << endl;

  return 0;
}
#包括
使用名称空间std;
int main(){
int x=0;
cout x;
库特
  • 将您的代码拆分为能够完成最简单任务的函数(根据俗语“分而治之”)
  • 避免重复代码
  • 我会这样做:

    #include <iostream>
    #include <sstream>
    
    static void printEdge(const size_t length) {
        std::stringstream out;
        for(size_t idx{}; idx < length; ++idx) {
            out << "X" << ((idx +1 < length) ? " " : "" );
        }
        std::cout << out.str() << std::endl;
    }
    
    static void printLine(const size_t length) {
        std::stringstream out;
        out << "X ";
        for(size_t idx{}; idx < length-2; ++idx) {
            out << "O" << ((idx +1 < length-2) ? " " : "" );
        }
        out << " X";
        std::cout << out.str() << std::endl;
    }
    
    static void printRectangle(const size_t length) {
        printEdge(length);
        for(size_t idx{}; idx < length -2; ++idx) {
            printLine(length);
        }
        printEdge(length);
    }
    
    
    int main() {
        int x = 0;
        std::cout
            << "Please enter an integer for the dimension that will represent the rows and columns: "
            << std::endl;
        std::cin >> x;
        printRectangle(x);
    }
    
    #包括
    #包括
    静态空白打印边(常量大小\u t长度){
    std::stringout;
    对于(大小\u t idx{};idx将您的代码拆分为能够完成最简单任务的函数(根据俗语“分而治之”)
    
  • 避免重复代码
  • 我会这样做:

    #include <iostream>
    #include <sstream>
    
    static void printEdge(const size_t length) {
        std::stringstream out;
        for(size_t idx{}; idx < length; ++idx) {
            out << "X" << ((idx +1 < length) ? " " : "" );
        }
        std::cout << out.str() << std::endl;
    }
    
    static void printLine(const size_t length) {
        std::stringstream out;
        out << "X ";
        for(size_t idx{}; idx < length-2; ++idx) {
            out << "O" << ((idx +1 < length-2) ? " " : "" );
        }
        out << " X";
        std::cout << out.str() << std::endl;
    }
    
    static void printRectangle(const size_t length) {
        printEdge(length);
        for(size_t idx{}; idx < length -2; ++idx) {
            printLine(length);
        }
        printEdge(length);
    }
    
    
    int main() {
        int x = 0;
        std::cout
            << "Please enter an integer for the dimension that will represent the rows and columns: "
            << std::endl;
        std::cin >> x;
        printRectangle(x);
    }
    
    #包括
    #包括
    静态空白打印边(常量大小\u t长度){
    std::stringout;
    对于(大小\u t idx{};idx
    我不确定我是否完全理解了您的问题,但您只需使用
    \n
    (转义n将被解释为换行符)在新行上打印即可。
    例如:

    std::cout << '\n'; // Add a new line
    
    void drawSquare(size_t s)
    {
        drawSquareShape(s, s);
    }
    
    在控制台中绘制以下输出:

    X X X X X X X X
    X O O O O O O X
    X O O O O O O X
    X O O O O O O X
    X X X X X X X X
    

    编辑:

    我知道这是一个长方形而不是正方形,但是很容易修改/专门化此函数来绘制正方形。
    例如:

    std::cout << '\n'; // Add a new line
    
    void drawSquare(size_t s)
    {
        drawSquareShape(s, s);
    }
    
    如何避免在同一行上打印

    我不确定我是否完全理解了您的问题,但您只需使用
    \n
    (转义n将被解释为换行符)在新行上打印即可。
    例如:

    std::cout << '\n'; // Add a new line
    
    void drawSquare(size_t s)
    {
        drawSquareShape(s, s);
    }
    
    在控制台中绘制以下输出:

    X X X X X X X X
    X O O O O O O X
    X O O O O O O X
    X O O O O O O X
    X X X X X X X X
    

    编辑:

    我知道这是一个长方形而不是正方形,但是很容易修改/专门化此函数来绘制正方形。
    例如:

    std::cout << '\n'; // Add a new line
    
    void drawSquare(size_t s)
    {
        drawSquareShape(s, s);
    }
    

    似乎您指的是上一个模式输出的以下版本

    for (int m = 0; m < x; m++){
            cout << "X";
        }
        cout << endl;
        ^^^^^^^^^^^^^
        for (int m = 0; m < x - 2; m++){
            cout << "X";
            for (int n = 0; n < x - 2; n++){
                cout << "O";
            }
            cout << "X" << endl;
            ^^^^^^^^^^^^^^^^^^^^
        }
        for (int m = 0; m < x; m++){
            cout << "X";
        }
    cout << endl;
    

    似乎您指的是上一个模式输出的以下版本

    for (int m = 0; m < x; m++){
            cout << "X";
        }
        cout << endl;
        ^^^^^^^^^^^^^
        for (int m = 0; m < x - 2; m++){
            cout << "X";
            for (int n = 0; n < x - 2; n++){
                cout << "O";
            }
            cout << "X" << endl;
            ^^^^^^^^^^^^^^^^^^^^
        }
        for (int m = 0; m < x; m++){
            cout << "X";
        }
    cout << endl;
    

    请包括应用程序的示例输出以及您希望正确输出的内容。因此,我建议不要对新行使用
    std::endl
    ,因为这包括一个额外的
    std::flush
    。只需对新行使用
    '\n'
    。您所说的最后一位代码是什么意思?顺便说一下,您可以查看de>使用名称空间std;
    是邪恶的化身:)请包括应用程序的示例输出以及您希望正确的输出是什么样的。因此,我建议不要对新行使用
    std::endl
    ,因为这包括一个额外的
    std::flush
    。只需对新行使用
    '\n'
    。您所说的最后一位o是什么意思f代码?顺便说一句,您可以检查以了解为什么
    使用名称空间std;
    是邪恶的化身:)对于这样一个简单的函数来说,代码太多了。在达到一定限制之前,拆分是可以的。对于太原子化的操作,拆分会增加不必要的函数调用/噪音(请认为代码需要尽可能可读)。对于这样一个简单的函数来说,代码太多。在达到某个限制之前,拆分是可以的。对于太多原子操作,拆分会增加不必要的函数调用/干扰(请认为代码需要尽可能可读)。