1

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

这是我必须制作的盒子:

#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; 
}
4

2 回答 2

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

于 2012-06-26T06:29:33.963 回答
0
#include <iostream>
using namespace std;

void draw_rect( int width, int height)
{   
int i;  
cout << char(218); 
for (i=0; i<width-2; i++) 
cout << char(196);
cout << char(191) << endl;

for (i=0; i<height-2; i++)
{
cout << char(179);
for (int j=0; j<width-2; j++) 
cout << " ";
cout << char(179) << endl;
}
cout << char(192);

for(i=0; i<width-2; i++)
cout << char(196);
cout << char(217) << endl;
}

int main()
{
draw_rect(20,10);
return 0;
}
于 2015-05-02T18:13:04.133 回答