gpt4 book ai didi

c++ - 如何打印空心正方形?

转载 作者:太空宇宙 更新时间:2023-11-04 12:32:16 24 4
gpt4 key购买 nike

我使用二维 for 循环为类作业制作形状生成函数,但无法制作空心正方形;只有一个填充的。我传入两个参数,即宽度和所使用的符号。

我已经试过好几次了,但最终我只是在一个未完成的情况下上交了它。它应该打印这样的东西

++++
+ +
+ +
++++
#include <iostream>
#include "shapemaker.h"

using namespace std;

void shapemaker::Initialize(int w, int h, char sym)
{
width = w;
height = h;
symbol = sym;
}

int shapemaker::getcanWidth() {return width;}

int shapemaker::getcanHeight() {return height;}

char shapemaker::getSymbol() {return symbol;}

void shapemaker::setSymbol(char s) { symbol = s; }

void shapemaker::setcanWidth(int w) { width = w; }

void shapemaker::setcanHeight(int h) { height = h; }

void shapemaker::drawmidHorline()
{
symbol = getSymbol();
int drawingheight = getcanHeight();
int drawingwidth = getcanWidth();
double midpoint = getcanHeight()/2;
for(int x = 0; x < drawingwidth; x++)
{
for(int y = 0; y < drawingwidth; y++)
{
if(x == midpoint)
cout << getSymbol();
}
cout << endl;
}
}

void shapemaker::drawmidVertline()
{
symbol = getSymbol();
int drawingheight = getcanHeight();
int drawingwidth = getcanWidth();
double midpoint = getcanWidth()/2;

for(int x = 0; x < drawingwidth;x++)
{
for(int y = 0; y < drawingheight; y++)
{
if(y == midpoint)
{
cout << getSymbol();
}
cout << " ";
}
cout << endl;
}
}

void shapemaker::drawWidthsizedFullSquare()
{
symbol = getSymbol();
int drawingwidth = getcanWidth();
for(int x = 0; x < drawingwidth;x++)
{
cout << symbol;
for(int y = 0; y < drawingwidth; y++)
{
cout << symbol;
}
cout << endl;
}
}

void shapemaker::drawWidthsizedOpenSquare()
{
symbol = getSymbol();
int drawingwidth = getcanWidth();
for(int x = 0; x < drawingwidth;x++)
{
cout << getSymbol();
for(int y = 0; y < drawingwidth; y++)
{
if(x == 0 || x == drawingwidth-1)
cout << getSymbol();
}
cout << getSymbol();
cout << endl;
}
}

最佳答案

如果你想让drawWidthsizedOpenSquare打印这个

++++
+ +
+ +
++++

你应该这样写这个方法:

void shapemaker::drawWidthsizedOpenSquare()
{
symbol = getSymbol();
int drawingwidth = getcanWidth();
for(int x = 0; x < drawingwidth;x++)
{
for(int y = 0; y < drawingwidth; y++)
{
if(x == 0 || x == drawingwidth-1 || y == 0 || y == drawingwidth-1) // border cell case
{
cout << getSymbol();
}
else
{
cout << " ";
}
}
cout << endl;
}
}

关于c++ - 如何打印空心正方形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58187641/

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