我使用二维的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;
}
}
答案 0 :(得分:0)
如果您要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;
}
}