输出重复的 ASCII 菱形

Output the repetitive ASCII diamonds

我想要的输出看起来像这样:

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

我已经能够通过以下方法弄清楚如何获得第一个菱形:

//inside of a void function called printDots()
char dot = '*';
char blank = ' ';

for (int i = 1; i < 6; i++) {
    for (int k = 6; k > i; k--) { // right-shifts the first triangle
        std::cout << blank;
    }
    for (int j = 0; j < i; j++) { // first triangle
        std::cout << dot;
    }
    for (int j = 0; j < i; j++) { // second triangle (left-aligned)
        std::cout << dot;
    }
    std::cout << "\n";
}
for (int i = 1; i < 6; i++) {
    for (int k = 0; k < i; k++) { // right-shifts the third triangle
        std::cout << blank;
    }
    for (int j = 6; j > i; j--) { // third triangle
        std::cout << dot;
    }
    for (int j = 6; j > i; j--) { // fourth triangle (left-aligned)
        std::cout << dot;
    }
    std::cout << "\n";
}

不过,我正在努力弄清楚如何以所需的模式重复这个形状。我已经尝试添加更多 for 循环来做我已经在做的同样的事情来制作钻石的所有四个部分,但它似乎只是将星号垂直向下移动并破坏图案,所有这些都没有实现任意横向输出。我可以用一个大的 std::cout 语句手动输出模式,但我想弄清楚如何生成形状而不是仅仅复制和粘贴它。有什么建议吗?

我会重新设计您的应用程序,以便您具有打印一行菱形的功能:

所以最初的程序是这样的:

void printDiamondLine(int line)
{
    // You write this part
}

int main()
{
     for(int loop = 0; loop < sizeOfDiamong; ++loop)
     {
         printDiamondLine(loop);
         std::cout << "\n";
     }
}

这应该打印出来:

    **    Notice that each line is space filled on both sides.
   ****   
  ******  
 ******** 
**********
**********
 ******** 
  ******  
   ****   
    **    

现在您可以更改代码以打印多个钻石:

int main()
{
     // Looping over the whole things
     // to get multiple lines of diamonds.
     for(int vertical = 0; vertical < verticalDiamonds; ++vertical)
     {
         for(int loop = 0; loop < sizeOfDiamong; ++loop)
         {
             // Printing each line of the diamond
             // multiple times across.
             for(int horz = 0; horz < horzDiamonds; ++horz)
             {
                 printDiamondLine(loop);
                 std::cout << " ";
             }
             std::cout << "\n";
         }
     }
}

您必须一次将其更多地视为 1 行。你的第一个大 foo 循环产生每个菱形的上半部分,第二个大 for 循环产生下半部分,是吗?

我只处理其中的一半,因为它从顶部和底部变为相同的解决方案:

// This produces 5 lines of output.
for (int i = 1; i < 6; i++) {
    // This puts out leading spaces as required for this line
    for (int k = 6; k > i; k--) { // right-shifts the first triangle
        std::cout << blank;
    }

    // This puts out the diamond itself -- just this line
    for (int j = 0; j < i; j++) { // first triangle
        std::cout << dot;
    }

    // And this puts out the second half of the diamond -- just this line
    for (int j = 0; j < i; j++) { // second triangle (left-aligned)
        std::cout << dot;
    }

    // Note: THIS IS WHERE WE'LL INSERT MORE CODE

    // And this is why you're only getting 1
    std::cout << "\n";
}

我评论了代码。到目前为止,你实际上做得很好。但是现在,让我们保留所有代码,但我要在最后的计算之前添加一点。

    for (int k = 6; k > i; k--) {
        std::cout << blank;
    }

这样做是 space- 填充行,以便(到目前为止)每行的长度相同。

然后您所要做的就是将大部分代码包装在另一个 for 循环中。你最终得到这样的结果:

for (int i = 1; i < 6; i++) {
    for (int whichDiamond = 0; whichDiamond < 3; ++whichDiamond) {
        for (int k = 6; k > i; k--) { // right-shifts the first triangle
            std::cout << blank;
        }
        for (int j = 0; j < i; j++) { // first triangle
            std::cout << dot;
        }
        for (int j = 0; j < i; j++) { // second triangle (left-aligned)
            std::cout << dot;
        }
        for (int k = 6; k > i; k--) { // right-shifts the first triangle
            std::cout << blank;
        }
    }
    std::cout << "\n";
}

你可以用第二个大循环做同样的事情。

现在,可能有更聪明的方法来执行此操作,但这适用于您现有的代码。