如何在C ++中绘制矩形 [英] how to draw a rectangle in C++

查看:490
本文介绍了如何在C ++中绘制矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好..

是C ++初学者,我想知道如何在C ++中绘制矩形

这是我到目前为止尝试过的方法,但是,矩形的顶部和底部都没有任何内容,请有人帮助我获得在C ++中绘制矩形或三角形的代码

这是下面的ma代码

hello every Body ..

am a c++ beginner and i would like to know how can i draw a rectangle in C++

this is what i have tried so far but , there is nothing on the top and the bottom of my rectangle, please can any one help me get code to draw a rectangle, or a triangle in c++

this is ma code below

#include <iostream>
#include <string>

using namespace std;

 int main( void )
 { 
	 string spaces( 20, '' '' ); 

	 cout<< ''#'' << spaces << "#\n";
	 cout<< ''#'' << spaces << "#\n";
	 cout<< ''#'' << spaces << "#\n";
	 cout<< ''#'' << spaces << "#\n";
	 system("pause");
	 return 0;
 }

推荐答案

只需在侧面前后定义string border(20,''#'') cout 即可:

just define a string border(20,''#'') and cout it before and after the sides:

#include <iostream>
#include <string>
using namespace std;
 int main( void )
 {
     string border( 20, '#');
     string spaces( 20, ' ' );
     cout<< '#' << border << "#\n";
     cout<< '#' << spaces << "#\n";
     cout<< '#' << spaces << "#\n";
     cout<< '#' << spaces << "#\n";
     cout<< '#' << spaces << "#\n";
     cout<< '#' << border << "#\n";
     system("pause");
     return 0;
 }



但现在尝试了解一下:



But now try to understand this:

#include <string>
#include <iostream>
int main()
{
    static const int WIDTH=18;
    static const int HEIGHT=8;

    std::string border(WIDTH-2,'#');
    std::string spaces(WIDTH-2,' ');
    std::cout << '#' << border << '#' << std::endl;
    for(unsigned int i=1; i<HEIGHT-1; ++i)
    {
        std::cout << '#' << spaces << '#' << std::endl;
    }
    std::cout << '#' << border << '#' << std::endl;

    system("pause");
    return 0;
}



您了解常量和控件结构的用法吗?!?



Do you understand the use of constants and of the control structures ?!?


这篇关于如何在C ++中绘制矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆