gpt4 book ai didi

c++ - 如何使用循环为 C++ 绘制矩形?

转载 作者:行者123 更新时间:2023-11-30 04:26:58 24 4
gpt4 key购买 nike

所以我有这个制作盒子的代码,但想要制作角 +、长度 | 和宽度 - 。还想输入一个数字,这样你就可以像 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;
}

最佳答案

绘制一个矩形,其中 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

关于c++ - 如何使用循环为 C++ 绘制矩形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11201728/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com