创建用户在C++中定义的星形对角线

Creating a diagonal line of stars that the user has defined in C++

本文关键字:对角线 定义 用户 C++ 创建      更新时间:2023-10-16

我必须编写一些需要创建星号对角线的代码。我知道如何编写输出三角形的代码:

cout<<"What size of stars would you like to draw? ";
cin>>star;
int space;
for(int i = 1, k = 0; i <= star; I++, k = 0)
{
    for(space = 1; space <= star-i; space++)
    {
        cout <<"  ";
    }
    while(k != 2*i-1)
    {
        cout << "* ";
        k++;
    }
    cout << endl;

更改了该代码以从中创建半三角形,我似乎找不到如何使其仅成为对角线。这是我对半三角形的了解:

for(int i = 1, k = 0; i <= star; i++, k = 0) {
    while(k != 2*i-1) {
        cout << "* ";
        k++;
    }
    cout << endl;
}

只需将对角线想象成一个充满空白空间的半三角形:-(。

for(int i = 1, k = 0; i <= star; i++, k = 0) {
    while(k != 2*i-1) {
        cout << "  ";    // print whitespace until the end of the row
        k++;
    }
    cout << "*" << endl; // print a dot at the end
}