如何在 C++ 中制作正方形

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20667960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:16:25  来源:igfitidea点击:

How do I make a square in C++

c++

提问by John Dibling

How to make a square box in C++?

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

a square box

一个方盒

回答by John Dibling

I'm not sure what you're trying to do exactly but it sounds like one of my first programs (which printed "BEAUTY EH?" to the screen in huge graphics. Drawing the question mark was really hard.). But here is a program that displays a box like this:

我不确定你到底想做什么,但它听起来像是我的第一个程序(它以巨大的图形将“BEAUTY EH?”打印到屏幕上。画问号真的很难。)。但这里有一个程序,它显示一个这样的框:

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

And here's the program:

这是程序:

#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";

}

回答by JGroven

Using ascii characters can help you make a great looking box. If you look at the table on this site: http://www.theasciicode.com.ar/extended-ascii-code/box-drawing-character-ascii-code-179.html, you can see that 185-188, and 200-207 line up pretty nicely to make a box. I use 187 and 188 to draw the right corners, 200 and 201 for the left corners, and 186 and 205 for vertical and horizontal walls. Remember that integers and characters are the same thing in the compiler's mind, so if you assign these integer values to a char variable, it will output the ascii value.

使用 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 值。