如何用循环绘制c ++的矩形?

时间:2012-06-26 06:21:14

标签: c++

所以我有这个代码制作一个盒子,但想要制作角+,长度|和宽度 - 。还想输入一个数字,这样你就可以像cout一样绘制它们<<“输入长度数字等等......我该怎么做?

以下是我要制作一个盒子的内容:

#include <iostream.h> 
#include <string.h> 

void main() 
{ 
  for(int z=1; z<=79; z++) 
  { 
    cout << ""; 
  } 

  cout << endl; 

  for(int i=1; i<=5; i++) 
  { 
    cout << ""; 
    for(int j=1; j<=77; j++) 
    { 
      cout << " "; 
    } 

    cout << "" << endl; 
  } 

  for(int y=1; y<=79; y++) 
  { 
    cout << ""; 
  } 

  cout << endl; 
}

1 个答案:

答案 0 :(得分:2)

绘制一个矩形,其中int height是高度,int width是宽度

#include <iostream>

void draw_rect(int width,int height) 
{
    using std::cout;
    cout << "+";
    for (int i = 0; i < width - 2; i++)
    {
        cout << "-";
    }
    cout << "+\n";

    for (int i = 0; i < height - 2; i++)
    {
        cout << "|";
        for (int j = 0; j < width - 2; j++)
        {
            cout << " ";
        }
        cout << "|\n";
    }

    cout << "+";
    for (int i = 0; i < width - 2; i++)
    {
        cout << "-";
    }
    cout << "+\n";
}

int main ()
{
    draw_rect(8,6);
    return 0;
}

关于如何获取用户输入,请阅读: Basic C++ IO