使用C / C ++中带边框的星号打印菱形

时间:2011-12-22 01:10:37

标签: c++

我需要在C / C ++

中使用星号打印带边框的菱形

下面的代码只渲染钻石,但我需要的是一个边框。我该怎么办?

期望:

diamond with border

现实:

diamond without border

#include <iostream>

void PrintDiamond(int iSide) {
    using namespace std;
    int iSpaces = iSide;
    int iAsterisks = 1;
    // Output the top half of the diamond
    for (int iI = 0; iI < iSide; ++iI) {
        for (int iJ = 0; iJ < iSpaces; ++iJ) {
            cout << " ";
        }
        for (int iJ = 0; iJ < iAsterisks; ++iJ) {
            cout << "*";
        }
        cout << endl;
        --iSpaces;
        iAsterisks += 2;
    }
    // Output the bottom half of the diamond
    for (int iI = 0; iI <= iSide; ++iI) {
        for (int iJ = 0; iJ < iSpaces; ++iJ) {
            cout << " ";
        }
        for (int iJ = 0; iJ < iAsterisks; ++iJ) {
            cout << "*";
        }
        cout << endl;
        ++iSpaces;
        iAsterisks -= 2;
    }
}

int main()
{
    // Print a diamond with side = 4
    PrintDiamond(4);

    return 0;
}

5 个答案:

答案 0 :(得分:3)

#include <iostream>

void PrintDiamond(int iSide) {
    using namespace std;
    int iSpaces = iSide;
    int iAsterisks = 1;
    // Output the top half of the diamond

    // ADDED: here you print the top border, the number of * is
    // calculated from iSide
    for (int i = 0; i < iSide*2+3; i++)
        cout << "*";
    cout << endl;


    for (int iI = 0; iI < iSide; ++iI) {
        // ADDED: print one * of the left border, and several blanks
        cout << "*";
        for (int iJ = 0; iJ < iSpaces; ++iJ) {
            cout << " ";
        }

        // here is your original code that prints the main part
        for (int iJ = 0; iJ < iAsterisks; ++iJ) {
            cout << "*";
        }

       // ADDED: the same as the left border, we print blanks and then
       // one * of the right border
        for (int iJ = 0; iJ < iSpaces; ++iJ) {
            cout << " ";
        }
        cout << "*";
        cout << endl;

        --iSpaces;
        iAsterisks += 2;
    }
    // Output the bottom half of the diamond
    for (int iI = 0; iI <= iSide; ++iI) {
        cout << "*";
        for (int iJ = 0; iJ < iSpaces; ++iJ) {
            cout << " ";
        }
        for (int iJ = 0; iJ < iAsterisks; ++iJ) {
            cout << "*";
        }
        for (int iJ = 0; iJ < iSpaces; ++iJ) {
            cout << " ";
        }
        cout << "*";
        cout << endl;
        ++iSpaces;
        iAsterisks -= 2;
    }

    // ADDED: we end up with the bottom border, which is the same as the top one
    for (int i = 0; i < iSide*2+3; i++)
        cout << "*";
    cout << endl;
}

int main()
{
    // Print a diamond with side = 4
    PrintDiamond(4);

    return 0;
}

答案 1 :(得分:1)

嗯,这并不是那么困难......对于盒子的顶部和底部,打印完整行星号的for就足够了。至于其他方面,只需在每行的开头和末尾添加一个星号。

答案 2 :(得分:0)

在我看来,你只是错过了边界。你可以在“*”的顶部和底部添加静态行,然后在每行的开头和结尾添加*

像这样......

#include <iostream>

void PrintDiamond(int iSide) {
    using namespace std;
    int iSpaces = iSide;
    int iAsterisks = 1;
    for (int k = 0; k < iSide*2+3; ++k) {
        cout << "*";
    }
    cout << endl;
    // Output the top half of the diamond
    for (int iI = 0; iI < iSide; ++iI) {
        cout << "*";
        for (int iJ = 0; iJ < iSpaces; ++iJ) {
            cout << " ";
        }
        for (int iJ = 0; iJ < iAsterisks; ++iJ) {
            cout << "*";
        }
        for (int iJ = 0; iJ < iSpaces; ++iJ) {
            cout << " ";
        }
        cout << "*";
        cout << endl;
        --iSpaces;
        iAsterisks += 2;
    }
    // Output the bottom half of the diamond
    for (int iI = 0; iI <= iSide; ++iI) {
        cout << "*";
        for (int iJ = 0; iJ < iSpaces; ++iJ) {
            cout << " ";
        }
        for (int iJ = 0; iJ < iAsterisks; ++iJ) {
            cout << "*";
        }
        for (int iJ = 0; iJ < iSpaces; ++iJ) {
            cout << " ";
        }
        cout << "*";
        cout << endl;
        ++iSpaces;
        iAsterisks -= 2;
    }
    for (int k = 0; k < iSide*2+3; ++k) {
        cout << "*";
    }
    cout << endl;
}

int main()
{
    // Print a diamond with side = 4
    PrintDiamond(4);

    return 0;
}

希望这有帮助!

编辑:Oop有一些问题没有想到它一直到那时当我发现我上面的那个人有同样的事情xD

答案 3 :(得分:0)

代码:(不是原版的很多修改)

void PrintDiamond(int iSide) {
using namespace std;
int iSpaces = iSide;
int width = (iSide)*2+1+2 //number of spaces + number of '*' + the first and last '*'
int iAsterisks = 1;
// Output the top half of the diamond
//first line : 
for (int iI = 0; iI < width; ++iI) 
{
    cout<<"*";
}
cout << endl;

for (int iI = 0; iI < iSide; ++iI) {
    cout<<"*";
    for (int iJ = 0; iJ < iSpaces; ++iJ) {
        cout << " ";
    }
    for (int iJ = 0; iJ < iAsterisks; ++iJ) {
        cout << "*";
    }
    for (int iJ = 0; iJ < iSpaces; ++iJ) {
        cout << " ";
    }
    cout<<"*";
    cout << endl;
    --iSpaces;
    iAsterisks += 2;
}
// Output the bottom half of the diamond
for (int iI = 0; iI <= iSide; ++iI) {
    cout<<"*";
    for (int iJ = 0; iJ < iSpaces; ++iJ) {
        cout << " ";
    }
    for (int iJ = 0; iJ < iAsterisks; ++iJ) {
        cout << "*";
    }
    for (int iJ = 0; iJ < iSpaces; ++iJ) {
        cout << " ";
    }
    cout << "*";
    cout << endl;
    ++iSpaces;
    iAsterisks -= 2;
}
//last line :
//first line : 
for (int iI = 0; iI < width; ++iI) 
{
    cout<<"*";
}
cout << endl;
}

答案 4 :(得分:0)

我建议的替代方案:

#include <iostream>
const int ROWS = 11, COLS = 11, HMID = COLS/2, VMID = ROWS/2;

int main(){
    int c, r, d = -1;
    for(r=0; r < ROWS; r++){
        for(c=0; c< COLS; c++){
            char pixel = ' ';
            if ((r*c) >= (r*(HMID-d))) pixel = '*';
            if ((r*c) > (r*(HMID+d))) pixel = ' ';
            if (c == 0 || r == 0 || r == ROWS-1 || c == COLS-1) pixel='*' ;
            std::cout << pixel;
        }
        if (r < VMID) d++; else d--;
        std::cout << std::endl;
    }
}

如果你放弃CONSTANT声明并将它们直接放到位,你可以将它缩短1行,用它们的值替换COLS,ROWS,HMID,VMID。