如何在C ++中创建一个正方形

时间:2013-12-18 20:41:49

标签: c++

如何在C ++中制作一个方框?

a square box

2 个答案:

答案 0 :(得分:16)

我不确定你要做的是什么,但这听起来像是我的第一个程序之一(用巨大的图形在屏幕上打印“BEAUTY EH?”。绘制问号非常困难。)。但这是一个显示如下框的程序:

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

这是程序:

#include <cstdlib>  // It's always best to include this file before any others.
#include <iostream> // This is so we can use "cout"

int main()
{
    // first let's save ourselves a little typing
    using namespace std;

    // We're going to draw a box of stars ("*").
    // It will be 80 characters wide by 10 rows tall.
    //
    // Since we're writing to the console using 'cout',
    // we're just going to write one line at a time,
    // and then issue a carraige return to start printing
    // on the next line.

    // First let's draw the top "wall", which is a solid 
    // row of 80 stars, one at a time
    for (int column = 0; column < 80; ++column)
    {
        cout << "*";
    }
    // now print a carraige return, so we can start printing on the next line
    cout << "\n";

    // Now we're going to print the sides.
    // There are 8 rows here.  Each row is a star, followed by
    // 78 spaces, followed by another star and a carraige return.
    for (int row = 0; row < 8; ++row)
    {
        // print the left "wall"
        cout << "*";
        // now print 78 spaces
        for (int column = 0; column < 78; ++column)
        {
            cout << " ";
        }
        // finally print the right "wall" and a carraige return
        cout << "*\n";
        // continue the for loop to print the next row
    }

    // Once the loop is done, we can print the bottom wall the same way we printed the top one.
    for (int column = 0; column < 80; ++column)
    {
        cout << "*";
    }
    // now print a carraige return, so we can start printing on the next line
    cout << "\n";

}

答案 1 :(得分:0)

使用ascii字符可以帮助您制作漂亮的盒子。如果你看一下这个网站上的表:http://www.theasciicode.com.ar/extended-ascii-code/box-drawing-character-ascii-code-179.html,你可以看到185-188和200-207非常适合制作一个盒子。我使用187和188绘制右角,左角为200和201,垂直和水平墙为186和205。请记住,整数和字符在编译器的脑海中是相同的,所以如果将这些整数值赋给char变量,它将输出ascii值。